Docker Compose commands cheat sheet

This cheat sheet brings together the Docker Compose commands for managing a multi-service application on a server: starting and updating, stopping and removing, logs, running commands in containers, override files, profiles, and environment variables, along with common settings in the compose.yaml file. You can search for a command, filter by topic, and copy it with one click.

Version and projects

  • docker compose version --short

    Shows only the Docker Compose version number. When the command returns an error, the Compose plugin is not installed on the server.

  • docker compose ls

    Lists the Compose projects running on the server, with the status of each project and the path to its configuration files.

  • docker compose ls -a

    Also lists projects whose containers are all stopped.

Starting and updating

  • docker compose up -d

    Creates and starts in the background all the services defined in the compose.yaml file in the current folder. When the configuration or the image of a service has changed, the command recreates only that service's containers, and the volumes are kept.

  • docker compose up

    Starts the services in the foreground and shows the logs of all of them in the terminal. Useful for a first test of a new file. Pressing Ctrl+C stops the services.

  • docker compose up -d --build web

    Rebuilds the image of the web service and starts the service with the new image, for example after a change to the code or to the Dockerfile.

  • docker compose up -d --force-recreate

    Recreates all the containers, even when the configuration and the image haven't changed. The data in volumes is kept.

  • docker compose up -d --build --remove-orphans

    Builds and updates the services, and removes the containers of services that were deleted from the file or renamed.

  • docker compose up -d --no-deps web

    Recreates and starts only the web service, without starting the services it depends on.

  • docker compose up -d --pull always

    Pulls the latest version of every image from the registry before starting, even when the image already exists on the server.

  • docker compose up -d --wait --wait-timeout 120

    Waits until the services are running, and for services with a healthcheck, until the check passes, for up to 120 seconds. Useful in a deployment script, to continue only after the application is up.

  • docker compose up -d --renew-anon-volumes

    Caution: recreates the anonymous volumes instead of carrying over the data from the previous containers, so the data in them starts from scratch. Named volumes are not affected.

Stopping, restarting, and removing

  • docker compose stop -t 30

    Stops the project's containers without deleting them, and waits up to 30 seconds for each container to shut down cleanly before stopping it by force.

  • docker compose start

    Starts stopped services again, with the same containers and the same configuration.

  • docker compose restart

    Restarts the existing containers. The command does not apply changes made to the compose.yaml file or to the environment variables. To apply them, run docker compose up -d.

  • docker compose pause web

    Pauses the processes in the containers of the web service, without stopping them. The docker compose unpause web command resumes them.

  • docker compose kill web

    Caution: stops the containers of the web service immediately, without waiting for a clean shutdown. Suitable for a container that doesn't respond to the stop command.

  • docker compose down --remove-orphans

    Stops and deletes the project's containers and networks, including containers of services that were already removed from the file. Volumes are kept, and volumes and networks defined as external are never deleted.

  • docker compose down -v

    Caution: also deletes the named volumes declared in the volumes section of the file and the anonymous volumes, along with all the data in them, for example the database files. Back up the data before you run it.

  • docker compose down --rmi local

    Also deletes the images of services that don't have a custom tag, usually images that were built locally. The value all deletes all the images that the services use.

  • docker compose rm -s -v web

    Caution: stops the containers of the web service and deletes them, along with their anonymous volumes, after asking for confirmation. The -f option deletes them without asking for confirmation.

Status, logs, and resources

  • docker compose ps -a

    Lists all the project's containers, including stopped containers. Without -a, only running containers are shown.

  • docker compose ps --services

    Shows only the service names, one per line. Convenient for scripts.

  • docker compose ps --status exited

    Shows only containers that have exited, for example a service that stopped right after starting because of an error.

  • docker compose logs --since 30m web

    Shows the log of the web service from the last 30 minutes. You can also specify a full date and time.

  • docker compose logs -f -n 50 -t web

    Follows the log of the web service in real time, starting from the last 50 lines, and adds a timestamp to each line.

  • docker compose logs --no-log-prefix web

    Shows the log without the container name at the start of each line, for example to save a clean copy to a file.

  • docker compose top

    Shows the processes running in each of the project's containers.

  • docker compose images

    Lists the images that the project's containers were created from, with the tag of each image.

  • docker compose stats

    Shows the CPU, memory, and network usage of the containers in real time.

  • docker compose port web 80

    Shows which address and port on the server port 80 of the web service is mapped to.

