Skip to content
Engineering Bible
Docker

Docker on Compose, registries and production

Compose runs a whole app from one file

  • compose.yaml lists the app's services, one per container, with networks and volumes.
  • docker compose up starts everything. docker compose down stops and removes it.
  • Compose puts all services on one network, so web reaches the cache at cache:6379.
  • Type docker compose with a space. docker-compose is the old v1, and version: is ignored.
services:
  web:
    build: .
    ports: ["8080:3000"]
    depends_on:
      cache: { condition: service_healthy }
  cache:
    image: redis:8
    healthcheck: { test: redis-cli ping }
A web app built from the local Dockerfile, started only once its Redis cache answers a ping.

A health check says when a service is ready

  • With plain depends_on, Compose waits until a container is running, not until it's ready.
  • A health check is a command Docker runs inside the container. Exit code 0 means healthy.
  • condition: service_healthy holds a service back until the one it needs is healthy.
  • In a Dockerfile, HEALTHCHECK runs every 30 seconds and fails after 3 misses by default.

A registry carries the image to your servers

  • A registry stores images. Docker Hub is the default, GitHub and the clouds run others.
  • Name the image for the registry with docker tag, sign in with docker login, then push.
  • The server pulls and runs the exact image you tested, not a fresh build.
  • docker buildx build --platform linux/amd64,linux/arm64 covers x86 and ARM chips.
  1. docker buildimage on your machine
  2. docker pushimage in the registry
  3. docker pullimage on the server
  4. docker runcontainer serving users
Build once, then run the same image everywhere. In practice a CI job does the build and push.

Run the app as a non-root user

  • Unless the image says otherwise, the process in a container runs as root.
  • Add USER to the Dockerfile to switch. The official Node.js images ship a node user.
  • A bug in a non-root app gives an attacker far less to work with.
  • Avoid --privileged. It gives back every kernel permission that Docker drops by default.

Keep secrets out of images and environment variables

  • Values passed with ARG or ENV stay in the image, where anyone who pulls it can read them.
  • For builds, RUN --mount=type=secret,id=token shows the secret to that one step only.
  • At run time, Compose secrets appear as files under /run/secrets/.
  • Environment variables reach every process and can end up printed in logs.

Scan images for known vulnerabilities

  • An image carries many packages you didn't write, and new flaws in them keep turning up.
  • A scanner lists every package in the image and checks each against a vulnerability database.
  • docker scout cves todo-app does this with Docker Scout. Run it in CI on every build.

Whoever controls the Docker daemon controls the host

  • The docker command talks to a background service, the daemon, which runs as root.
  • Anyone allowed to send it commands can mount host folders and change any file.
  • Give that access only to people and tools you would trust with root.
  • Rootless mode runs the daemon as a normal user, which limits the damage.
Docker in four pages4 / 4