2021-03-11 16:20:30 +01:00
## Description
You can use compose subcommand, `docker compose [-f <arg>...] [options] [COMMAND] [ARGS...]` , to build and manage
multiple services in Docker containers.
### Use `-f` to specify name and path of one or more Compose files
Use the `-f` flag to specify the location of a Compose configuration file.
#### Specifying multiple Compose files
2022-03-09 13:24:09 +01:00
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
2021-03-11 16:20:30 +01:00
to their predecessors.
For example, consider this command line:
2021-09-13 17:14:32 +02:00
```console
2021-06-25 13:41:58 +02:00
$ docker compose -f docker-compose.yml -f docker-compose.admin.yml run backup_db
2021-03-11 16:20:30 +01:00
```
2021-09-13 17:14:32 +02:00
2021-03-11 16:20:30 +01:00
The `docker-compose.yml` file might specify a `webapp` service.
```yaml
services:
webapp:
image: examples/web
ports:
- "8000:8000"
volumes:
- "/data"
```
2022-03-09 13:24:09 +01:00
If the `docker-compose.admin.yml` also specifies this same service, any matching fields override the previous file.
2021-03-11 16:20:30 +01:00
New values, add to the `webapp` service configuration.
```yaml
services:
webapp:
build: .
environment:
- DEBUG=1
```
2022-03-09 13:24:09 +01:00
When you use multiple Compose files, all paths in the files are relative to the first configuration file specified
2021-03-11 16:20:30 +01:00
with `-f` . You can use the `--project-directory` option to override this base path.
2022-03-09 13:24:09 +01:00
Use a `-f` with `-` (dash) as the filename to read the configuration from stdin. When stdin is used all paths in the
2021-03-11 16:20:30 +01:00
configuration are relative to the current working directory.
2022-03-09 13:24:09 +01:00
The `-f` flag is optional. If you don’ t provide this flag on the command line, Compose traverses the working directory
2021-03-11 16:20:30 +01:00
and its parent directories looking for a `compose.yaml` or `docker-compose.yaml` file.
#### Specifying a path to a single Compose file
2022-03-09 13:24:09 +01:00
You can use the `-f` flag to specify a path to a Compose file that is not located in the current directory, either
2021-03-11 16:20:30 +01:00
from the command line or by setting up a `COMPOSE_FILE` environment variable in your shell or in an environment file.
2022-03-09 13:24:09 +01:00
For an example of using the `-f` option at the command line, suppose you are running the Compose Rails sample, and
have a `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:
2021-09-13 17:14:32 +02:00
```console
$ docker compose -f ~/sandbox/rails/compose.yaml pull db
2021-03-11 16:20:30 +01:00
```
### Use `-p` to specify a project name
2022-03-09 13:24:09 +01:00
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.
2021-03-11 16:20:30 +01:00
Project name can also be set by `COMPOSE_PROJECT_NAME` environment variable.
2022-03-09 13:24:09 +01:00
Most compose subcommand can be ran without a compose file, just passing
2021-03-11 16:20:30 +01:00
project name to retrieve the relevant resources.
2021-09-13 17:14:32 +02:00
```console
2021-03-11 16:20:30 +01:00
$ docker compose -p my_project ps -a
NAME SERVICE STATUS PORTS
2022-03-09 13:24:09 +01:00
my_project_demo_1 demo running
2021-03-11 16:20:30 +01:00
$ 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
Use `--profile` to specify one or more active profiles
2022-03-09 13:24:09 +01:00
Calling `docker compose --profile frontend up` will start the services with the profile `frontend` and services
without any specified profiles.
2021-03-11 16:20:30 +01:00
You can also enable multiple profiles, e.g. with `docker compose --profile frontend --profile debug up` the profiles `frontend` and `debug` will be enabled.
Profiles can also be set by `COMPOSE_PROFILES` environment variable.
### Set up environment variables
2021-06-25 13:41:58 +02:00
You can set environment variables for various docker compose options, including the `-f` , `-p` and `--profiles` flags.
2021-03-11 16:20:30 +01:00
Setting the `COMPOSE_FILE` environment variable is equivalent to passing the `-f` flag,
`COMPOSE_PROJECT_NAME` environment variable does the same for to the `-p` flag,
and so does `COMPOSE_PROFILES` environment variable for to the `--profiles` flag.
If flags are explicitly set on command line, associated environment variable is ignored
2022-03-03 23:21:27 +01:00
2022-03-09 13:24:09 +01:00
Setting the `COMPOSE_IGNORE_ORPHANS` environment variable to `true` will stop docker compose from detecting orphaned
2022-03-03 23:21:27 +01:00
containers for the project.