Docker on Compose, registries and production
Compose runs a whole app from one file
compose.yamllists the app's services, one per container, with networks and volumes.docker compose upstarts everything.docker compose downstops and removes it.- Compose puts all services on one network, so
webreaches the cache atcache:6379. - Type
docker composewith a space.docker-composeis the old v1, andversion:is ignored.
services:
web:
build: .
ports: ["8080:3000"]
depends_on:
cache: { condition: service_healthy }
cache:
image: redis:8
healthcheck: { test: redis-cli 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_healthyholds a service back until the one it needs is healthy.- In a Dockerfile,
HEALTHCHECKruns 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 withdocker login, then push. - The server pulls and runs the exact image you tested, not a fresh build.
docker buildx build --platform linux/amd64,linux/arm64covers x86 and ARM chips.
- docker buildimage on your machine
- docker pushimage in the registry
- docker pullimage on the server
- docker runcontainer serving users
Run the app as a non-root user
- Unless the image says otherwise, the process in a container runs as root.
- Add
USERto the Dockerfile to switch. The official Node.js images ship anodeuser. - 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
ARGorENVstay in the image, where anyone who pulls it can read them. - For builds,
RUN --mount=type=secret,id=tokenshows 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-appdoes this with Docker Scout. Run it in CI on every build.
Whoever controls the Docker daemon controls the host
- The
dockercommand 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.