Skip to main content
Welcome to this comprehensive lesson on Docker Stacks. Previously, you learned how to run the Voting Application using Docker run and Docker Compose. In this lesson, we will enhance your deployment strategy by using Docker Stack to deploy the Voting Application across multiple nodes in a cluster.

Converting the Compose File

Begin by modifying your Docker Compose file to be compatible with Docker Stack. Update the file format to version 3 by specifying the version at the top and organizing the configuration under a services section. Below is the updated Compose file:
This file defines the following services:
  • Redis for caching.
  • PostgreSQL database (db).
  • Voting interface (vote) exposed on port 5000.
  • Backend worker to process votes.
  • Result service exposed on port 5001.
The images for vote, worker, and result are maintained in the Docker Samples repository on Docker Hub.
The image shows a Docker Hub profile page for "dockersamples," listing public repositories with their stars and pull counts.

Preparing the Application for Deployment

To prepare the application:
  1. Create a new directory (e.g., “voting-app-stack”) on your Docker host.
  2. Copy the updated Compose file into this directory and save it as docker-stack.yaml.
Once you have the file ready, deploy the stack with the following command, ensuring you use the same stack name (“voting-app-stack”):
When you deploy the stack, Docker creates a default network named voting-app-stack_default where all containers connect. Use the following command to verify the status of your services:
Initially, you may notice that the worker and vote services are missing one instance. This is normal as the deployment process scales up based on resource availability.
To inspect the status of a particular service, such as the vote service, run:
Once deployed, open your web browser and navigate to port 5000 to view the voting interface. Each vote is captured and processed, with results displayed through the result service.

Scaling the Voting Application

By default, each service starts with a single instance. For increased availability, you can scale the voting interface by deploying multiple replicas. To scale the vote service, update the Compose file to include a deploy parameter with the replicas setting under the vote service:
Save the changes and redeploy the stack using the same command. Docker Stack updates existing services rather than creating new ones:
Next, check the updated service list and replica counts:
To view detailed information about the vote service’s tasks, execute:
In this output, one task is running on node two while the other begins processing on node one.

Conclusion

The Voting Application is now successfully deployed using Docker Stack. This configuration not only simplifies deployment across multiple nodes but also provides an easy pathway for scaling services. Keep experimenting with Docker Stack to enhance your container orchestration skills. Happy Dockering!

Watch Video