Running commands and copying files

  • docker compose exec -u root web sh

    Opens a shell as the root user in the running container of the web service, for example to install a troubleshooting tool. Without -u, the command runs as the user defined in the image.

  • docker compose exec -w /app web ls -la

    Runs a command in the running container, from the /app folder.

  • docker compose exec -T db pg_dump -U app app > backup.sql

    Backs up a PostgreSQL database from the db service to a file on the server. The -T option turns off terminal allocation, so the output is saved to the file without extra characters.

  • docker compose run --rm --no-deps web sh

    Runs a separate, temporary container of the web service and opens a shell in it, without starting the services it depends on. The --rm option deletes the container when it exits.

  • docker compose run --rm --service-ports web

    By default, the run command does not map the ports defined for the service. The --service-ports option maps them to the server, as the up command does.

  • docker compose cp web:/etc/nginx/nginx.conf ./nginx.conf

    Copies a file from the container of the web service to the current folder on the server.

  • docker compose cp ./index.html web:/usr/share/nginx/html/

    Copies a file from the server to the container of the web service. A change like this is lost when the container is recreated, so for a permanent change, use a volume or a new image.

Images and builds

  • docker compose pull web

    Pulls the latest version of the web service image. For the new version to take effect, run docker compose up -d afterward.

  • docker compose pull --ignore-buildable

    Pulls only ready-made images, and skips services that are built from a Dockerfile.

  • docker compose build --no-cache web

    Builds the image of the web service from scratch, without using layers saved from a previous build.

  • docker compose build --pull

    Builds the images, and first pulls a newer version of the base image, when one exists.

  • docker compose build --build-arg APP_ENV=production

    Passes a variable to the build, for use by an ARG instruction in the Dockerfile.

  • docker compose push

    Pushes the images of the services to the registry, by the names in the image field.

Files, overrides, and project name

  • docker compose -f compose.yaml -f compose.prod.yaml config

    Shows the configuration after merging the two files. The second file overrides the first in matching fields and adds the new fields. When the configuration is correct, run the same command with up -d instead of config.

  • docker compose -f /srv/app/compose.yaml ps

    Runs a command on a project located in another folder. Relative paths in the file are resolved from the folder of the first file specified in the command.

  • docker compose -p shop up -d

    Starts the project under the name shop instead of the folder name, for example to run two copies of the same file on the same server. The name can contain only lowercase letters, digits, hyphens, and underscores.

  • docker compose --dry-run up -d

    Shows what the command would do, without changing any containers, networks, or volumes.

Checking the configuration file (config)

  • docker compose config -q

    Checks that the file is valid, without showing the configuration. Useful in a script, before a deployment.

  • docker compose config --services

    Lists the names of all the services defined in the file, one per line.

  • docker compose config --images

    Lists the names of the images that the services use.

  • docker compose config --profiles

    Lists the names of the profiles defined in the file.

  • docker compose config --environment

    Shows the variables and the values that Compose uses to substitute variables in the file, from the environment and from the .env file.

  • docker compose config -o resolved.yaml

    Saves the full configuration to a file, after merging the files and substituting the variables. The file may include passwords from the .env file, so don't share it.

Profiles and environment variables

  • docker compose --profile debug up -d

    Also starts the services assigned to the profile named debug. Services without a profile always start.

  • docker compose --profile debug --profile tools up -d

    Enables several profiles together, with a --profile option for each one.

  • COMPOSE_PROFILES=debug,tools docker compose up -d

    Enables profiles through an environment variable, as a comma-separated list.

  • docker compose --profile "*" up -d

    Starts all the services in the file, from all the profiles.

  • docker compose up -d adminer

    Starts a service that is assigned to a profile, even when the profile is not enabled, because the service is named in the command. The services it depends on are started too.

  • docker compose --env-file .env.prod up -d

    Loads the variables from the .env.prod file instead of the .env file, to substitute the values in the compose.yaml file. The path is resolved from the current folder.

  • docker compose --env-file .env.prod config

    Shows the configuration with the values from the .env.prod file, to check it before starting the services.

  • ${APP_PORT:-8080}

    In the compose.yaml file: replaced with the value of the APP_PORT variable from the environment or from the .env file, and with 8080 when the variable is missing or empty.

  • ${DB_PASSWORD:?DB_PASSWORD is missing}

    In the compose.yaml file: stops Compose with an error message when the variable is missing or empty, so a service doesn't start without a password.

Scaling and watching for changes

  • docker compose up -d --scale worker=3

    Starts three containers of the worker service. The value overrides the scale field in the file.

  • docker compose scale worker=3

    Changes the number of containers of a service that is already running. The --no-deps option prevents starting the services it depends on.

  • docker compose up --watch

    Starts the services and updates them automatically when the source files change, according to the rules in the develop.watch section of each service. Intended for development environments.

  • docker compose watch

    A separate command for watch mode, with the same rules from the develop.watch section. The --no-up option starts watching without building and starting the services.

