2022-03-09 12:57:07 +01:00
# docker compose
2021-03-11 16:20:30 +01:00
2022-03-09 12:57:07 +01:00
<!-- - MARKER_GEN_START -->
2023-05-11 18:33:45 +02:00
Define and run multi-container applications with Docker.
2022-03-09 12:57:07 +01:00
### Subcommands
2022-12-29 14:32:16 +01:00
| Name | Description |
|:--------------------------------|:------------------------------------------------------------------------|
2023-01-11 11:52:19 +01:00
| [`alpha` ](compose_alpha.md ) | Experimental commands |
2022-12-29 14:32:16 +01:00
| [`build` ](compose_build.md ) | Build or rebuild services |
2023-01-27 10:04:52 +01:00
| [`config` ](compose_config.md ) | Parse, resolve and render compose file in canonical format |
2022-12-29 14:32:16 +01:00
| [`cp` ](compose_cp.md ) | Copy files/folders between a service container and the local filesystem |
| [`create` ](compose_create.md ) | Creates containers for a service. |
| [`down` ](compose_down.md ) | Stop and remove containers, networks |
| [`events` ](compose_events.md ) | Receive real time events from containers. |
| [`exec` ](compose_exec.md ) | Execute a command in a running container. |
| [`images` ](compose_images.md ) | List images used by the created containers |
| [`kill` ](compose_kill.md ) | Force stop service containers. |
| [`logs` ](compose_logs.md ) | View output from containers |
| [`ls` ](compose_ls.md ) | List running compose projects |
| [`pause` ](compose_pause.md ) | Pause services |
| [`port` ](compose_port.md ) | Print the public port for a port binding. |
| [`ps` ](compose_ps.md ) | List containers |
| [`pull` ](compose_pull.md ) | Pull service images |
| [`push` ](compose_push.md ) | Push service images |
| [`restart` ](compose_restart.md ) | Restart service containers |
| [`rm` ](compose_rm.md ) | Removes stopped service containers |
| [`run` ](compose_run.md ) | Run a one-off command on a service. |
| [`start` ](compose_start.md ) | Start services |
| [`stop` ](compose_stop.md ) | Stop services |
| [`top` ](compose_top.md ) | Display the running processes |
| [`unpause` ](compose_unpause.md ) | Unpause services |
| [`up` ](compose_up.md ) | Create and start containers |
| [`version` ](compose_version.md ) | Show the Docker Compose version information |
2022-03-09 12:57:07 +01:00
### Options
2022-12-29 14:32:16 +01:00
| Name | Type | Default | Description |
|:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------|
| `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") |
| `--compatibility` | | | Run compose in backward compatibility mode |
2023-05-05 10:40:18 +02:00
| `--dry-run` | | | Execute command in dry run mode |
2023-02-15 14:30:09 +01:00
| `--env-file` | `stringArray` | | Specify an alternate environment file. |
2022-12-29 14:32:16 +01:00
| `-f` , `--file` | `stringArray` | | Compose configuration files |
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |
| `--profile` | `stringArray` | | Specify a profile to enable |
| `--project-directory` | `string` | | Specify an alternate working directory< br > (default: the path of the, first specified, Compose file) |
| `-p` , `--project-name` | `string` | | Project name |
2022-03-09 12:57:07 +01:00
<!-- - MARKER_GEN_END -->
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.
2022-11-29 16:52:22 +01:00
### Use `-f` to specify the name and path of one or more Compose files
2021-03-11 16:20:30 +01:00
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
2023-03-21 17:41:50 +01:00
Each configuration has a project name. Compose sets the project name using
the following mechanisms, in order of precedence:
- The `-p` command line flag
- The `COMPOSE_PROJECT_NAME` environment variable
- The top level `name:` variable from the config file (or the last `name:`
from a series of config files specified using `-f` )
- The `basename` of the project directory containing the config file (or
containing the first config file specified using `-f` )
- The `basename` of the current directory if no config file is specified
Project names must contain only lowercase letters, decimal digits, dashes,
and underscores, and must begin with a lowercase letter or decimal digit. If
the `basename` of the project directory or current directory violates this
constraint, you must use one of the other mechanisms.
2021-03-11 16:20:30 +01:00
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.
2023-01-24 15:53:46 +01:00
### Configuring parallelism
Use `--parallel` to specify the maximum level of parallelism for concurrent engine calls.
Calling `docker compose --parallel 1 pull` will pull the pullable images defined in the Compose file
one at a time. This can also be used to control build concurrency.
Parallelism can also be set by the `COMPOSE_PARALLEL_LIMIT` environment variable.
2021-03-11 16:20:30 +01:00
### 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,
2022-11-29 16:52:22 +01:00
`COMPOSE_PROJECT_NAME` environment variable does the same as the `-p` flag,
2023-01-24 15:53:46 +01:00
`COMPOSE_PROFILES` environment variable is equivalent to the `--profiles` flag
and `COMPOSE_PARALLEL_LIMIT` does the same as the `--parallel` flag.
2021-03-11 16:20:30 +01:00
2022-11-29 16:52:22 +01:00
If flags are explicitly set on the command line, the 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.
2023-05-05 10:40:18 +02:00
### Use Dry Run mode to test your command
Use `--dry-run` flag to test a command without changing your application stack state.
2023-05-05 12:43:27 +02:00
Dry Run mode shows you all the steps Compose applies when executing a command, for example:
2023-05-05 10:40:18 +02:00
```console
$ docker compose --dry-run up --build -d
[+] Pulling 1/1
✔ DRY-RUN MODE - db Pulled 0.9s
[+] Running 10/8
✔ DRY-RUN MODE - build service backend 0.0s
✔ DRY-RUN MODE - ==> ==> writing image dryRun-754a08ddf8bcb1cf22f310f09206dd783d42f7dd 0.0s
✔ DRY-RUN MODE - ==> ==> naming to nginx-golang-mysql-backend 0.0s
✔ DRY-RUN MODE - Network nginx-golang-mysql_default Created 0.0s
✔ DRY-RUN MODE - Container nginx-golang-mysql-db-1 Created 0.0s
✔ DRY-RUN MODE - Container nginx-golang-mysql-backend-1 Created 0.0s
✔ DRY-RUN MODE - Container nginx-golang-mysql-proxy-1 Created 0.0s
✔ DRY-RUN MODE - Container nginx-golang-mysql-db-1 Healthy 0.5s
✔ DRY-RUN MODE - Container nginx-golang-mysql-backend-1 Started 0.0s
✔ DRY-RUN MODE - Container nginx-golang-mysql-proxy-1 Started Started
```
2023-05-05 12:43:27 +02:00
From the example above, you can see that the first step is to pull the image defined by `db` service, then build the `backend` service.
Next, the containers are created. The `db` service is started, and the `backend` and `proxy` wait until the `db` service is healthy before starting.
2023-05-05 10:40:18 +02:00
2023-05-05 12:43:27 +02:00
Dry Run mode does not currently work with all commands. In particular, you cannot use Dry Run mode with a command that doesn't change the state of a Compose stack
2023-05-05 10:40:18 +02:00
such as `ps` , `ls` , `logs` for example.
Here the list of commands supporting `--dry-run` flag:
* build
* cp
* create
* down
* exec
* kill
* pause
* pull
* push
* remove
* restart
* run
* start
* stop
* unpause
* up