curly{bits}

curlybits / Guides / docker run vs Docker Compose: when and how to switch

docker run vs Docker Compose: when and how to switch

One container with three flags is fine as a docker run command. Once you have two containers, a volume and a password, a compose.yaml is easier to read, share and repeat.

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 -d and docker compose down to 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 runcompose.yaml
--name appcontainer_name: app (usually leave it out, so you can scale)
-p 3000:3000ports: ["3000:3000"]
-v ./src:/appvolumes: ["./src:/app"]
-v data:/var/lib/datathe same, plus data: under a top-level volumes:
-e NODE_ENV=productionenvironment: {NODE_ENV: production}
--env-file .env.appenv_file: [.env.app]
--restart unless-stoppedrestart: unless-stopped
--network backendnetworks: [backend], declared at the top level
-u 1000:1000user: "1000:1000"
-w /appworking_dir: /app
--memory 512m --cpus 1.5mem_limit: 512m, cpus: 1.5
--cap-add NET_ADMINcap_add: [NET_ADMIN]
--add-host host.docker.internal:host-gatewayextra_hosts: ["host.docker.internal:host-gateway"]
-itstdin_open: true and tty: true
arguments after the imagecommand: ["npm", "start"]
-d, --rmno 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

CommandWhat it does
docker compose up -dCreate and start everything in the background
docker compose psList the project's containers
docker compose logs -f webFollow one service's logs
docker compose exec web shOpen a shell in a running service
docker compose pull && docker compose up -dUpdate images and recreate what changed
docker compose downStop and remove containers and the network (volumes are kept)
docker compose down -vAlso delete named volumes, including database data
docker compose configCheck 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