2015-06-07 22:59:58 +02:00
<!-- [metadata]>
+++
title = "docker-compose.yml reference"
description = "docker-compose.yml reference"
keywords = ["fig, composition, compose, docker"]
[menu.main]
parent="smn_compose_ref"
+++
<![end-metadata]-->
2014-01-27 16:03:21 +01:00
2015-01-19 06:46:23 +01:00
# docker-compose.yml reference
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-01-19 06:46:23 +01:00
### image
2014-07-12 18:45:11 +02:00
2015-01-19 06:46:23 +01:00
Tag or partial image ID. Can be local or remote - Compose will attempt to
pull if it doesn't exist locally.
2014-07-12 18:45:11 +02:00
2015-06-19 11:28:09 +02:00
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
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-03-06 21:33:56 +01:00
### dockerfile
Alternate Dockerfile.
Compose will use an alternate file to build with.
2015-06-19 11:28:09 +02:00
dockerfile: Dockerfile-alternate
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
2014-08-08 19:59:08 +02:00
< a name = "links" > < / a >
2014-07-12 18:45:11 +02:00
### links
2015-01-19 06:46:23 +01: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).
2014-07-12 18:45:11 +02:00
2015-06-19 11:28:09 +02:00
links:
- db
- db:database
- redis
2014-07-12 18:45:11 +02:00
2015-01-19 06:46:23 +01:00
An entry with the alias' name will be created in `/etc/hosts` inside containers
for this service, e.g:
2014-08-08 19:59:08 +02:00
2015-06-19 11:28:09 +02:00
172.17.2.186 db
172.17.2.186 database
172.17.2.187 redis
2014-08-08 19:59:08 +02:00
2015-01-19 06:46:23 +01:00
Environment variables will also be created - see the [environment variable
2015-02-23 04:56:13 +01:00
reference](env.md) for details.
2014-08-08 19:59:08 +02:00
2014-10-09 04:31:04 +02:00
### external_links
2015-01-19 06:46:23 +01:00
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`).
2014-10-09 04:31:04 +02:00
2015-06-19 11:28:09 +02:00
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
2014-10-09 04:31:04 +02:00
2015-01-15 21:58:17 +01:00
### extra_hosts
2015-03-24 11:25:09 +01:00
Add hostname mappings. Use the same values as the docker client `--add-host` parameter.
2015-01-15 21:58:17 +01:00
2015-06-19 11:28:09 +02:00
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
2015-01-15 21:58:17 +01:00
An entry with the ip address and hostname will be created in `/etc/hosts` inside containers for this service, e.g:
2015-06-19 11:28:09 +02:00
162.242.195.82 somehost
50.31.209.229 otherhost
2015-01-15 21:58:17 +01:00
2014-07-12 18:45:11 +02:00
### ports
2015-01-19 06:46:23 +01:00
Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container
port (a random host port will be chosen).
2014-01-27 16:03:21 +01:00
2015-01-19 06:46:23 +01: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-07-12 18:45:11 +02:00
2015-06-19 11:28:09 +02:00
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
2014-07-12 18:45:11 +02:00
### expose
2015-01-19 06:46:23 +01: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.
2014-01-27 16:03:21 +01:00
2015-06-19 11:28:09 +02:00
expose:
- "3000"
- "8000"
2014-03-04 18:59:42 +01:00
2014-07-12 18:45:11 +02:00
### volumes
2014-08-26 04:16:37 +02:00
Mount paths as volumes, optionally specifying a path on the host machine
(`HOST:CONTAINER`), or an access mode (`HOST:CONTAINER:ro`).
2014-07-24 23:16:49 +02:00
2015-06-19 11:28:09 +02:00
volumes:
- /var/lib/mysql
- cache/:/tmp/cache
- ~/configs:/etc/configs/:ro
2014-07-12 18:45:11 +02:00
### volumes_from
2014-01-27 16:03:21 +01:00
2014-07-12 18:45:11 +02:00
Mount all of the volumes from another service or container.
2015-06-19 11:28:09 +02:00
volumes_from:
- service_name
- container_name
2014-07-12 18:45:11 +02:00
### environment
2014-07-12 18:55:11 +02:00
Add environment variables. You can use either an array or a dictionary.
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
SESSION_SECRET:
2014-07-12 18:55:11 +02:00
2015-06-19 11:28:09 +02:00
environment:
- RACK_ENV=development
- SESSION_SECRET
2014-01-27 16:03:21 +01:00
2014-09-12 05:57:23 +02:00
### env_file
Add environment variables from a file. Can be a single value or a list.
2015-03-12 14:59:23 +01: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-09-12 05:57:23 +02:00
Environment variables specified in `environment` override these values.
2015-06-19 11:28:09 +02:00
env_file: .env
2015-03-12 14:59:23 +01:00
2015-06-19 11:28:09 +02:00
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
2014-09-12 05:57:23 +02:00
2015-03-24 15:22:11 +01: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.
2015-06-19 11:28:09 +02:00
# Set Rails/Rack environment
RACK_ENV=development
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.
Here's a simple example. Suppose we have 2 files - **common.yml** and
**development.yml**. We can use `extends` to define a service in
**development.yml** which uses configuration defined in **common.yml** :
**common.yml**
2015-06-19 11:28:09 +02:00
webapp:
build: ./webapp
environment:
- DEBUG=false
- SEND_EMAILS=false
2015-03-18 21:51:27 +01:00
**development.yml**
2015-06-19 11:28:09 +02:00
web:
extends:
file: common.yml
service: webapp
ports:
- "8000:8000"
links:
- db
environment:
- DEBUG=true
db:
image: postgres
2015-03-18 21:51:27 +01:00
Here, the `web` service in **development.yml** inherits the configuration of
the `webapp` service in **common.yml** - the `build` and `environment` keys -
and adds `ports` and `links` configuration. It overrides one of the defined
environment variables (DEBUG) with a new value, and the other one
2015-04-03 22:31:12 +02:00
(SEND_EMAILS) is left untouched.
2015-03-18 21:51:27 +01:00
2015-07-07 15:17:09 +02:00
The `file` key is optional, if it is not set then Compose will look for the
service within the current file.
2015-04-03 22:31:12 +02:00
For more on `extends` , see the [tutorial ](extends.md#example ) and
[reference ](extends.md#reference ).
2015-03-18 21:51:27 +01:00
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-05-06 13:18:58 +02:00
### log driver
Specify a logging driver for the service's containers, as with the ``--log-driver`` option for docker run ([documented here](http://docs.docker.com/reference/run/#logging-drivers-log-driver)).
Allowed values are currently ``json-file``, ``syslog`` and ``none``. The list will change over time as more drivers are added to the Docker engine.
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-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:
address: "tcp://192.168.0.42:123"
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
### dns
Custom DNS servers. Can be a single value or a list.
2015-06-19 11:28:09 +02:00
dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
2014-07-23 00:36:32 +02:00
2014-11-06 20:38:58 +01:00
### cap_add, cap_drop
Add or drop container capabilities.
See `man 7 capabilities` for a full list.
2015-06-19 11:28:09 +02:00
cap_add:
- ALL
2014-11-06 20:38:58 +01:00
2015-06-19 11:28:09 +02:00
cap_drop:
- NET_ADMIN
- SYS_ADMIN
2014-11-06 20:38:58 +01:00
2014-08-17 15:18:24 +02:00
### dns_search
Custom DNS search domains. Can be a single value or a list.
2015-06-19 11:28:09 +02:00
dns_search: example.com
dns_search:
- dc1.example.com
- dc2.example.com
2014-08-17 15:18:24 +02:00
2015-05-09 01:14:32 +02:00
### devices
List of device mappings. Uses the same format as the `--device` docker
client create option.
2015-06-19 11:28:09 +02:00
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
2015-05-09 01:14:32 +02:00
2015-04-17 03:34:42 +02:00
### security_opt
Override the default labeling scheme for each container.
2015-06-19 11:28:09 +02:00
security_opt:
- label:user:USER
- label:role:ROLE
2015-04-17 03:34:42 +02:00
2015-07-06 18:07:31 +02:00
### working\_dir, entrypoint, user, hostname, domainname, mac\_address, mem\_limit, memswap\_limit, privileged, restart, stdin\_open, tty, cpu\_shares, cpuset, read\_only
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
working_dir: /code
entrypoint: /code/entrypoint.sh
user: postgresql
2014-07-23 00:36:32 +02:00
2015-06-19 11:28:09 +02:00
hostname: foo
domainname: foo.com
2014-07-23 00:36:32 +02:00
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-06-19 11:28:09 +02:00
stdin_open: true
tty: true
read_only: true
2015-03-20 20:14:30 +01:00
2015-02-25 09:43:33 +01:00
## Compose documentation
2015-06-16 05:52:55 +02:00
- [User guide ](/ )
2015-05-12 13:44:43 +02:00
- [Installing Compose ](install.md )
- [Get started with Django ](django.md )
- [Get started with Rails ](rails.md )
- [Get started with Wordpress ](wordpress.md )
2015-02-25 09:43:33 +01:00
- [Command line reference ](cli.md )
- [Compose environment variables ](env.md )
- [Compose command line completion ](completion.md )