Docker Compose commands cheat sheet
Version and projects
-
docker compose version --shortShows only the Docker Compose version number. When the command returns an error, the Compose plugin is not installed on the server.
-
docker compose lsLists the Compose projects running on the server, with the status of each project and the path to its configuration files.
-
docker compose ls -aAlso lists projects whose containers are all stopped.
Starting and updating
-
docker compose up -dCreates 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 upStarts 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 webRebuilds 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-recreateRecreates 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-orphansBuilds and updates the services, and removes the containers of services that were deleted from the file or renamed.
-
docker compose up -d --no-deps webRecreates and starts only the web service, without starting the services it depends on.
-
docker compose up -d --pull alwaysPulls 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 120Waits 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-volumesCaution: 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 30Stops 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 startStarts stopped services again, with the same containers and the same configuration.
-
docker compose restartRestarts 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 webPauses the processes in the containers of the web service, without stopping them. The docker compose unpause web command resumes them.
-
docker compose kill webCaution: 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-orphansStops 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 -vCaution: 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 localAlso 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 webCaution: 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 -aLists all the project's containers, including stopped containers. Without -a, only running containers are shown.
-
docker compose ps --servicesShows only the service names, one per line. Convenient for scripts.
-
docker compose ps --status exitedShows only containers that have exited, for example a service that stopped right after starting because of an error.
-
docker compose logs --since 30m webShows 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 webFollows 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 webShows the log without the container name at the start of each line, for example to save a clean copy to a file.
-
docker compose topShows the processes running in each of the project's containers.
-
docker compose imagesLists the images that the project's containers were created from, with the tag of each image.
-
docker compose statsShows the CPU, memory, and network usage of the containers in real time.
-
docker compose port web 80Shows 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 shOpens 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 -laRuns a command in the running container, from the /app folder.
-
docker compose exec -T db pg_dump -U app app > backup.sqlBacks 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 shRuns 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 webBy 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.confCopies 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 webPulls 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-buildablePulls only ready-made images, and skips services that are built from a Dockerfile.
-
docker compose build --no-cache webBuilds the image of the web service from scratch, without using layers saved from a previous build.
-
docker compose build --pullBuilds the images, and first pulls a newer version of the base image, when one exists.
-
docker compose build --build-arg APP_ENV=productionPasses a variable to the build, for use by an ARG instruction in the Dockerfile.
-
docker compose pushPushes 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 configShows 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 psRuns 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 -dStarts 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 -dShows what the command would do, without changing any containers, networks, or volumes.
Checking the configuration file (config)
-
docker compose config -qChecks that the file is valid, without showing the configuration. Useful in a script, before a deployment.
-
docker compose config --servicesLists the names of all the services defined in the file, one per line.
-
docker compose config --imagesLists the names of the images that the services use.
-
docker compose config --profilesLists the names of the profiles defined in the file.
-
docker compose config --environmentShows 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.yamlSaves 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 -dAlso starts the services assigned to the profile named debug. Services without a profile always start.
-
docker compose --profile debug --profile tools up -dEnables several profiles together, with a --profile option for each one.
-
COMPOSE_PROFILES=debug,tools docker compose up -dEnables profiles through an environment variable, as a comma-separated list.
-
docker compose --profile "*" up -dStarts all the services in the file, from all the profiles.
-
docker compose up -d adminerStarts 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 -dLoads 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 configShows 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=3Starts three containers of the worker service. The value overrides the scale field in the file.
-
docker compose scale worker=3Changes 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 --watchStarts 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 watchA 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-stoppedRestart policy: the container restarts whenever it exits, until the service is stopped or deleted.
-
restart: alwaysThe container always restarts, until it is deleted.
-
restart: on-failure:5The 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_healthyIn the depends_on section: the service starts only after the healthcheck of the service it depends on passes.
-
condition: service_completed_successfullyIn 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.envPasses 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.
No matching commands.
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.
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.
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 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.