Docker Compose cheatsheet

Docker Compose commands and the compose.yaml file: up, down, logs, exec, build, profiles, environment and healthchecks.

loading Docker Compose cheatsheet
About this tool

What is a Docker Compose cheatsheet?

Docker Compose describes an application made of several containers in one YAML file, usually called compose.yaml, and controls them with a single command: docker compose up -d starts the web server, the database and the cache together, on a shared network, with the volumes you declared. This cheatsheet has two halves. The first is the command line (up, down, logs, exec, run, build, pull). The second is the file itself: short, correct snippets for the keys you write all the time.

The commands are grouped in five sections: start and stop, inspect, run commands, images and the compose.yaml file. Values you replace are written between angle brackets, such as <service> and <project>. The file snippets are YAML: the indentation is two spaces and it matters. The commands use the current docker compose plugin, written with a space. The old docker-compose with a hyphen was the previous generation of the tool and is no longer maintained.

The page is plain reference material that runs in your browser, with no account and nothing sent anywhere. For single containers without a file, see the Docker cheatsheet.

How to use the Docker Compose cheatsheet

  1. Open the page and scroll. Run the commands from the folder that holds your compose.yaml, or point to another file with -f.
  2. Press / to focus the search box, or click it. Type a task or a word of the command, such as logs, volume or healthcheck. Every word has to match. The search ignores accents and capital letters.
  3. Use the section chips under the search box to jump to a section, for example the compose.yaml file.
  4. Press the round copy button next to an item to copy it. Items from the file section copy several lines at once, with the indentation intact, so you can paste them straight into your file.
  5. Press Copy section in a section title to copy everything in it.
  6. Replace the placeholders in angle brackets, then run the command or paste the snippet. Snippets are fragments: merge them under the right services: key.
  7. Press Print for a clean sheet on paper or PDF.

A counter at the bottom shows how many items matched. If nothing matches, remove a word or try up, down, logs or volumes.

Examples

A web server in a minimal file

Item "A minimal file with a web server". Save this as compose.yaml in a folder named myapp:

services:
  web:
    image: nginx:1.27
    ports:
      - "8080:80"

Then start it and list the containers:

docker compose up -d
docker compose ps
NAME          IMAGE        COMMAND                  SERVICE   CREATED         STATUS         PORTS
myapp-web-1   nginx:1.27   "/docker-entrypoint.…"   web       9 seconds ago   Up 8 seconds   0.0.0.0:8080->80/tcp

The container name comes from the folder (the project name), the service and a number.

A database that is ready before the app starts

Items "A healthcheck" and "Wait for a healthy dependency":

services:
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
  app:
    build: .
    depends_on:
      db:
        condition: service_healthy

With only depends_on: [db], Compose waits for the container to start, not for Postgres to accept connections. The service_healthy condition waits for the healthcheck to pass.

Check the final configuration

docker compose config

This prints the file as Compose understands it, with the variables replaced and the short syntax expanded, or an error with the line of the problem. Use docker compose config --services to list only the service names. To check the YAML on its own, use the YAML viewer.

Stop, with or without the data

docker compose down
docker compose down -v

The first removes containers and networks and keeps named volumes, so your database is still there next time. The second also removes the volumes: the data is gone.

Common errors and how to fix them

  • no configuration file provided: not found: you are in a folder without compose.yaml. Move to the right folder or pass the file with docker compose -f <file> up -d.
  • docker-compose: command not found or 'compose' is not a docker command: the plugin is missing. Install Docker Compose v2 with Docker Desktop or your package manager, and use docker compose with a space.
  • yaml: line 5: mapping values are not allowed in this context: bad indentation or a missing space after a colon. Use spaces, never tabs, and keep two spaces per level. Run docker compose config to find the line.
  • Ports written without quotes: a value with two numbers below 60, such as 22:22, can be read by YAML as a base-60 number. Always quote ports, as in "8080:80".
  • depends_on does not wait for the service to be ready: it only orders the start. Add a healthcheck and condition: service_healthy.
  • docker compose down -v deletes the volumes: databases and uploads in named volumes are lost for good. Use plain docker compose down unless you want a clean start.
  • A variable is empty or a $ disappears: Compose replaces ${VAR} from your shell and from the .env file. Use ${VAR:-default} for a default, and write $$ for a literal dollar sign.
  • The warning that the attribute version is obsolete: the top-level version: key is no longer used. Remove it.
  • No command matches your search: remove a word or try up, logs or healthcheck.

Frequently asked questions

What is the difference between docker compose up and docker compose run?

up starts the services of the file and keeps them running. run starts a new one-off container for a single service, to run a command such as a database migration, and --rm removes it afterwards.

How do I rebuild an image after changing the code?

Run docker compose up -d --build. To ignore the cache entirely, run docker compose build --no-cache first.

How do I update to a newer image?

Run docker compose pull && docker compose up -d. Compose recreates only the containers whose image changed. Use fixed tags such as nginx:1.27 and not latest, so updates are a choice.

Can I have a development and a production file?

Yes. Keep common settings in compose.yaml and the differences in compose.prod.yaml, then run docker compose -f compose.yaml -f compose.prod.yaml up -d. Later files override earlier ones.

How do I turn a docker run command into a Compose file?

Use the docker run to Compose converter. Paste the command and it writes the matching services: block.

Report a problem