Docker swarm — how to use stacks
Docker stacks allows to manage multi-container applications as a single unit
Docker stacks allows to manage multi-container applications as a single unit
Lets create the following file stacks_example.yml wich is actually a docker compose file
On the swarm manager enter$ docker stack deploy -c stack_example.yml simple
Creating network simple_default
Creating service simple_busybox
Creating service simple_web
the simple parameter is just a prefix of our stack services to identify them.
The command created a network which is used for the services to communicate each other and two services, one with the nginx web server and the other with a bash echoing some text in a loop.
Lets try some commands
The docker stack ls command lists the running stacks$ docker stack ls
NAME SERVICES ORCHESTRATOR
simple 2 Swarm
The docker stack ps gives more detailed information, like the state of each container of the stack and on which node it runsdocker stack ps simple
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ov2vuguozj2y simple_web.1 nginx:latest worker_node1 Running Running 7 minutes ago
zchlrbpm2lfw simple_busybox.1 radial/busyboxplus:curl worker_node2 Running Running 7 minutes ago
The docker stack services give us details about the replica statuses of the services$ docker stack services simple
ID NAME MODE REPLICAS IMAGE PORTS
7lg45bfg08pt simple_busybox replicated 1/1 radial/busyboxplus:curl
vyi4aegl388x simple_web replicated 1/1 nginx:latest
The docker service logs allows us to see logs of a specific servicedocker service logs simple_busybox
worker_node2 | Hello kostas!!
worker_node2 | Hello kostas!!
worker_node2 | Hello kostas!!
worker_node2 | Hello kostas!!
worker_node2 | Hello kostas!!
worker_node2 | Hello kostas!!
worker_node2 | Hello kostas!!
worker_node2 | Hello kostas!!
Lets scale up the busybox replicas to three
Open file stack_example.yml and do the following changes, add a deploy section under the busybox service with a value of replicas: 3version: '3'
services:
web:
image: nginx
busybox:
deploy:
replicas: 3
image: radial/busyboxplus:curl
command: /bin/sh -c "while true; do echo Hello!; sleep 10; done"
Now rundocker stack deploy -c stack_example.yml simple
Updating service simple_web (id: vyi4aegl388xayhkcbelwtust)
Updating service simple_busybox (id: 7lg45bfg08ptg3xu6e5an4l54)
Running docker stack services can verify that the replicas of busybox service are now 3$ docker stack services simple
ID NAME MODE REPLICAS IMAGE PORTS
7lg45bfg08pt simple_busybox replicated 3/3 radial/busyboxplus:curl
vyi4aegl388x simple_web replicated 1/1 nginx:latest
Another way to scale a service without changing the yml file is to use the command line$ docker service scale simple_busybox=1
simple_busybox scaled to 1
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
To delete a stack use docker stack rm$ docker stack rm simple
Removing service simple_busybox
Removing service simple_web
Removing network simple_default