When to switch
Move from docker run to Compose when any of these is true:
- You run more than one container that talk to each other, such as an app and its database.
- The command has grown past one line and you keep it in a notes file or your shell history.
- Someone else (or you, in six months) needs to start the same setup.
- You want
docker compose up -danddocker compose downto start and stop everything together.
Compose doesn't replace Docker. It reads a YAML file and makes the same API calls docker run would, with a project name,
a shared network and readable configuration.
One command, translated
A typical web server started by hand:
docker run -d --name web --restart unless-stopped \ -p 8080:80 \ -v ./site:/usr/share/nginx/html:ro \ -e API_URL=http://api:3000 \ nginx:1.27
The same thing as compose.yaml:
services:
web:
image: nginx:1.27
container_name: web
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./site:/usr/share/nginx/html:ro
environment:
- API_URL=http://api:3000
Start it with docker compose up -d. The Docker run to Compose converter does this
translation for you, including several commands at once.
Flag by flag
docker run | compose.yaml |
|---|---|
--name app | container_name: app (usually leave it out, so you can scale) |
-p 3000:3000 | ports: ["3000:3000"] |
-v ./src:/app | volumes: ["./src:/app"] |
-v data:/var/lib/data | the same, plus data: under a top-level volumes: |
-e NODE_ENV=production | environment: {NODE_ENV: production} |
--env-file .env.app | env_file: [.env.app] |
--restart unless-stopped | restart: unless-stopped |
--network backend | networks: [backend], declared at the top level |
-u 1000:1000 | user: "1000:1000" |
-w /app | working_dir: /app |
--memory 512m --cpus 1.5 | mem_limit: 512m, cpus: 1.5 |
--cap-add NET_ADMIN | cap_add: [NET_ADMIN] |
--add-host host.docker.internal:host-gateway | extra_hosts: ["host.docker.internal:host-gateway"] |
-it | stdin_open: true and tty: true |
| arguments after the image | command: ["npm", "start"] |
-d, --rm | no key: use docker compose up -d; docker compose down removes the containers |
Every line in this table was checked by running a file with all of these keys through docker compose config.
Gotcha 1: named volumes must be declared
docker run -v pgdata:/var/lib/postgresql/data creates the volume for you. In Compose, a named volume has to be listed at the top
level too, or Compose refuses the file:
service "db" refers to undefined volume pgdata: invalid compose project
Add it at the bottom of the file:
volumes: pgdata:
Paths that start with ./ or / are bind mounts and don't need this. Relative paths are resolved from the folder
that holds compose.yaml, not from where you run the command.
Gotcha 2: a $ in a value
Compose replaces $NAME and ${NAME} with variables from your shell or .env file. That's useful, until a
password contains a dollar sign:
environment: - PASSWORD=pa$word1
WARN The "word1" variable is not set. Defaulting to a blank string. PASSWORD: pa
The container silently gets the password pa. Write $$ for a literal dollar sign (pa$$word1).
Moving the value to an env_file doesn't avoid this: Compose reads PASSWORD=pa$word1 in an env file the same way.
There, wrap the value in single quotes, PASSWORD='pa$word1', and it's passed through unchanged. Run
docker compose config and read the warnings before the first start.
Gotcha 3: depends_on doesn't wait for “ready”
depends_on: [db] only makes Compose start the database container first. Your app can still start before Postgres accepts
connections and crash on its first query. To wait until it's actually ready, give the database a health check and depend on it:
services:
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: example
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 10
web:
image: nginx:1.27
depends_on:
db:
condition: service_healthy
volumes:
pgdata:
Gotcha 4: --link and localhost
Inside Compose, every service is reachable from the others by its service name: the web container connects to
db:5432, not localhost:5432. Compose creates a shared network for the project, so the old --link flag
isn't needed. localhost inside a container means that container itself.
Quote your ports
Write ports as strings: "8080:80". Compose today reads 22:22 correctly, but older YAML 1.1 tools read numbers
like 22:22 as base-60 (that's 1342), and quoting costs nothing. The same goes for values like no or
on in other YAML tools; see JSON vs YAML vs XML.
Commands you'll use every day
| Command | What it does |
|---|---|
docker compose up -d | Create and start everything in the background |
docker compose ps | List the project's containers |
docker compose logs -f web | Follow one service's logs |
docker compose exec web sh | Open a shell in a running service |
docker compose pull && docker compose up -d | Update images and recreate what changed |
docker compose down | Stop and remove containers and the network (volumes are kept) |
docker compose down -v | Also delete named volumes, including database data |
docker compose config | Check the file and show exactly how Compose reads it |
How this guide was checked: every example on this page was run as written, using Docker Compose v5.7 (docker compose config, which validates a file and prints it the way Compose reads it).
Spotted a mistake? Tell us and we'll fix it.
Updated