Package an application so it runs the same everywhere

A trail about containers for developers: images and Dockerfiles, the container lifecycle, data, networking and Docker Compose. Each lesson takes one command apart with an example and a common mistake, and at the end you build a compact production image.

Why containers

What a container packages and how it differs from a virtual machine.

  1. “It works on my machine” — What exactly a container carries along with the program.
  2. A container is not a virtual machine — Why a container starts in a fraction of a second and what it shares with the host.
  3. Your first docker run — What happens at start and how to find a stopped container.

Images

Tags, layers, the Dockerfile and the build cache.

  1. Tags and layers — Why latest does not mean “fresh” and how images share disk space.
  2. The Dockerfile — Writing an image recipe and building it.
  3. The build cache — How the order of instructions saves minutes on every build.

Life of a container

Stopping, logs, getting inside and the start command.

  1. Stopping and restarting — How a container receives a signal and why it sometimes waits ten seconds.
  2. Logs and getting inside — Where to find the program's output and how to look at a container from the inside.
  3. CMD, ENTRYPOINT and PID 1 — How to set the start command so that signals reach the program.

Data

Where to keep what must outlive a container.

  1. The container layer is temporary — Why data disappears and where to keep it.
  2. Volumes and host folders — Named volumes, bind mounts and when to choose which.
  3. Backups and read-only mounts — How not to lose a volume and how to protect a config from writes.

Networking

Ports, networks between containers and what localhost means inside.

  1. Publishing ports — How to make a service in a container reachable from outside.
  2. Networks between containers — How containers find each other by name.
  3. localhost inside a container — Why localhost in a container is not your computer.

Docker Compose

Several services in one file: start-up, order and settings.

  1. The compose.yaml file — Describing an application and its database instead of long commands.
  2. Order and readiness — Why “started” does not mean “ready” and how to fix it.
  3. Variables and secrets — How to pass settings without putting a password into the image.

A production image

Multi-stage builds, security and keeping the disk tidy.

  1. Multi-stage builds — Build in one image, run in another.
  2. A secure image — A non-root user, a fresh base and nothing extra.
  3. Size and clean-up — Finding where image weight comes from and cleaning the disk without losses.