You can see the hands-on video here: https://blog.sofianeouafir.com/docker/video-hands-on-introduction-to-docker-stack
Docker Stack is an essential tool for managing Docker services across multiple nodes in a Docker Swarm using a docker-compose.yml file. In this tutorial, I dive into Docker Stack’s deployment capabilities and the secure management of secrets, using a practical example involving Drupal and PostgreSQL.
Here’s our docker-compose.yml
file that sets up the Drupal and PostgreSQL services:
version: '3.8' services: drupal: image: drupal:8.2 ports: - "8080:80" deploy: replicas: 2 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure
postgres: image: postgres:14 secrets: - psql-pw environment: POSTGRES_PASSWORD_FILE: /run/secrets/psql-pw POSTGRES_DB: drupal POSTGRES_USER: user deploy: placement: constraints: [node.role == manager]
secrets: psql-pw: external: true
The deploy
section of the Docker Compose file specifies deployment strategies in a swarm environment, including:
A Docker secret securely stores and manages sensitive data within Docker services, encrypted during transit and at rest, and only accessible by specifically authorized services.
Secrets can be managed in Docker Stack in two ways:
To deploy your stack in Docker Swarm, follow these steps:
Ensure Docker is running and connected to your Docker Swarm. Refer to this guide for setting up a 3-node Docker Swarm.
On your swarm node, initialize and edit the docker-compose file to inclue our own docker-compose file:
touch docker-compose.yml; vim docker-compose.yml;
echo "myverysecretpassword" | docker secret create psql-pw -
docker stack deploy -c docker-compose.yml demo-stack
docker stack services demo-stack
docker stack ps demo-stack
docker exec -it [container-name] bash
cat /run/secrets/psql-pw
docker service logs drupal docker service logs postgres
For example, you can scale up and down your services by editing the replicas values in your docker-compose file and re-deploy using the same deploy command.
Your drupal application should now be up and running on port 8080!
You have successfully configured a robust Docker Swarm with three nodes. This setup is now running efficient instances of Drupal and PostgreSQL, demonstrating Docker Stack’s powerful capabilities for real-world applications.
Happy deploying!