What’s new in Docker Compose v3
In this
blog, we’ll discuss new features added in Docker Compose v3. Docker
compose added the feature of stack deployment with swarm.
To achieve this, we need to create a file named, docker-stack.yml and specify the deploy key, which would allow you to specify various properties of a swarm deployment in docker-stack.yml file.
For deploying this, you simply need to run the
docker-stack deploy
command with this docker-stack.yml
file to pull the referenced images and launch the services in a swarm as configured in the .yml
.deploy key
The
deploy
key allows you to specify configuration related to the deployment and running of services. The deploy
key can also be used to constrain some services to run only on the manager node.
Specify
configuration related to the deployment and running of services. This
only takes effect when deploying to a swarm cluster with `docker stack
deploy` command and is ignored by
docker-compose up
and docker-compose run
commands.deploy:
mode: replicated
replicas: 5
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
Several sub-options available are mentioned below:
mode
mode is specified either as
global
(exactly one container per swarm node) or replicated
(a specified number of containers). Default is replicated
.mode: global
replicas
To specify the number of containers that should be running at any given time.
replicas: 5
placement
To specify the placement constraints.
placement:
constraints:
- node.role == manager
- engine.labels.operatingsystem == ubuntu 16.04
update_config
To configure how the service should be updated. Useful for configuring rolling updates.
parallelism
: The number of containers to update at a time.delay
: The time to wait between updating a group of containers.failure_action
: What to do if an update fails. Eithercontinue
orpause
Default ispause
.
update_config: parallelism: 2 delay: 10s
resources
To configure resource constraints. This replaces the older resource constraint options in Compose files prior to version 3 (
cpu_shares
, cpu_quota
, cpuset
, mem_limit
, memswap_limit
).resources:
limits:
cpus: '1.5'
memory: 500M
reservations:
cpus: '1.0'
memory: 200M
restart_policy
To configure whether and how to restart containers when they exit.
condition
: One ofnone
,on-failure
orany
. Default is any.delay
: How long to wait between restart attempts. Default is 0.max_attempts
: How many times to attempt to restart a container before giving up. Default is never give up.window
: How long to wait before deciding if a restart has succeeded. Default is decide immediately.
restart_policy: condition: on-failure delay: 5s max_attempts: 3 window: 120s
labels
To specify labels for the service. These labels will be set only on the service, and not on any containers for the service.
version: "3"
services:
web:
image: web
deploy:
labels:
com.example.description: "This label will appear on the web service"
To set labels on containers instead, use the
labels
key outside of deploy
:version: "3"
services:
web:
image: web
labels:
com.example.description: "This label will appear on all containers for the web service"
“docker stack deploy” command
docker stack deploy
command (supported only by compose v3) is used to deploy with docker-stack.yml
.
This command does not support the
build
key supported in Compose files, which builds based on a Dockerfile. You need to use pre-built images with docker stack deploy