Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Deploying and Managing Services at Scale with Docker Swarm
91%

Docker Swarm Stacks

A stack is a group of interrelated services that share dependencies and can be orchestrated and scaled together. Here are some examples:

  • A WordPress stack that includes a WordPress service, Apache, and a MySQL service.
  • A web application stack that includes a web application service, a caching service like Redis, and a database service like PostgreSQL.
  • A monitoring stack that includes Prometheus, Loki, and Grafana services.
  • And so on.

One of the main advantages of Docker Swarm is its ability to deploy using a Docker Compose file (docker-compose.yml). In Docker Swarm, this file is referred to as a stack.

Let's start with a basic example. Create a Compose file for a WordPress application that includes a MySQL database. Here is what we're going to start with:

cat < docker-compose.yml
version: '3.9'

services:
   db:
     image: mysql:9.6.0
     volumes:
       - db_data:/var/lib/mysql
     environment:
       MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD_FILE: /run/secrets/db_password
     secrets:
       - db_root_password
       - db_password
     networks:
       - wordpress-network

   wordpress:
     depends_on:
       - db
     image: wordpress:6.9.1-apache
     ports:
       - "8000:80"
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD_FILE: /run/secrets/db_password
     secrets:
       - db_password
     networks:
       - wordpress-network

networks:
   wordpress-network:
     driver: overlay

secrets:
   db_password:
     file: .password.txt
   db_root_password:
     file: .root_password.txt

volumes:
    db_data:
EOF

Remove all previous services to free up resources and ports:

docker service rm $(docker service ls -q)

At this level, nothing new; we've seen all these configurations in the previous chapters. We're going to use secrets to store the database passwords. So we need to create two files: .root_password.txt and .password.txt and add the passwords to these files:

echo "your_db_root_password" > .root_password.txt
echo "your_db_wordpress_password" > .password.txt

Now, if you were working in your development environment, you could run the following command to start the stack docker compose up -d, but since our goal is to deploy the stack on a Docker Swarm cluster, we need to run the following command:

docker stack deploy -c docker-compose.yml my-stack

Now, we can check the services:

docker service ls

You should see that both my-stack_db and my-stack_wordpress are running.

Note: In Docker Swarm, depends_on in a Compose file doesn't wait for a service to be healthy before starting dependent services - it only expresses an order. To ensure a service is ready before others rely on it, use healthchecks, restart policies, update_config, or implement application-level retries.

That's it! You have successfully deployed your first stack on Docker Swarm.

Now let's learn how to add constraints to the services. For instance, if we want to deploy the WordPress service on the worker nodes and the MySQL service on the manager node, we need to add the following constraints to the services:

  • WordPress: node.role==worker
  • MySQL: node.role==manager

Here is the updated docker-compose.yml file:

cat < docker-compose.yml
version: '3.9'

services:
   db:
     image: mysql:9.6.0
     volumes:
       - db_data:/var/lib/mysql
     environment:
       MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD_FILE: /run/secrets/db_password
     secrets:
       - db_root_password
       - db_password
     networks:
       - wordpress-network
     deploy:
       placement:
         constraints:
           - node.role==manager

   wordpress:
     depends_on:
       - db
     image: wordpress:6.9.1-apache
     ports:
       - "8000:80"
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More