Skip to main content

Command Palette

Search for a command to run...

Docker Stack Explained: Simplify Deploying Multiple Services 🐋❤️

Part 7

Updated
4 min read

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

FeatureCompose Alone (Single Host)Compose with Stack (Swarm)
ScopeSingle host onlyMulti-host (Swarm cluster)
Version required2.x or 3.x3.x or higher
NetworksBridge / Host / CustomMust use overlay networks
VolumesSimple declarationMust specify type: volume for Stack
Deploy blockIgnoredUsed for replicas, placement, restart policy
Service namesAs written in Compose filePrefixed with <stackname>_ automatically
Port mappingQuotes optionalQuotes recommended ("8080:80")
Container namesCan be manually setStack auto-generates container names
BuildSupported locallyNot supported; image must exist on all nodes
Attachable networkNot relevantOptional (attachable: true) for standalone containers
Scalingdocker-compose up --scale works on hostdeploy.replicas in Compose for Stack
Multi-node communicationNot possibleFully 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