5. Stacks
Stacks
Group of interrelated services that share dependencies, and can be orchestrated and scaled together.
Inter-related services all running in concert.
A single stack is capable of defining and coordinating the functionality of an entire application (though very complex applications may want to use multiple stacks).
Add a new service and redeploy
It’s easy to add services to our docker-compose.yml
file. First, let’s add a free visualizer service that lets us look at how our swarm is scheduling containers.
Open up
docker-compose.yml
in an editor and replace its contents with the following:version: "3" services: web: # replace username/repo:tag with your name and image details image: username/repo:tag deploy: replicas: 5 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50M ports: - "80:80" networks: - webnet visualizer: image: dockersamples/visualizer:stable ports: - "8080:8080" volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] networks: - webnet networks: webnet:
The only thing new here is the peer service to
web
, namedvisualizer
. Notice two new things here: avolumes
key, giving the visualizer access to the host’s socket file for Docker, and aplacement
key, ensuring that this service only ever runs on a swarm manager -- never a worker. That’s because this container, built from an open source project created by Docker, displays Docker services running on a swarm in a diagram.We talk more about placement constraints and volumes in a moment.
Make sure your shell is configured to talk to
myvm1
(full examples are here).Run
docker-machine ls
to list machines and make sure you are connected tomyvm1
, as indicated by an asterisk next to it.If needed, re-run
docker-machine env myvm1
, then run the given command to configure the shell.On Mac or Linux the command is:
eval $(docker-machine env myvm1)
On Windows the command is:
& "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env myvm1 | Invoke-Expression
Re-run the
docker stack deploy
command on the manager, and whatever services need updating are updated:$ docker stack deploy -c docker-compose.yml getstartedlab Updating service getstartedlab_web (id: angi1bf5e4to03qu9f93trnxm) Creating service getstartedlab_visualizer (id: l9mnwkeq2jiononb5ihz9u7a4)
Take a look at the visualizer.
You saw in the Compose file that
visualizer
runs on port 8080. Get the IP address of one of your nodes by runningdocker-machine ls
. Go to either IP address at port 8080 and you can see the visualizer running:The single copy of
visualizer
is running on the manager as you expect, and the 5 instances ofweb
are spread out across the swarm. You can corroborate this visualization by runningdocker stack ps <stack>
:docker stack ps getstartedlab
The visualizer is a standalone service that can run in any app that includes it in the stack. It doesn’t depend on anything else. Now let’s create a service that does have a dependency: the Redis service that provides a visitor counter.
Last updated