There are three legacy versions of the Compose file format:
Version 1. This is specified by omitting a version
key at the root of the YAML.
Version 2.x. This is specified with a version: '2'
or version: '2.1'
, etc., entry at the root of the YAML.
Version 3.x, designed to be cross-compatible between Compose and the Docker Engine's
swarm mode. This is specified with a version: '3'
or version: '3.1'
, etc., entry at the root of the YAML.
The latest and recommended version of the Compose file format is defined by the Compose Specification. This format merges the 2.x and 3.x versions and is implemented by Compose 1.27.0+.
Note
If you're using multiple Compose files or extending services, each file must be of the same version - you cannot, for example, mix version 1 and 2 in a single project.
Several things differ depending on which version you use:
These differences are explained below.
Compose files using the version 2 syntax must indicate the version number at
the root of the document. All services
must be declared under the services
key.
Version 2 files are supported by Compose 1.6.0+ and require a Docker Engine of version 1.10.0+.
Named volumes can be declared under the
volumes
key, and networks can be declared
under the networks
key.
By default, every container joins an application-wide default network, and is discoverable at a hostname that's the same as the service name. This means links are largely unnecessary. For more details, see Networking in Compose.
Note
With Compose version 2, when specifying the Compose file version to use, make sure to specify both the major and minor numbers. If no minor version is given,
0
is used by default and not the latest minor version. As a result, features added in later versions will not be supported. For example:version: "2"
is equivalent to:
version: "2.0"
Simple example:
version: "{{% param "compose_file_v2" %}}"
services:
web:
build: .
ports:
- "8000:5000"
volumes:
- .:/code
redis:
image: redis
A more extended example, defining volumes and networks:
version: "{{% param "compose_file_v2" %}}"
services:
web:
build: .
ports:
- "8000:5000"
volumes:
- .:/code
networks:
- front-tier
- back-tier
redis:
image: redis
volumes:
- redis-data:/var/lib/redis
networks:
- back-tier
volumes:
redis-data:
driver: local
networks:
front-tier:
driver: bridge
back-tier:
driver: bridge
Several other options were added to support networking, such as:
The depends_on
option can be used in place of links to indicate dependencies
between services and startup order.
version: "{{% param "compose_file_v2" %}}" services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
Variable substitution also was added in Version 2.
An upgrade of version 2 that introduces new parameters only available with Docker Engine version 1.12.0+. Version 2.1 files are supported by Compose 1.9.0+.
Introduces the following additional parameters:
link_local_ips
isolation
in build configurations and
service definitionslabels
for volumes,
networks, and
buildname
for volumesuserns_mode
healthcheck
sysctls
pids_limit
oom_kill_disable
cpu_period
An upgrade of version 2.1 that introduces new parameters only available with Docker Engine version 1.13.0+. Version 2.2 files are supported by Compose 1.13.0+. This version also allows you to specify default scale numbers inside the service's configuration.
Introduces the following additional parameters:
An upgrade of version 2.2 that introduces new parameters only available with Docker Engine version 17.06.0+. Version 2.3 files are supported by Compose 1.16.0+.
Introduces the following additional parameters:
target
, extra_hosts
and
shm_size
for build configurationsstart_period
for healthchecks
runtime
for service definitionsdevice_cgroup_rules
An upgrade of version 2.3 that introduces new parameters only available with Docker Engine version 17.12.0+. Version 2.4 files are supported by Compose 1.21.0+.
Introduces the following additional parameters:
platform
for service definitionsDesigned to be cross-compatible between Compose and the Docker Engine's swarm mode, version 3 removes several options and adds several more.
Removed: volume_driver
, volumes_from
, cpu_shares
, cpu_quota
,
cpuset
, mem_limit
, memswap_limit
, extends
, group_add
. See
the upgrading guide for how to migrate away from these.
Added: deploy
If only the major version is given (version: '3'
),
the latest minor version is used by default.
An upgrade of version 3 that introduces new parameters only available with Docker Engine version 1.13.1+, and higher.
Introduces the following additional parameters:
An upgrade of version 3 that introduces new parameters only available with Docker Engine version 17.04.0+, and higher.
Introduces the following additional parameters:
cache_from
in build configurationsattachable
network driver optionendpoint_mode
preference
An upgrade of version 3 that introduces new parameters only available with Docker Engine version 17.06.0+, and higher.
Introduces the following additional parameters:
An upgrade of version 3 that introduces new parameters. It is only available with Docker Engine version 17.09.0 and higher.
Introduces the following additional parameters:
target
and network
in
build configurationsstart_period
for healthchecks
order
for update configurationsname
for volumesAn upgrade of version 3 that introduces new parameters. It is only available with Docker Engine version 17.12.0 and higher.
Introduces the following additional parameters:
isolation
in service definitionsname
for networks, secrets and configsshm_size
in build configurationsAn upgrade of version 3 that introduces new parameters. It is only available with Docker Engine version 18.02.0 and higher.
Introduces the following additional parameters:
tmpfs
size for tmpfs
-type mountsAn upgrade of version 3 that introduces new parameters. It is only available with Docker Engine version 18.06.0 and higher.
Introduces the following additional parameters:
init
in service definitionsrollback_config
in deploy configurationsAn upgrade of version 3 that introduces new parameters. It is only available with Docker Engine version 19.03.0 and higher.
Introduces the following additional parameters:
max_replicas_per_node
in placement
configurationstemplate_driver
option for config
and secret configurations. This
option is only supported when deploying swarm services using
docker stack deploy
.driver
and driver_opts
option for secret
configurations. This option is only supported when deploying swarm services
using docker stack deploy
.Compose versions below 1.6.x are
Compose files that do not declare a version are considered "version 1". In those files, all the services are declared at the root of the document.
Version 1 is supported by Compose up to 1.6.x** and has been deprecated.
Version 1 files cannot declare named volumes, networks or build arguments.
Compose does not take advantage of networking when you
use version 1: every container is placed on the default bridge
network and is
reachable from every other container at its IP address. You need to use
links
to enable discovery between containers.
Example:
web:
build: .
ports:
- "8000:5000"
volumes:
- .:/code
links:
- redis
redis:
image: redis
Between versions 2.x and 3.x, the structure of the Compose file is the same, but several options have been removed:
volume_driver
: Instead of setting the volume driver on the service, define
a volume using the
top-level volumes
option
and specify the driver there.
version: "3.8"
services:
db:
image: postgres
volumes:
- data:/var/lib/postgresql/data
volumes:
data:
driver: mydriver
volumes_from
: To share a volume between services, define it using the
top-level volumes
option
and reference it from each service that shares it using the
service-level volumes
option.
cpu_shares
, cpu_quota
, cpuset
, mem_limit
, memswap_limit
: These
have been replaced by the resources key under
deploy
. deploy
configuration only takes effect when using
docker stack deploy
, and is ignored by docker-compose
.
extends
: This option has been removed for version: "3.x"
Compose files.
For more information on extends
, see
Extending services.
group_add
: This option has been removed for version: "3.x"
Compose files.
pids_limit
: This option has not been introduced in version: "3.x"
Compose files.
link_local_ips
in networks
: This option has not been introduced in
version: "3.x"
Compose files.
docker-compose
1.20.0 introduces a new --compatibility
flag designed to
help developers transition to version 3 more easily. When enabled,
docker-compose
reads the deploy
section of each service's definition and
attempts to translate it into the equivalent version 2 parameter. Currently,
the following deploy keys are translated:
condition
and max_attempts
All other keys are ignored and produce a warning if present. You can review
the configuration that will be used to deploy by using the --compatibility
flag with the config
command.
Do not use this in production
We recommend against using
--compatibility
mode in production. The resulting configuration is only an approximate using non-Swarm mode properties, it may produce unexpected results.
In the majority of cases, moving from version 1 to 2 is a very simple process:
services:
key at the top.version: '2'
line at the top of the file.It's more complicated if you're using particular configuration features:
dockerfile
: This now lives under the build
key:
build:
context: .
dockerfile: Dockerfile-alternate
log_driver
, log_opt
: These now live under the logging
key:
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
links
with environment variables: environment variables created by
links, such as CONTAINERNAME_PORT
, ` have been deprecated for some time. In the new Docker network system,
they have been removed. You should either connect directly to the
appropriate hostname or set the relevant environment variable yourself,
using the link hostname:
web:
links:
- db
environment:
- DB_PORT=tcp://db:5432
external_links
: Compose uses Docker networks when running version 2
projects, so links behave slightly differently. In particular, two
containers must be connected to at least one network in common in order to
communicate, even if explicitly linked together.
Either connect the external container to your app's default network, or connect both the external container and your service's containers to an external network.
net
: This is now replaced by network_mode:
net: host -> network_mode: host
net: bridge -> network_mode: bridge
net: none -> network_mode: none
If you're using net: "container:[service name]"
, you must now use
network_mode: "service:[service name]"
instead.
net: "container:web" -> network_mode: "service:web"
If you're using net: "container:[container name/id]"
, the value does not
need to change.
net: "container:cont-name" -> network_mode: "container:cont-name"
net: "container:abc12345" -> network_mode: "container:abc12345"
volumes
with named volumes: these must now be explicitly declared in a
top-level volumes
section of your Compose file. If a service mounts a
named volume called data
, you must declare a data
volume in your
top-level volumes
section. The whole file might look like this:
version: "{{% param "compose_file_v2" %}}"
services:
db:
image: postgres
volumes:
- data:/var/lib/postgresql/data
volumes:
data: {}
By default, Compose creates a volume whose name is prefixed with your
project name. If you want it to just be called data
, declare it as
external:
volumes:
data:
external: true