2015-06-07 22:59:58 +02:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
2016-01-29 00:53:40 +01:00
|
|
|
title = "Extending Services in Compose"
|
2015-06-07 22:59:58 +02:00
|
|
|
description = "How to use Docker Compose's extends keyword to share configuration between files and projects"
|
|
|
|
keywords = ["fig, composition, compose, docker, orchestration, documentation, docs"]
|
|
|
|
[menu.main]
|
2016-01-24 21:03:44 +01:00
|
|
|
parent="workw_compose"
|
2016-01-29 00:50:05 +01:00
|
|
|
weight=20
|
2015-06-07 22:59:58 +02:00
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
# Extending services and Compose files
|
2015-10-09 18:34:55 +02:00
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
Compose supports two methods of sharing common configuration:
|
2015-10-09 18:34:55 +02:00
|
|
|
|
2015-11-03 17:52:44 +01:00
|
|
|
1. Extending an entire Compose file by
|
2015-11-02 21:14:42 +01:00
|
|
|
[using multiple Compose files](#multiple-compose-files)
|
2015-11-03 17:52:44 +01:00
|
|
|
2. Extending individual services with [the `extends` field](#extending-services)
|
|
|
|
|
|
|
|
|
|
|
|
## Multiple Compose files
|
|
|
|
|
|
|
|
Using multiple Compose files enables you to customize a Compose application
|
|
|
|
for different environments or different workflows.
|
|
|
|
|
|
|
|
### Understanding multiple Compose files
|
|
|
|
|
|
|
|
By default, Compose reads two files, a `docker-compose.yml` and an optional
|
|
|
|
`docker-compose.override.yml` file. By convention, the `docker-compose.yml`
|
|
|
|
contains your base configuration. The override file, as its name implies, can
|
|
|
|
contain configuration overrides for existing services or entirely new
|
|
|
|
services.
|
|
|
|
|
2016-02-01 20:04:53 +01:00
|
|
|
If a service is defined in both files Compose merges the configurations using
|
|
|
|
the rules described in [Adding and overriding
|
|
|
|
configuration](#adding-and-overriding-configuration).
|
2015-11-03 17:52:44 +01:00
|
|
|
|
|
|
|
To use multiple override files, or an override file with a different name, you
|
|
|
|
can use the `-f` option to specify the list of files. Compose merges files in
|
|
|
|
the order they're specified on the command line. See the [`docker-compose`
|
2016-02-01 18:10:21 +01:00
|
|
|
command reference](./reference/overview.md) for more information about
|
2015-11-03 17:52:44 +01:00
|
|
|
using `-f`.
|
|
|
|
|
|
|
|
When you use multiple configuration files, you must make sure all paths in the
|
|
|
|
files are relative to the base Compose file (the first Compose file specified
|
|
|
|
with `-f`). This is required because override files need not be valid
|
|
|
|
Compose files. Override files can contain small fragments of configuration.
|
|
|
|
Tracking which fragment of a service is relative to which path is difficult and
|
|
|
|
confusing, so to keep paths easier to understand, all paths must be defined
|
|
|
|
relative to the base file.
|
|
|
|
|
|
|
|
### Example use case
|
|
|
|
|
|
|
|
In this section are two common use cases for multiple compose files: changing a
|
|
|
|
Compose app for different environments, and running administrative tasks
|
|
|
|
against a Compose app.
|
|
|
|
|
|
|
|
#### Different environments
|
|
|
|
|
|
|
|
A common use case for multiple files is changing a development Compose app
|
|
|
|
for a production-like environment (which may be production, staging or CI).
|
|
|
|
To support these differences, you can split your Compose configuration into
|
|
|
|
a few different files:
|
|
|
|
|
|
|
|
Start with a base file that defines the canonical configuration for the
|
|
|
|
services.
|
|
|
|
|
|
|
|
**docker-compose.yml**
|
|
|
|
|
|
|
|
web:
|
|
|
|
image: example/my_web_app:latest
|
|
|
|
links:
|
|
|
|
- db
|
|
|
|
- cache
|
|
|
|
|
|
|
|
db:
|
|
|
|
image: postgres:latest
|
|
|
|
|
|
|
|
cache:
|
|
|
|
image: redis:latest
|
|
|
|
|
|
|
|
In this example the development configuration exposes some ports to the
|
|
|
|
host, mounts our code as a volume, and builds the web image.
|
|
|
|
|
|
|
|
**docker-compose.override.yml**
|
|
|
|
|
|
|
|
|
|
|
|
web:
|
|
|
|
build: .
|
|
|
|
volumes:
|
|
|
|
- '.:/code'
|
|
|
|
ports:
|
|
|
|
- 8883:80
|
|
|
|
environment:
|
|
|
|
DEBUG: 'true'
|
|
|
|
|
|
|
|
db:
|
|
|
|
command: '-d'
|
|
|
|
ports:
|
|
|
|
- 5432:5432
|
|
|
|
|
|
|
|
cache:
|
|
|
|
ports:
|
|
|
|
- 6379:6379
|
|
|
|
|
|
|
|
When you run `docker-compose up` it reads the overrides automatically.
|
|
|
|
|
|
|
|
Now, it would be nice to use this Compose app in a production environment. So,
|
|
|
|
create another override file (which might be stored in a different git
|
|
|
|
repo or managed by a different team).
|
|
|
|
|
|
|
|
**docker-compose.prod.yml**
|
|
|
|
|
|
|
|
web:
|
|
|
|
ports:
|
|
|
|
- 80:80
|
|
|
|
environment:
|
|
|
|
PRODUCTION: 'true'
|
|
|
|
|
|
|
|
cache:
|
|
|
|
environment:
|
|
|
|
TTL: '500'
|
|
|
|
|
|
|
|
To deploy with this production Compose file you can run
|
|
|
|
|
|
|
|
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
|
|
|
|
|
|
|
|
This deploys all three services using the configuration in
|
|
|
|
`docker-compose.yml` and `docker-compose.prod.yml` (but not the
|
|
|
|
dev configuration in `docker-compose.override.yml`).
|
|
|
|
|
|
|
|
|
|
|
|
See [production](production.md) for more information about Compose in
|
|
|
|
production.
|
|
|
|
|
|
|
|
#### Administrative tasks
|
|
|
|
|
|
|
|
Another common use case is running adhoc or administrative tasks against one
|
|
|
|
or more services in a Compose app. This example demonstrates running a
|
|
|
|
database backup.
|
|
|
|
|
|
|
|
Start with a **docker-compose.yml**.
|
|
|
|
|
|
|
|
web:
|
|
|
|
image: example/my_web_app:latest
|
|
|
|
links:
|
|
|
|
- db
|
|
|
|
|
|
|
|
db:
|
|
|
|
image: postgres:latest
|
|
|
|
|
|
|
|
In a **docker-compose.admin.yml** add a new service to run the database
|
|
|
|
export or backup.
|
|
|
|
|
|
|
|
dbadmin:
|
|
|
|
build: database_admin/
|
|
|
|
links:
|
|
|
|
- db
|
|
|
|
|
|
|
|
To start a normal environment run `docker-compose up -d`. To run a database
|
|
|
|
backup, include the `docker-compose.admin.yml` as well.
|
|
|
|
|
|
|
|
docker-compose -f docker-compose.yml -f docker-compose.admin.yml \
|
|
|
|
run dbadmin db-backup
|
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
## Extending services
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
Docker Compose's `extends` keyword enables sharing of common configurations
|
|
|
|
among different files, or even different projects entirely. Extending services
|
2015-10-09 18:34:55 +02:00
|
|
|
is useful if you have several services that reuse a common set of configuration
|
|
|
|
options. Using `extends` you can define a common set of service options in one
|
|
|
|
place and refer to it from anywhere.
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2016-02-01 20:04:53 +01:00
|
|
|
> **Note:** `links`, `volumes_from`, and `depends_on` are never shared between
|
|
|
|
> services using >`extends`. These exceptions exist to avoid
|
|
|
|
> implicit dependencies—you always define `links` and `volumes_from`
|
|
|
|
> locally. This ensures dependencies between services are clearly visible when
|
|
|
|
> reading the current file. Defining these locally also ensures changes to the
|
|
|
|
> referenced file don't result in breakage.
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
### Understand the extends configuration
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
When defining any service in `docker-compose.yml`, you can declare that you are
|
|
|
|
extending another service like this:
|
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
web:
|
|
|
|
extends:
|
|
|
|
file: common-services.yml
|
|
|
|
service: webapp
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
This instructs Compose to re-use the configuration for the `webapp` service
|
|
|
|
defined in the `common-services.yml` file. Suppose that `common-services.yml`
|
|
|
|
looks like this:
|
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
webapp:
|
|
|
|
build: .
|
|
|
|
ports:
|
|
|
|
- "8000:8000"
|
|
|
|
volumes:
|
|
|
|
- "/data"
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
In this case, you'll get exactly the same result as if you wrote
|
2015-10-09 18:56:59 +02:00
|
|
|
`docker-compose.yml` with the same `build`, `ports` and `volumes` configuration
|
|
|
|
values defined directly under `web`.
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
You can go further and define (or re-define) configuration locally in
|
|
|
|
`docker-compose.yml`:
|
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
web:
|
|
|
|
extends:
|
|
|
|
file: common-services.yml
|
|
|
|
service: webapp
|
|
|
|
environment:
|
|
|
|
- DEBUG=1
|
|
|
|
cpu_shares: 5
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-14 20:06:05 +02:00
|
|
|
important_web:
|
|
|
|
extends: web
|
|
|
|
cpu_shares: 10
|
|
|
|
|
2015-04-03 22:31:12 +02:00
|
|
|
You can also write other services and link your `web` service to them:
|
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
web:
|
|
|
|
extends:
|
|
|
|
file: common-services.yml
|
|
|
|
service: webapp
|
|
|
|
environment:
|
|
|
|
- DEBUG=1
|
|
|
|
cpu_shares: 5
|
|
|
|
links:
|
|
|
|
- db
|
|
|
|
db:
|
|
|
|
image: postgres
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
### Example use case
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
Extending an individual service is useful when you have multiple services that
|
2015-11-02 21:14:42 +01:00
|
|
|
have a common configuration. The example below is a Compose app with
|
2015-10-09 18:56:59 +02:00
|
|
|
two services: a web application and a queue worker. Both services use the same
|
|
|
|
codebase and share many configuration options.
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
In a **common.yml** we define the common configuration:
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
app:
|
|
|
|
build: .
|
|
|
|
environment:
|
|
|
|
CONFIG_FILE_PATH: /code/config
|
|
|
|
API_KEY: xxxyyy
|
|
|
|
cpu_shares: 5
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
In a **docker-compose.yml** we define the concrete services which use the
|
2015-10-09 18:34:55 +02:00
|
|
|
common configuration:
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
webapp:
|
|
|
|
extends:
|
|
|
|
file: common.yml
|
|
|
|
service: app
|
|
|
|
command: /code/run_web_app
|
|
|
|
ports:
|
|
|
|
- 8080:8080
|
|
|
|
links:
|
|
|
|
- queue
|
|
|
|
- db
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
queue_worker:
|
|
|
|
extends:
|
|
|
|
file: common.yml
|
|
|
|
service: app
|
2015-10-09 18:56:59 +02:00
|
|
|
command: /code/run_worker
|
|
|
|
links:
|
|
|
|
- queue
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-11-03 17:52:44 +01:00
|
|
|
## Adding and overriding configuration
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2016-02-01 20:04:53 +01:00
|
|
|
Compose copies configurations from the original service over to the local one.
|
2015-10-09 18:56:59 +02:00
|
|
|
If a configuration option is defined in both the original service the local
|
|
|
|
service, the local value *replaces* or *extends* the original value.
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
For single-value options like `image`, `command` or `mem_limit`, the new value
|
2015-10-09 18:56:59 +02:00
|
|
|
replaces the old value.
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# original service
|
|
|
|
command: python app.py
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# local service
|
|
|
|
command: python otherapp.py
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# result
|
|
|
|
command: python otherapp.py
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
In the case of `build` and `image`, using one in the local service causes
|
|
|
|
Compose to discard the other, if it was defined in the original service.
|
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
Example of image replacing build:
|
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# original service
|
|
|
|
build: .
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# local service
|
|
|
|
image: redis
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# result
|
|
|
|
image: redis
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
|
|
|
|
Example of build replacing image:
|
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# original service
|
|
|
|
image: redis
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# local service
|
|
|
|
build: .
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# result
|
|
|
|
build: .
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
For the **multi-value options** `ports`, `expose`, `external_links`, `dns` and
|
|
|
|
`dns_search`, Compose concatenates both sets of values:
|
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# original service
|
|
|
|
expose:
|
|
|
|
- "3000"
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# local service
|
|
|
|
expose:
|
|
|
|
- "4000"
|
|
|
|
- "5000"
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# result
|
|
|
|
expose:
|
|
|
|
- "3000"
|
|
|
|
- "4000"
|
|
|
|
- "5000"
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-10-09 18:56:59 +02:00
|
|
|
In the case of `environment`, `labels`, `volumes` and `devices`, Compose
|
|
|
|
"merges" entries together with locally-defined values taking precedence:
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# original service
|
|
|
|
environment:
|
|
|
|
- FOO=original
|
|
|
|
- BAR=original
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# local service
|
|
|
|
environment:
|
|
|
|
- BAR=local
|
|
|
|
- BAZ=local
|
2015-04-03 22:31:12 +02:00
|
|
|
|
2015-06-21 21:37:20 +02:00
|
|
|
# result
|
|
|
|
environment:
|
|
|
|
- FOO=original
|
|
|
|
- BAR=local
|
|
|
|
- BAZ=local
|
2015-04-03 22:31:12 +02:00
|
|
|
|
|
|
|
|
2015-10-09 18:34:55 +02:00
|
|
|
|
|
|
|
|
2015-05-12 13:44:43 +02:00
|
|
|
## Compose documentation
|
|
|
|
|
2015-11-18 11:17:23 +01:00
|
|
|
- [User guide](index.md)
|
2015-05-12 13:44:43 +02:00
|
|
|
- [Installing Compose](install.md)
|
2015-10-09 16:49:41 +02:00
|
|
|
- [Getting Started](gettingstarted.md)
|
2015-05-12 13:44:43 +02:00
|
|
|
- [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)
|
2015-10-13 21:23:27 +02:00
|
|
|
- [Compose file reference](compose-file.md)
|