The Docker stack

As it was pretty obvious from just a few paragraphs before, a manual setup of these services seems somewhat of a pain, so here we introduce a new tool that can help us do this much easier: Docker Stack. This tool uses a YAML file to get things to deploy all the services easily and repeatedly.

First we will clean up our old exercise before trying to use Docker stack configuration:

$ docker service ls -q | xargs docker service rm
pilssv8du68r
pue2ant1lg2u
swi95q7z38i2

$ docker network rm service_network
service_network

Now we can write our YAML configuration file--you can easily notice the parallels that the CLI has to this configuration file:

You can find more information about all the available options usable in Docker stack YAML files by visiting https://docs.docker.com/docker-cloud/apps/stack-yaml-reference/. Generally, anything you can set with the CLI commands, you can do the same with the YAML configuration.
version: "3"
services:
local-database:
image: local_database
networks:
- service_network
deploy:
replicas: 1
restart_policy:
condition: on-failure
volumes:
- database_volume:/data/db

application-server:
image: application_server
networks:
- service_network
depends_on:
- local-database
environment:
- DB_HOST=local-database
deploy:
replicas: 3
restart_policy:
condition: on-failure

web-server:
image: web_server
networks:
- service_network
ports:
- 8080:8080
depends_on:
- application-server
environment:
- APP_NAME=application-server
deploy:
replicas: 3
restart_policy:
condition: on-failure

networks:
service_network:

volumes:
database_volume:

What about starting our stack? That's easy too! Stack has almost the same commands as docker services:

$ docker stack deploy --compose-file swarm_application.yml swarm_test
Creating network swarm_test_service_network
Creating service swarm_test_local-database
Creating service swarm_test_application-server
Creating service swarm_test_web-server

$ # Sanity checks
$ docker stack ls
NAME SERVICES
swarm_test 3

$ docker stack services swarm_test
ID NAME MODE REPLICAS IMAGE PORTS
n5qnthc6031k swarm_test_application-server replicated 3/3 application_server
v9ho17uniwc4 swarm_test_web-server replicated 3/3 web_server *:8080->8080/tcp
vu06jxakqn6o swarm_test_local-database replicated 1/1 local_database

$ docker ps --format 'table {{.ID}} {{.Image}} {{.Ports}}'
CONTAINER ID IMAGE PORTS
afb936897b0d application_server:latest 8000/tcp
d9c6bab2453a web_server:latest 80/tcp, 8080/tcp
5e6591ee608b web_server:latest 80/tcp, 8080/tcp
c8a8dc620023 web_server:latest 80/tcp, 8080/tcp
5db03c196fda application_server:latest 8000/tcp
d2bf613ecae0 application_server:latest 8000/tcp
369c86b73ae1 local_database:latest 27017/tcp

If you go to http://127.0.0.1:8080 in your browser again, you will see that our app works just like before! We have managed to deploy our whole cluster worth of images with a single file on a Docker Swarm cluster!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.149.136