Docker: A Beginner’s Guide

Dipak MaliDipak Mali
3 min read

Docker is a platform that lets you build, ship, and run applications in containers. Containers are lightweight, portable environments that hold everything your app needs—code, libraries, and dependencies—so it works the same on any system with Docker.


What is a Container?

A container is an isolated space for your app. It’s like a mini-machine that shares your computer’s resources efficiently. This is called containerization, and it ensures your app runs consistently everywhere.


Docker Images

A Docker image is a template for creating containers. You build it using a file called a Dockerfile, which lists the setup steps. Think of it as a recipe for your app’s environment.


What is a Dockerfile?

A Dockerfile is a text file that contains instructions to build a Docker image.

Key Instructions:

  • FROM: Base image (e.g., python:3.9, openjdk:17)

  • WORKDIR: Sets the working directory inside the container

  • COPY: Copies files from host to container

  • RUN: Runs commands (e.g., install dependencies)

  • CMD: Default command when the container runs

  • EXPOSE: Documents the port the app runs on

  • ENV: Sets environment variables

Example

FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Docker Hub

Docker Hub is an online library where you can find, download (pull), or upload (push) Docker images. For example, you can grab an Nginx web server image and run it instantly.


Docker Desktop and Docker CLI

  • Docker Desktop: A handy app for Mac and Windows with a graphical interface to manage containers and images.

  • Docker CLI: The command-line tool where you type commands to control Docker. It’s your main way to interact with it.


Key Docker Commands

Here’s what you’ll use most:

  • docker run: Starts a container from an image.
    Example: docker run -d -p 80:80 nginx (runs Nginx, maps port 80).

  • docker ps: Lists running containers (-a for all).

  • docker stop: Stops a container.
    Example: docker stop my_container.

  • docker rm: Deletes a stopped container.
    Example: docker rm my_container.

  • docker images: Lists your images.

  • docker rmi: Deletes an image.
    Example: docker rmi my_image.

  • docker build: Builds an image from a Dockerfile.
    Example: docker build -t my_image ..

  • docker pull: Downloads an image from Docker Hub.
    Example: docker pull nginx.

  • docker push: Uploads an image to Docker Hub.
    Example: docker push my_image.

  • docker exec: Runs a command in a container.
    Example: docker exec -it my_container bash (opens a shell).


Port Mapping

Port mapping connects a port on your machine to one in the container. For example, -p 80:80 in docker run lets you access your app on port 80 of your computer.


Kubernetes

Kubernetes manages lots of containers across multiple machines. It automates starting, stopping, and scaling them. Key terms:

  • Pods: Smallest unit, holds one or more containers.

  • Services: Exposes pods to the network.

  • Deployments: Keeps the right number of pods running.

  • Namespaces: Organizes resources in a cluster.


Summary

Docker simplifies app development with containers, images, and tools like Docker Desktop and CLI. Docker Hub shares images, and Kubernetes handles big container setups. It’s straightforward and saves headaches!

0
Subscribe to my newsletter

Read articles from Dipak Mali directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dipak Mali
Dipak Mali

Hello, I'm Dipak, a Junior Software Engineer with a strong foundation in React JS, Python, and Java. I’m a working professional focused on developing efficient software solutions in the dynamic adtech industry. Beyond core coding, I specialize in RESTful API design, web development, and have hands-on experience with technologies like Spring Boot, MySQL, Docker, and AWS cloud services. I thrive on creating scalable, user-centric applications and enjoy tackling complex problem-solving challenges. Eager to collaborate, I’m passionate about innovation, continuous learning, and making a meaningful impact through technology.