|
| 1 | + |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +You can use compose subcommand, `docker compose [-f <arg>...] [options] [COMMAND] [ARGS...]`, to build and manage |
| 6 | +multiple services in Docker containers. |
| 7 | + |
| 8 | +### Use `-f` to specify name and path of one or more Compose files |
| 9 | +Use the `-f` flag to specify the location of a Compose configuration file. |
| 10 | + |
| 11 | +#### Specifying multiple Compose files |
| 12 | +You can supply multiple `-f` configuration files. When you supply multiple files, Compose combines them into a single |
| 13 | +configuration. Compose builds the configuration in the order you supply the files. Subsequent files override and add |
| 14 | +to their predecessors. |
| 15 | + |
| 16 | +For example, consider this command line: |
| 17 | + |
| 18 | +``` |
| 19 | +$ docker-compose -f docker-compose.yml -f docker-compose.admin.yml run backup_db |
| 20 | +``` |
| 21 | +The `docker-compose.yml` file might specify a `webapp` service. |
| 22 | + |
| 23 | +```yaml |
| 24 | +services: |
| 25 | + webapp: |
| 26 | + image: examples/web |
| 27 | + ports: |
| 28 | + - "8000:8000" |
| 29 | + volumes: |
| 30 | + - "/data" |
| 31 | +``` |
| 32 | +If the `docker-compose.admin.yml` also specifies this same service, any matching fields override the previous file. |
| 33 | +New values, add to the `webapp` service configuration. |
| 34 | + |
| 35 | +```yaml |
| 36 | +services: |
| 37 | + webapp: |
| 38 | + build: . |
| 39 | + environment: |
| 40 | + - DEBUG=1 |
| 41 | +``` |
| 42 | + |
| 43 | +When you use multiple Compose files, all paths in the files are relative to the first configuration file specified |
| 44 | +with `-f`. You can use the `--project-directory` option to override this base path. |
| 45 | + |
| 46 | +Use a `-f` with `-` (dash) as the filename to read the configuration from stdin. When stdin is used all paths in the |
| 47 | +configuration are relative to the current working directory. |
| 48 | + |
| 49 | +The `-f` flag is optional. If you don’t provide this flag on the command line, Compose traverses the working directory |
| 50 | +and its parent directories looking for a `compose.yaml` or `docker-compose.yaml` file. |
| 51 | + |
| 52 | +#### Specifying a path to a single Compose file |
| 53 | +You can use the `-f` flag to specify a path to a Compose file that is not located in the current directory, either |
| 54 | +from the command line or by setting up a `COMPOSE_FILE` environment variable in your shell or in an environment file. |
| 55 | + |
| 56 | +For an example of using the `-f` option at the command line, suppose you are running the Compose Rails sample, and |
| 57 | +have a `compose.yaml` file in a directory called `sandbox/rails`. You can use a command like `docker compose pull` to |
| 58 | +get the postgres image for the db service from anywhere by using the `-f` flag as follows: |
| 59 | +``` |
| 60 | +docker compose -f ~/sandbox/rails/compose.yaml pull db |
| 61 | +``` |
| 62 | + |
| 63 | +### Use `-p` to specify a project name |
| 64 | + |
| 65 | +Each configuration has a project name. If you supply a `-p` flag, you can specify a project name. If you don’t |
| 66 | +specify the flag, Compose uses the current directory name. |
| 67 | +Project name can also be set by `COMPOSE_PROJECT_NAME` environment variable. |
| 68 | + |
| 69 | +Most compose subcommand can be ran without a compose file, just passing |
| 70 | +project name to retrieve the relevant resources. |
| 71 | + |
| 72 | +``` |
| 73 | +$ docker compose -p my_project ps -a |
| 74 | +NAME SERVICE STATUS PORTS |
| 75 | +my_project_demo_1 demo running |
| 76 | + |
| 77 | +$ docker compose -p my_project logs |
| 78 | +demo_1 | PING localhost (127.0.0.1): 56 data bytes |
| 79 | +demo_1 | 64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.095 ms |
| 80 | +``` |
| 81 | +
|
| 82 | +### Use profiles to enable optional services |
| 83 | +
|
| 84 | +Use `--profile` to specify one or more active profiles |
| 85 | +Calling `docker compose --profile frontend up` will start the services with the profile `frontend` and services |
| 86 | +without any specified profiles. |
| 87 | +You can also enable multiple profiles, e.g. with `docker compose --profile frontend --profile debug up` the profiles `frontend` and `debug` will be enabled. |
| 88 | +
|
| 89 | +Profiles can also be set by `COMPOSE_PROFILES` environment variable. |
| 90 | +
|
| 91 | +### Set up environment variables |
| 92 | +
|
| 93 | +You can set environment variables for various docker-compose options, including the `-f`, `-p` and `--profiles` flags. |
| 94 | +
|
| 95 | +Setting the `COMPOSE_FILE` environment variable is equivalent to passing the `-f` flag, |
| 96 | +`COMPOSE_PROJECT_NAME` environment variable does the same for to the `-p` flag, |
| 97 | +and so does `COMPOSE_PROFILES` environment variable for to the `--profiles` flag. |
| 98 | +
|
| 99 | +If flags are explicitly set on command line, associated environment variable is ignored |
0 commit comments