Docker Stack Explained: Simplify Deploying Multiple Services 🐋❤️
Part 7
Docker Stack is basically the power of Docker Compose + Docker Swarm combined.
Docker Compose lets you run multiple services (like frontend, backend, database, etc.) on a single host.
Docker Swarm lets you run a single service across multiple hosts for high availability and scaling.
Docker Stack does both it lets you deploy multiple services across multiple hosts in a Swarm cluster.

We already know how to:
write Docker Compose files
set up Docker Swarm on servers
Now we just use both together at the same time.
That’s exactly what Docker Stack is
Let’s do it practically 😊
Set up 3 servers for Docker Swarm:
1 Manager Node
2 Worker Nodes
Install Docker on each of the three servers and start the Docker service.
yum install docker -y && systemctl start docker
Install docker compose on manager node
vim .bashrc
export PATH=$PATH:/usr/local/bin/
source .bashrc
# Download the current stable release of Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Apply executable permissions
sudo chmod +x /usr/local/bin/docker-compose
# Create a symbolic link
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# Install using pip (Python package manager)
sudo yum install -y python3-pip
sudo pip3 install docker-compose
#check version
docker-compose --version
Initialize Docker Swarm on the manager node to enable swarm mode.
docker swarm init

Now, copy the generated token and paste it on the worker nodes to join them to the swarm and form a cluster.

Create a Docker Compose file to define the services we need to set up.
---
version: "3.9"
services:
myservice1:
image: shaikmustafa/cycle
ports:
- "1111:80"
deploy:
replicas: 4
networks:
- mynetwork1
myservice2:
image: shaikmustafa/dm
ports:
- "2222:80"
deploy:
replicas: 5
volumes:
- type: volume
source: myvolume2
target: /rama/code
networks:
- mynetwork1
volumes:
myvolume2:
driver: local
networks:
mynetwork1:
driver: overlay
attachable: true
...
Things to remember while writing compose file with stack
| Feature | Compose Alone (Single Host) | Compose with Stack (Swarm) |
| Scope | Single host only | Multi-host (Swarm cluster) |
| Version required | 2.x or 3.x | 3.x or higher |
| Networks | Bridge / Host / Custom | Must use overlay networks |
| Volumes | Simple declaration | Must specify type: volume for Stack |
| Deploy block | Ignored | Used for replicas, placement, restart policy |
| Service names | As written in Compose file | Prefixed with <stackname>_ automatically |
| Port mapping | Quotes optional | Quotes recommended ("8080:80") |
| Container names | Can be manually set | Stack auto-generates container names |
| Build | Supported locally | Not supported; image must exist on all nodes |
| Attachable network | Not relevant | Optional (attachable: true) for standalone containers |
| Scaling | docker-compose up --scale works on host | deploy.replicas in Compose for Stack |
| Multi-node communication | Not possible | Fully supported via overlay networks |
To deploy compose using stack
docker stack deploy mysite -c=compose.yaml
To get the service list
docker service ls
To get the containers for service
docker service ps myservice1
To get the config of stack
docker stack config -c=compose.yaml
To delete the stack
docker stack rm mysite
To get list of services we created for a stack
docker stack services mysite
To get containers of a service
docker service ps myservice1
To inspect a service
docker service inspect myservice1
To scale in/scale out for a service
docker service scale myservice1=5
Conclusion
Docker Stack combines the power of Docker Compose and Docker Swarm, enabling the deployment of multiple services across a Swarm cluster. It provides high availability, scalability, and easy management of services on multiple hosts. This makes orchestrating complex applications simpler and more efficient. Whether for a small app or a large microservices architecture, Docker Stack offers the flexibility and reliability needed for modern deployments.
If you found this guide useful, please like ❤️, comment, and share your thoughts or questions. Stay tuned for the next part, where we'll explore advanced Docker concepts and practices.
Thank you,
Yours, Rama Grandhi