2015-06-07 22:59:58 +02:00
<!-- [metadata]>
+++
2015-10-09 17:25:16 +02:00
title = "Compose file reference"
description = "Compose file reference"
keywords = ["fig, composition, compose, docker"]
2015-10-13 21:23:27 +02:00
aliases = ["/compose/yml"]
2015-06-07 22:59:58 +02:00
[menu.main]
parent="smn_compose_ref"
+++
<![end-metadata]-->
2014-01-27 16:03:21 +01:00
2015-10-09 17:25:16 +02:00
# Compose file reference
The compose file is a [YAML ](http://yaml.org/ ) file where all the top level
keys are the name of a service, and the values are the service definition.
The default path for a compose file is `./docker-compose.yml` .
2014-01-27 16:03:21 +01:00
2015-01-19 06:46:23 +01:00
Each service defined in `docker-compose.yml` must specify exactly one of
`image` or `build` . Other keys are optional, and are analogous to their
`docker run` command-line counterparts.
2014-01-27 16:03:21 +01:00
2015-01-19 06:46:23 +01:00
As with `docker run` , options specified in the Dockerfile (e.g., `CMD` ,
`EXPOSE` , `VOLUME` , `ENV` ) are respected by default - you don't need to
specify them again in `docker-compose.yml` .
2014-01-27 16:03:21 +01:00
2015-10-27 21:29:36 +01:00
## Service configuration reference
This section contains a list of all configuration options supported by a service
definition.
2014-07-12 18:45:11 +02:00
### build
2015-05-09 01:14:32 +02:00
Path to a directory containing a Dockerfile. When the value supplied is a
relative path, it is interpreted as relative to the location of the yml file
2015-03-29 22:06:35 +02:00
itself. This directory is also the build context that is sent to the Docker daemon.
2015-02-25 09:43:33 +01:00
Compose will build and tag it with a generated name, and use that image thereafter.
2014-01-27 16:03:21 +01:00
2015-06-19 11:28:09 +02:00
build: /path/to/build/dir
2014-07-12 18:45:11 +02:00
2015-08-26 23:03:45 +02:00
Using `build` together with `image` is not allowed. Attempting to do so results in an error.
2015-07-31 08:35:13 +02:00
### cap_add, cap_drop
2015-03-06 21:33:56 +01:00
2015-07-31 08:35:13 +02:00
Add or drop container capabilities.
See `man 7 capabilities` for a full list.
2015-03-06 21:33:56 +01:00
2015-07-31 08:35:13 +02:00
cap_add:
- ALL
2015-03-06 21:33:56 +01:00
2015-07-31 08:35:13 +02:00
cap_drop:
- NET_ADMIN
- SYS_ADMIN
2015-03-06 21:33:56 +01:00
2014-07-12 18:45:11 +02:00
### command
2014-01-27 16:03:21 +01:00
2014-07-12 18:45:11 +02:00
Override the default command.
2015-06-19 11:28:09 +02:00
command: bundle exec thin -p 3000
2014-07-12 18:45:11 +02:00
2015-09-30 09:25:26 +02:00
### cgroup_parent
Specify an optional parent cgroup for the container.
cgroup_parent: m-executor-abcd
2015-07-31 08:35:13 +02:00
### container_name
2015-01-15 21:58:17 +01:00
2015-07-31 08:35:13 +02:00
Specify a custom container name, rather than a generated default name.
2015-01-15 21:58:17 +01:00
2015-07-31 08:35:13 +02:00
container_name: my-web-container
2015-01-15 21:58:17 +01:00
2015-07-31 08:35:13 +02:00
Because Docker container names must be unique, you cannot scale a service
beyond 1 container if you have specified a custom name. Attempting to do so
results in an error.
2015-01-15 21:58:17 +01:00
2015-07-31 08:35:13 +02:00
### devices
2015-01-15 21:58:17 +01:00
2015-07-31 08:35:13 +02:00
List of device mappings. Uses the same format as the `--device` docker
client create option.
2014-07-12 18:45:11 +02:00
2015-07-31 08:35:13 +02:00
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
2014-01-27 16:03:21 +01:00
2015-07-31 08:35:13 +02:00
### dns
2015-08-07 17:30:00 +02:00
2015-07-31 08:35:13 +02:00
Custom DNS servers. Can be a single value or a list.
2015-08-07 17:30:00 +02:00
2015-07-31 08:35:13 +02:00
dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
2015-08-07 17:30:00 +02:00
2015-07-31 08:35:13 +02:00
### dns_search
2014-07-12 18:45:11 +02:00
2015-07-31 08:35:13 +02:00
Custom DNS search domains. Can be a single value or a list.
2014-07-12 18:45:11 +02:00
2015-07-31 08:35:13 +02:00
dns_search: example.com
dns_search:
- dc1.example.com
- dc2.example.com
2015-08-07 17:30:00 +02:00
2015-07-31 08:35:13 +02:00
### dockerfile
2014-07-12 18:45:11 +02:00
2015-07-31 08:35:13 +02:00
Alternate Dockerfile.
2014-07-12 18:45:11 +02:00
2015-10-28 17:48:35 +01:00
Compose will use an alternate file to build with. A build path must also be
specified using the `build` key.
2014-01-27 16:03:21 +01:00
2015-10-28 17:48:35 +01:00
build: /path/to/build/dir
2015-07-31 08:35:13 +02:00
dockerfile: Dockerfile-alternate
2014-03-04 18:59:42 +01:00
2015-10-15 15:13:27 +02:00
Using `dockerfile` together with `image` is not allowed. Attempting to do so results in an error.
2015-07-31 08:35:13 +02:00
### env_file
2014-07-12 18:45:11 +02:00
2015-07-31 08:35:13 +02:00
Add environment variables from a file. Can be a single value or a list.
2014-07-24 23:16:49 +02:00
2015-07-31 08:35:13 +02:00
If you have specified a Compose file with `docker-compose -f FILE` , paths in
`env_file` are relative to the directory that file is in.
2014-07-12 18:45:11 +02:00
2015-07-31 08:35:13 +02:00
Environment variables specified in `environment` override these values.
2015-06-03 13:21:29 +02:00
2015-07-31 08:35:13 +02:00
env_file: .env
2015-06-03 13:21:29 +02:00
2015-07-31 08:35:13 +02:00
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
2014-01-27 16:03:21 +01:00
2015-07-31 08:35:13 +02:00
Compose expects each line in an env file to be in `VAR=VAL` format. Lines
beginning with `#` (i.e. comments) are ignored, as are blank lines.
2014-07-12 18:45:11 +02:00
2015-07-31 08:35:13 +02:00
# Set Rails/Rack environment
RACK_ENV=development
2014-07-12 18:45:11 +02:00
### environment
2015-09-07 17:45:58 +02:00
Add environment variables. You can use either an array or a dictionary. Any
boolean values; true, false, yes no, need to be enclosed in quotes to ensure
they are not converted to True or False by the YML parser.
2014-07-12 18:55:11 +02:00
2015-01-19 06:46:23 +01:00
Environment variables with only a key are resolved to their values on the
machine Compose is running on, which can be helpful for secret or host-specific values.
2014-07-11 20:11:54 +02:00
2015-06-19 11:28:09 +02:00
environment:
RACK_ENV: development
2015-09-07 17:45:58 +02:00
SHOW: 'true'
2015-06-19 11:28:09 +02:00
SESSION_SECRET:
2014-07-12 18:55:11 +02:00
2015-06-19 11:28:09 +02:00
environment:
- RACK_ENV=development
2015-09-07 17:45:58 +02:00
- SHOW=true
2015-06-19 11:28:09 +02:00
- SESSION_SECRET
2014-01-27 16:03:21 +01:00
2015-07-31 08:35:13 +02:00
### expose
2014-09-12 05:57:23 +02:00
2015-07-31 08:35:13 +02:00
Expose ports without publishing them to the host machine - they'll only be
accessible to linked services. Only the internal port can be specified.
2015-03-24 15:22:11 +01:00
2015-07-31 08:35:13 +02:00
expose:
- "3000"
- "8000"
2014-09-12 05:57:23 +02:00
2015-03-18 21:51:27 +01:00
### extends
Extend another service, in the current file or another, optionally overriding
configuration.
2015-10-09 18:34:55 +02:00
You can use `extends` on any service together with other configuration keys.
2015-10-09 18:56:59 +02:00
The `extends` value must be a dictionary defined with a required `service`
and an optional `file` key.
2015-10-09 18:34:55 +02:00
extends:
file: common.yml
service: webapp
2015-10-09 18:56:59 +02:00
The `service` the name of the service being extended, for example
`web` or `database` . The `file` is the location of a Compose configuration
file defining that service.
2015-10-09 18:34:55 +02:00
2015-10-09 18:56:59 +02:00
If you omit the `file` Compose looks for the service configuration in the
current file. The `file` value can be an absolute or relative path. If you
specify a relative path, Compose treats it as relative to the location of the
current file.
2015-10-09 18:34:55 +02:00
You can extend a service that itself extends another. You can extend
indefinitely. Compose does not support circular references and `docker-compose`
returns an error if it encounters one.
For more on `extends` , see the
[the extends documentation ](extends.md#extending-services ).
2015-03-18 21:51:27 +01:00
2015-07-31 08:35:13 +02:00
### external_links
Link to containers started outside this `docker-compose.yml` or even outside
of Compose, especially for containers that provide shared or common services.
`external_links` follow semantics similar to `links` when specifying both the
container name and the link alias (`CONTAINER:ALIAS`).
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
### extra_hosts
Add hostname mappings. Use the same values as the docker client `--add-host` parameter.
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
An entry with the ip address and hostname will be created in `/etc/hosts` inside containers for this service, e.g:
162.242.195.82 somehost
50.31.209.229 otherhost
### image
Tag or partial image ID. Can be local or remote - Compose will attempt to
pull if it doesn't exist locally.
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
2015-03-20 00:10:27 +01:00
### labels
Add metadata to containers using [Docker labels ](http://docs.docker.com/userguide/labels-custom-metadata/ ). You can use either an array or a dictionary.
It's recommended that you use reverse-DNS notation to prevent your labels from conflicting with those used by other software.
2015-06-19 11:28:09 +02:00
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
2015-03-20 00:10:27 +01:00
2015-06-19 11:28:09 +02:00
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
2015-03-20 00:10:27 +01:00
2015-07-31 08:35:13 +02:00
### links
2015-07-16 13:51:15 +02:00
2015-07-31 08:35:13 +02:00
Link to containers in another service. Either specify both the service name and
the link alias (`SERVICE:ALIAS`), or just the service name (which will also be
used for the alias).
2015-07-16 13:51:15 +02:00
2015-07-31 08:35:13 +02:00
links:
- db
- db:database
- redis
2015-07-16 13:51:15 +02:00
2015-07-31 08:35:13 +02:00
An entry with the alias' name will be created in `/etc/hosts` inside containers
for this service, e.g:
172.17.2.186 db
172.17.2.186 database
172.17.2.187 redis
Environment variables will also be created - see the [environment variable
reference](env.md) for details.
2015-07-16 13:51:15 +02:00
2015-09-09 21:54:51 +02:00
### log_driver
2015-05-06 13:18:58 +02:00
2015-09-09 21:54:51 +02:00
Specify a logging driver for the service's containers, as with the ``--log-driver``
option for docker run ([documented here](https://docs.docker.com/reference/logging/overview/)).
2015-05-06 13:18:58 +02:00
The default value is json-file.
2015-06-19 11:28:09 +02:00
log_driver: "json-file"
log_driver: "syslog"
log_driver: "none"
2015-05-06 13:18:58 +02:00
2015-09-09 21:54:51 +02:00
> **Note:** Only the `json-file` driver makes the logs available directly from
> `docker-compose up` and `docker-compose logs`. Using any other driver will not
> print any logs.
### log_opt
2015-07-07 13:40:16 +02:00
Specify logging options with `log_opt` for the logging driver, as with the ``--log-opt`` option for `docker run` .
Logging options are key value pairs. An example of `syslog` options:
log_driver: "syslog"
log_opt:
2015-08-20 16:38:43 +02:00
syslog-address: "tcp://192.168.0.42:123"
2015-07-07 13:40:16 +02:00
2014-07-12 18:45:11 +02:00
### net
Networking mode. Use the same values as the docker client `--net` parameter.
2015-06-19 11:28:09 +02:00
net: "bridge"
net: "none"
net: "container:[name or id]"
net: "host"
2015-02-24 14:39:06 +01:00
### pid
2015-06-19 11:28:09 +02:00
pid: "host"
2015-02-24 14:39:06 +01:00
Sets the PID mode to the host PID mode. This turns on sharing between
container and the host operating system the PID address space. Containers
launched with this flag will be able to access and manipulate other
containers in the bare-metal machine's namespace and vise-versa.
2014-07-18 03:11:50 +02:00
2015-07-31 08:35:13 +02:00
### ports
2014-07-18 03:11:50 +02:00
2015-07-31 08:35:13 +02:00
Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container
port (a random host port will be chosen).
2014-07-23 00:36:32 +02:00
2015-07-31 08:35:13 +02:00
> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
> parse numbers in the format `xx:yy` as sexagesimal (base 60). For this reason,
> we recommend always explicitly specifying your port mappings as strings.
2014-11-06 20:38:58 +01:00
2015-07-31 08:35:13 +02:00
ports:
- "3000"
2015-10-08 12:50:27 +02:00
- "3000-3005"
2015-07-31 08:35:13 +02:00
- "8000:8000"
2015-10-08 12:50:27 +02:00
- "9090-9091:8080-8081"
2015-07-31 08:35:13 +02:00
- "49100:22"
- "127.0.0.1:8001:8001"
2015-10-08 12:50:27 +02:00
- "127.0.0.1:5000-5010:5000-5010"
2014-11-06 20:38:58 +01:00
2015-07-31 08:35:13 +02:00
### security_opt
2014-11-06 20:38:58 +01:00
2015-07-31 08:35:13 +02:00
Override the default labeling scheme for each container.
2014-11-06 20:38:58 +01:00
2015-07-31 08:35:13 +02:00
security_opt:
- label:user:USER
- label:role:ROLE
2014-08-17 15:18:24 +02:00
2015-10-26 22:39:50 +01:00
### ulimits
2015-11-10 19:37:07 +01:00
Override the default ulimits for a container. You can either specify a single
limit as an integer or soft/hard limits as a mapping.
2015-10-26 22:39:50 +01:00
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
2015-10-09 17:32:26 +02:00
### volumes, volume\_driver
2014-08-17 15:18:24 +02:00
2015-07-31 08:35:13 +02:00
Mount paths as volumes, optionally specifying a path on the host machine
(`HOST:CONTAINER`), or an access mode (`HOST:CONTAINER:ro`).
2014-08-17 15:18:24 +02:00
2015-07-31 08:35:13 +02:00
volumes:
- /var/lib/mysql
- ./cache:/tmp/cache
- ~/configs:/etc/configs/:ro
2015-05-09 01:14:32 +02:00
2015-07-31 08:35:13 +02:00
You can mount a relative path on the host, which will expand relative to
the directory of the Compose configuration file being used. Relative paths
should always begin with `.` or `..` .
2015-05-09 01:14:32 +02:00
2015-10-09 17:32:26 +02:00
If you use a volume name (instead of a volume path), you may also specify
a `volume_driver` .
volume_driver: mydriver
2015-07-31 08:35:13 +02:00
> Note: No path expansion will be done if you have also specified a
> `volume_driver`.
2015-05-09 01:14:32 +02:00
2015-10-09 17:32:26 +02:00
See [Docker Volumes ](https://docs.docker.com/userguide/dockervolumes/ ) and
[Volume Plugins ](https://docs.docker.com/extend/plugins_volume/ ) for more
information.
2015-07-31 08:35:13 +02:00
### volumes_from
2015-04-17 03:34:42 +02:00
2015-10-06 16:43:26 +02:00
Mount all of the volumes from another service or container, optionally
specifying read-only access(``ro``) or read-write(``rw``).
2015-04-17 03:34:42 +02:00
2015-07-31 08:35:13 +02:00
volumes_from:
- service_name
- container_name
2015-07-18 11:38:46 +02:00
- service_name:rw
2015-04-17 03:34:42 +02:00
2015-10-09 17:32:26 +02:00
### cpu\_shares, cpuset, domainname, entrypoint, hostname, ipc, mac\_address, mem\_limit, memswap\_limit, privileged, read\_only, restart, stdin\_open, tty, user, working\_dir
2014-07-23 00:36:32 +02:00
2015-01-19 06:46:23 +01:00
Each of these is a single value, analogous to its
[docker run ](https://docs.docker.com/reference/run/ ) counterpart.
2014-07-23 00:36:32 +02:00
2015-06-19 11:28:09 +02:00
cpu_shares: 73
cpuset: 0,1
2015-01-11 19:58:08 +01:00
2015-06-19 11:28:09 +02:00
entrypoint: /code/entrypoint.sh
user: postgresql
2015-09-18 12:30:24 +02:00
working_dir: /code
2014-07-23 00:36:32 +02:00
2015-06-19 11:28:09 +02:00
domainname: foo.com
2015-09-18 12:30:24 +02:00
hostname: foo
ipc: host
2015-06-19 11:28:09 +02:00
mac_address: 02:42:ac:11:65:43
2015-03-20 20:14:30 +01:00
2015-06-19 11:28:09 +02:00
mem_limit: 1000000000
2015-07-06 18:07:31 +02:00
memswap_limit: 2000000000
2015-06-19 11:28:09 +02:00
privileged: true
2014-10-28 16:31:09 +01:00
2015-06-19 11:28:09 +02:00
restart: always
2015-01-03 19:18:22 +01:00
2015-09-18 12:30:24 +02:00
read_only: true
2015-06-19 11:28:09 +02:00
stdin_open: true
tty: true
2015-03-20 20:14:30 +01:00
2015-07-24 16:58:18 +02:00
## Variable substitution
Your configuration options can contain environment variables. Compose uses the
variable values from the shell environment in which `docker-compose` is run. For
example, suppose the shell contains `POSTGRES_VERSION=9.3` and you supply this
configuration:
db:
image: "postgres:${POSTGRES_VERSION}"
When you run `docker-compose up` with this configuration, Compose looks for the
`POSTGRES_VERSION` environment variable in the shell and substitutes its value
in. For this example, Compose resolves the `image` to `postgres:9.3` before
running the configuration.
If an environment variable is not set, Compose substitutes with an empty
string. In the example above, if `POSTGRES_VERSION` is not set, the value for
the `image` option is `postgres:` .
Both `$VARIABLE` and `${VARIABLE}` syntax are supported. Extended shell-style
features, such as `${VARIABLE-default}` and `${VARIABLE/foo/bar}` , are not
supported.
2015-10-16 13:57:54 +02:00
You can use a `$$` (double-dollar sign) when your configuration needs a literal
dollar sign. This also prevents Compose from interpolating a value, so a `$$`
allows you to refer to environment variables that you don't want processed by
Compose.
2015-07-24 16:58:18 +02:00
2015-10-16 13:57:54 +02:00
web:
build: .
command: "$$VAR_NOT_INTERPOLATED_BY_COMPOSE"
If you forget and use a single dollar sign (`$`), Compose interprets the value as an environment variable and will warn you:
The VAR_NOT_INTERPOLATED_BY_COMPOSE is not set. Substituting an empty string.
2015-07-24 16:58:18 +02:00
2015-02-25 09:43:33 +01:00
## Compose documentation
2015-10-13 13:01:19 +02:00
- [User guide ](index.md )
2015-05-12 13:44:43 +02:00
- [Installing Compose ](install.md )
- [Get started with Django ](django.md )
- [Get started with Rails ](rails.md )
2015-09-16 17:01:43 +02:00
- [Get started with WordPress ](wordpress.md )
2015-10-13 13:01:19 +02:00
- [Command line reference ](./reference/index.md )