Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Learn Docker Step by Step: First Container to Production

· 6 min read · Home Tech

In this article
  1. What Is Docker and Why Use It?
  2. Installing Docker and Running Your First Container
  3. Understanding Images, Containers, and Dockerfiles
  4. Managing Data: Volumes and Bind Mounts
  5. Networking and Docker Compose
  6. Going to Production: Best Practices and Security
  7. Common Pitfalls and How to Avoid Them
  8. Frequently Asked Questions

This guide walks you through Docker from the very first container you run to deploying applications in production. You will learn what containers are, how to build and manage images, handle data and networking, and finally how to run Docker securely and reliably in a production environment.

What Is Docker and Why Use It?

Docker is a platform that packages an application and its dependencies into a lightweight, isolated unit called a container. Containers share the host operating system kernel but run in their own user space, making them faster to start and more resource-efficient than virtual machines. Docker solves the classic “it works on my machine” problem by ensuring the same image behaves identically across development, testing, and production.

Key benefits include:

  • Consistency – the same image runs everywhere.
  • Isolation – each container has its own filesystem, network, and process space.
  • Portability – images can be moved between hosts, clouds, and on-premise servers.
  • Scalability – you can start or stop containers in seconds, making horizontal scaling easy.

Docker is used by developers, DevOps engineers, and system administrators for microservices, CI/CD pipelines, and application packaging.

Installing Docker and Running Your First Container

Install Docker Desktop on Windows or macOS, or the Docker Engine on a Linux distribution. After installation, verify it with docker --version. Then run your first container:

docker run hello-world

This command downloads a tiny test image and runs it, printing a confirmation message. The image is pulled from Docker Hub, the default public registry. The container exits immediately because the image’s main process finishes.

To run a more interactive container, use the nginx web server:

docker run -d -p 8080:80 nginx
  • -d runs the container in the background (detached).
  • -p 8080:80 maps host port 8080 to container port 80.

Open http://localhost:8080 in your browser to see the nginx welcome page. List running containers with docker ps, stop one with docker stop <container-id>, and remove it with docker rm <container-id>.

Understanding Images, Containers, and Dockerfiles

An image is a read-only template with the application code, runtime, libraries, and settings. A container is a running instance of an image. Images are built from a Dockerfile, a text file with instructions. Here is a minimal example for a Python app:

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Build the image with:

docker build -t my-app .

Run it with:

docker run -p 5000:5000 my-app

Each line in the Dockerfile creates a layer. Layers are cached; if you change a line, only the layers after it are rebuilt. To keep images small, use a minimal base image, combine RUN commands, and clean up temporary files in the same layer.

Managing Data: Volumes and Bind Mounts

Containers are ephemeral by default – any data written inside is lost when the container is removed. To persist data, use volumes or bind mounts.

  • Volumes are managed by Docker and stored in the host filesystem under /var/lib/docker/volumes. Create one with docker volume create mydata and attach it with -v mydata:/data.
  • Bind mounts map a host directory directly into the container, e.g., -v /host/path:/container/path. Useful for development because changes are reflected immediately.

For databases, always use a volume to avoid losing data. In a docker-compose.yml file, you can declare volumes and bind mounts under the service definition.

Networking and Docker Compose

Docker provides several network drivers. The default bridge network lets containers communicate with each other via IP addresses. For multi-container applications, use Docker Compose, which defines services, networks, and volumes in a YAML file. Example:

version: "3.8"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  db:
    image: postgres:16
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: example
volumes:
  db-data:

Run the whole stack with docker-compose up -d. Compose automatically creates a network so services can find each other by service name (e.g., db). This is ideal for local development and small deployments.

Going to Production: Best Practices and Security

Production Docker requires more than just docker run. Follow these practices:

  • Use a registry – push images to a private registry like Docker Hub, Amazon ECR, or Google Artifact Registry.
  • Tag images properly – use semantic versions or commit hashes, not just latest.
  • Run as a non-root user – add USER appuser in the Dockerfile to reduce security risks.
  • Scan images for vulnerabilities – use tools like docker scan or Trivy.
  • Use secrets management – do not hardcode passwords; use Docker secrets or environment variables from a secure source.
  • Set resource limits – use --memory and --cpus to prevent a container from consuming all host resources.
  • Use orchestration – for multiple hosts and auto-scaling, use Kubernetes or Docker Swarm.
  • Log and monitor – send logs to a central system (ELK, Loki) and set up health checks.

A common misconception is that containers are less secure than VMs. In reality, containers share the kernel, so a vulnerability in the kernel affects all containers. Always keep the host kernel updated and run containers with the least privileges needed.

Common Pitfalls and How to Avoid Them

  • Running as root – always create a non-root user in the image.
  • Not pinning base image versions – use a specific tag like python:3.12-slim instead of latest.
  • Storing data inside the container – always use volumes for persistent data.
  • Ignoring layer caching – order Dockerfile instructions from least to most frequently changing to maximize cache hits.
  • Exposing unnecessary ports – only publish the ports you need.
  • Forgetting to clean up – use docker system prune to remove unused images, containers, and networks.

Frequently Asked Questions

What is the difference between Docker and a virtual machine?

Docker containers share the host OS kernel, while VMs each run a full guest OS. Containers start in seconds and use less memory, but VMs provide stronger isolation. For most applications, containers are more efficient.

How do I get data out of a container?

Use docker cp <container>:/path /host/path to copy files. For databases, use the native dump tools (e.g., pg_dump) and redirect output to a file on the host. Better yet, store data in a volume from the start.

Can I run Docker on Windows or macOS?

Yes, Docker Desktop provides a Linux VM that runs the Docker Engine. You can also use WSL 2 on Windows for better performance. On macOS, Docker Desktop is the standard option.

What is Docker Compose used for?

Docker Compose defines and runs multi-container applications with a single YAML file. It is perfect for local development and small production deployments, letting you start all services with one command.

How do I update a running container?

Pull the new image, stop the old container, and run a new one with the same volumes and network settings. In production, use rolling updates with an orchestrator like Kubernetes to avoid downtime.

Is Docker free to use?

Docker Engine is open source and free. Docker Desktop is free for small businesses (under 250 employees and under $10 million revenue) but requires a paid subscription for larger companies. Check the current licensing terms on Docker’s website.