Common settings in the compose.yaml file

  • restart: unless-stopped

    Restart policy: the container restarts whenever it exits, until the service is stopped or deleted.

  • restart: always

    The container always restarts, until it is deleted.

  • restart: on-failure:5

    The container restarts only when it exits with an error code, for up to five attempts. Without the number, the number of attempts is unlimited.

  • restart: "no"

    The default: the container never restarts.

  • - "127.0.0.1:8080:80"

    In the ports section: publishes port 80 of the container on port 8080 of the server, on the local address only, for example behind a reverse proxy. Without an address, the port is opened on all the network interfaces of the server and may be exposed to the internet.

  • test: ["CMD-SHELL", "pg_isready -U app -d app"]

    In the healthcheck section: the command that checks whether the service is healthy. Alongside it, you set interval, timeout, retries, and start_period, and a full example appears further down this page.

  • condition: service_healthy

    In the depends_on section: the service starts only after the healthcheck of the service it depends on passes.

  • condition: service_completed_successfully

    In the depends_on section: the service starts only after the service it depends on has completed successfully, for example a service that runs a database schema migration.

  • env_file: ./app.env

    Passes the variables in the app.env file to the container. A relative path is resolved from the folder of the Compose file, and values in the environment field override values from the file.

  • profiles: [debug]

    Assigns the service to the profile named debug, so it starts only with --profile debug or when you name it in the command.

  • version: "3.8"

    A field from old Compose files. Current versions of Compose ignore it, and you can remove it from the file.

docker compose and docker-compose

The first version of Compose was written in Python and was run with the docker-compose command, with a hyphen. Current versions are run with the docker compose command, with a space, as a Docker plugin. According to the Docker documentation, the first version is no longer supported, and the current versions are functionally identical. That is why all the commands on this page are written in the docker compose form, and general Docker commands are on the Docker commands page.

Which files Compose reads

Without the -f option, the command looks for the compose.yaml file, or the older name docker-compose.yaml, in the current folder and in the folders above it. The compose.override.yaml file is also read automatically along with it, when it exists. When you specify files with -f, only those files are read, in order, and each file overrides the previous one in matching fields.

The .env file next to compose.yaml is used to substitute variables such as ${APP_PORT} inside the configuration file, while the env_file field passes variables to the container itself. A variable set in the shell overrides the value in the .env file.

Example: a website and a database with a healthcheck

In the following example, the web service starts only after the database healthcheck passes, and Compose restarts it after the db service is updated. The password is taken from the .env file, and Compose stops with an error message when it is missing. The website port is open only on the local address of the server.

YAML
services:
  web:
    image: nginx:alpine
    ports:
      - "127.0.0.1:8080:80"
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
        restart: true
  db:
    image: postgres:18
    environment:
      POSTGRES_USER: app
      POSTGRES_DB: app
      POSTGRES_PASSWORD: ${DB_PASSWORD:?DB_PASSWORD is missing}
    volumes:
      - db-data:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d app"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    restart: unless-stopped
volumes:
  db-data:

The database data is stored in a volume named db-data, so it is kept even after docker compose down. The docker compose down -v command deletes it too, so run it only after a backup.

Did you know?

On a Docker VPS, Docker and Docker Compose are installed and configured when the server is set up, and from there you start and manage your projects with docker compose commands, with guaranteed resources for all your services.

Docker VPS plans

Docker Compose commands FAQ

What is the difference between docker compose and docker-compose?

The docker-compose command, with a hyphen, belongs to the first version of Compose, which was written in Python and installed as a separate tool. Current versions are run with the docker compose command, with a space, and work as a Docker plugin. According to the Docker documentation, the first version is no longer supported, so on a new server you work with docker compose. The command names themselves, such as up, down, and logs, have stayed the same.

How do I update a service to a new version of its image?

Run docker compose pull to pull the new versions, and then run docker compose up -d. The command recreates only the containers whose image or configuration has changed, and the data in volumes is kept. To update a service built from a Dockerfile, run docker compose up -d --build.

How do I apply a change I made to the compose.yaml file?

Run docker compose up -d. The command compares the configuration in the file with the existing containers, and recreates only the services that have changed. The docker compose restart command restarts the existing containers as they are, so it doesn't apply changes to the file or to the environment variables. Before you start the services, you can check the file with the docker compose config command.

How do I make the database start before the application?

Add a healthcheck to the database service, and in the application service, set depends_on with condition: service_healthy. This way, Compose starts the application only after the database healthcheck passes. A full example of a compose.yaml file with a website and a database appears on this page, after the list of commands.

Can I keep different settings for development and for the production server?

Yes. The shared settings are kept in the compose.yaml file, and the changes for a specific environment are written in an additional file. The compose.override.yaml file is loaded automatically together with compose.yaml, so it is convenient for development settings. On the production server, you specify the files explicitly, for example docker compose -f compose.yaml -f compose.prod.yaml up -d, and this way the compose.override.yaml file is not loaded. You can pass values such as passwords and ports in the .env file.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement