2014-01-27 16:03:21 +01:00
---
layout: default
2015-01-20 18:29:28 +01:00
title: docker-compose.yml reference
2015-01-19 06:46:23 +01:00
page_title: docker-compose.yml reference
page_description: docker-compose.yml reference
page_keywords: fig, composition, compose, docker
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
```
2014-01-27 16:03:21 +01:00
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
2014-07-12 18:45:11 +02:00
```
### build
2015-02-25 09:43:33 +01:00
Path to a directory containing a Dockerfile. This directory is also the
build context that is sent to the Docker daemon.
Compose will build and tag it with a generated name, and use that image thereafter.
2014-01-27 16:03:21 +01:00
2014-07-12 18:45:11 +02:00
```
2014-01-27 16:03:21 +01:00
build: /path/to/build/dir
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.
```
2014-01-27 16:03:21 +01: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
```
2014-01-27 16:03:21 +01:00
links:
- db
2014-03-03 18:58:35 +01:00
- db:database
2014-01-27 16:03:21 +01:00
- 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
```
2014-08-08 20:00:10 +02:00
172.17.2.186 db
2014-08-08 19:59:08 +02:00
172.17.2.186 database
2014-08-08 20:00:10 +02:00
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
```
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
```
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
```
2014-01-27 16:03:21 +01:00
ports:
2014-02-20 13:53:43 +01:00
- "3000"
- "8000:8000"
- "49100:22"
2014-07-12 18:38:18 +02:00
- "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
2014-07-12 18:45:11 +02:00
```
2014-03-04 18:59:42 +01:00
expose:
- "3000"
- "8000"
2014-07-12 18:45:11 +02:00
```
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
2014-07-12 18:45:11 +02:00
```
2014-01-27 16:03:21 +01:00
volumes:
2014-07-12 18:55:11 +02:00
- /var/lib/mysql
2014-01-27 16:03:21 +01:00
- cache/:/tmp/cache
2015-01-20 18:44:48 +01:00
- ~/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.
```
2014-07-11 20:11:54 +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
2014-07-12 18:45:11 +02:00
```
2014-01-27 16:03:21 +01:00
environment:
RACK_ENV: development
2014-07-11 19:07:50 +02:00
SESSION_SECRET:
2014-07-12 18:55:11 +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-03-12 14:59:23 +01:00
env_file: .env
2014-09-12 05:57:23 +02:00
env_file:
2015-03-12 14:59:23 +01:00
- ./common.env
- ./apps/web.env
- /opt/secrets.env
2014-09-12 05:57:23 +02:00
```
```
RACK_ENV: development
```
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**
```
webapp:
build: ./webapp
environment:
- DEBUG=false
- SEND_EMAILS=false
```
**development.yml**
```
web:
extends:
file: common.yml
service: webapp
ports:
- "8000:8000"
links:
- db
environment:
- DEBUG=true
db:
image: postgres
```
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
(SEND_EMAILS) is left untouched. It's exactly as if you defined `web` like
this:
```yaml
web:
build: ./webapp
ports:
- "8000:8000"
links:
- db
environment:
- DEBUG=true
- SEND_EMAILS=false
```
The `extends` option is great for sharing configuration between different
apps, or for configuring the same app differently for different environments.
You could write a new file for a staging environment, **staging.yml** , which
binds to a different port and doesn't turn on debugging:
```
web:
extends:
file: common.yml
service: webapp
ports:
- "80:8000"
links:
- db
db:
image: postgres
```
> **Note:** When you extend a service, `links` and `volumes_from`
> configuration options are **not** inherited - you will have to define
> those manually each time you extend it.
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.
```
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
- "com.example.label-with-empty-value"
```
2014-07-12 18:45:11 +02:00
### net
Networking mode. Use the same values as the docker client `--net` parameter.
```
2014-07-12 18:55:11 +02:00
net: "bridge"
net: "none"
net: "container:[name or id]"
2014-06-19 12:57:55 +02:00
net: "host"
2014-07-12 18:45:11 +02:00
```
2014-07-18 03:11:50 +02:00
### dns
Custom DNS servers. Can be a single value or a list.
```
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.
```
cap_add:
- ALL
cap_drop:
- NET_ADMIN
- SYS_ADMIN
```
2014-08-17 15:18:24 +02:00
### dns_search
Custom DNS search domains. Can be a single value or a list.
```
dns_search: example.com
2014-10-01 18:40:50 +02:00
dns_search:
2014-08-17 15:18:24 +02:00
- dc1.example.com
- dc2.example.com
```
2015-01-11 19:58:08 +01:00
### working\_dir, entrypoint, user, hostname, domainname, mem\_limit, privileged, restart, stdin\_open, tty, cpu\_shares
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-01-11 19:58:08 +01:00
cpu_shares: 73
2014-07-23 00:36:32 +02:00
working_dir: /code
entrypoint: /code/entrypoint.sh
user: postgresql
hostname: foo
domainname: foo.com
mem_limit: 1000000000
privileged: true
2014-10-28 16:31:09 +01:00
restart: always
2015-01-03 19:18:22 +01:00
stdin_open: true
tty: true
2014-07-23 00:36:32 +02:00
```
2015-02-25 09:43:33 +01:00
## Compose documentation
- [Installing Compose ](install.md )
- [User guide ](index.md )
- [Command line reference ](cli.md )
- [Compose environment variables ](env.md )
- [Compose command line completion ](completion.md )