Estimated reading time: 7 minutes
$ docker compose COMMAND
You can use compose subcommand,
docker compose [-f <arg>...] [options] [COMMAND] [ARGS...], to build and manage
multiple services in Docker containers.
-f to specify name and path of one or more Compose files
-f flag to specify the location of a Compose configuration file.
Specifying multiple Compose files
You can supply multiple
-f configuration files. When you supply multiple files, Compose combines them into a single
configuration. Compose builds the configuration in the order you supply the files. Subsequent files override and add
to their predecessors.
For example, consider this command line:
$ docker compose -f docker-compose.yml -f docker-compose.admin.yml run backup_db
docker-compose.yml file might specify a
services: webapp: image: examples/web ports: - "8000:8000" volumes: - "/data"
docker-compose.admin.yml also specifies this same service, any matching fields override the previous file.
New values, add to the
webapp service configuration.
services: webapp: build: . environment: - DEBUG=1
When you use multiple Compose files, all paths in the files are relative to the first configuration file specified
-f. You can use the
--project-directory option to override this base path.
- (dash) as the filename to read the configuration from stdin. When stdin is used all paths in the
configuration are relative to the current working directory.
-f flag is optional. If you don’t provide this flag on the command line, Compose traverses the working directory
and its parent directories looking for a
Specifying a path to a single Compose file
You can use the
-f flag to specify a path to a Compose file that is not located in the current directory, either
from the command line or by setting up a
COMPOSE_FILE environment variable in your shell or in an environment file.
For an example of using the
-f option at the command line, suppose you are running the Compose Rails sample, and
compose.yaml file in a directory called
sandbox/rails. You can use a command like
docker compose pull to
get the postgres image for the db service from anywhere by using the
-f flag as follows:
$ docker compose -f ~/sandbox/rails/compose.yaml pull db
-p to specify a project name
Each configuration has a project name. If you supply a
-p flag, you can specify a project name. If you don’t
specify the flag, Compose uses the current directory name.
Project name can also be set by
COMPOSE_PROJECT_NAME environment variable.
Most compose subcommand can be ran without a compose file, just passing project name to retrieve the relevant resources.
$ docker compose -p my_project ps -a NAME SERVICE STATUS PORTS my_project_demo_1 demo running $ docker compose -p my_project logs demo_1 | PING localhost (127.0.0.1): 56 data bytes demo_1 | 64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.095 ms
Use profiles to enable optional services
--profile to specify one or more active profiles
docker compose --profile frontend up will start the services with the profile
frontend and services
without any specified profiles.
You can also enable multiple profiles, e.g. with
docker compose --profile frontend --profile debug up the profiles
debug will be enabled.
Profiles can also be set by
COMPOSE_PROFILES environment variable.
Set up environment variables
You can set environment variables for various docker compose options, including the
COMPOSE_FILE environment variable is equivalent to passing the
COMPOSE_PROJECT_NAME environment variable does the same for to the
and so does
COMPOSE_PROFILES environment variable for to the
If flags are explicitly set on command line, associated environment variable is ignored
COMPOSE_IGNORE_ORPHANS environment variable to
true will stop docker compose from detecting orphaned
containers for the project.
||Control when to print ANSI control characters ("never"|"always"|"auto")|
||Run compose in backward compatibility mode|
||Specify an alternate environment file.|
||Compose configuration files|
||Do not print ANSI control characters (DEPRECATED)|
||Specify a profile to enable|
||Specify an alternate working directory (default: the path of the, first specified, Compose file)|
||Show more output|
||Show the Docker Compose version information|
||DEPRECATED! USE --project-directory INSTEAD. Specify an alternate working directory (default: the path of the, first specified, Compose file)|
|docker compose build||Build or rebuild services|
|docker compose convert||Converts the compose file to platform’s canonical format|
|docker compose cp||Copy files/folders between a service container and the local filesystem|
|docker compose create||Creates containers for a service.|
|docker compose down||Stop and remove containers, networks|
|docker compose events||Receive real time events from containers.|
|docker compose exec||Execute a command in a running container.|
|docker compose images||List images used by the created containers|
|docker compose kill||Force stop service containers.|
|docker compose logs||View output from containers|
|docker compose ls||List running compose projects|
|docker compose pause||Pause services|
|docker compose port||Print the public port for a port binding.|
|docker compose ps||List containers|
|docker compose pull||Pull service images|
|docker compose push||Push service images|
|docker compose restart||Restart containers|
|docker compose rm||Removes stopped service containers|
|docker compose run||Run a one-off command on a service.|
|docker compose start||Start services|
|docker compose stop||Stop services|
|docker compose top||Display the running processes|
|docker compose unpause||Unpause services|
|docker compose up||Create and start containers|
|docker compose version||Show the Docker Compose version information|