Deploy multiple sample stacks
In this step, you will learn how to deploy multiple stacks on your Docker Swarm. This demonstrates how you can manage different applications or services independently within the same swarm.
We will deploy two simple stacks: one using Nginx and another using Apache HTTP Server.
First, let's create a directory for the Apache stack definition. Navigate back to the ~/project
directory and create a new directory named my-apache-stack
.
cd ~/project
mkdir my-apache-stack
cd my-apache-stack
Now, create a file named docker-compose.yml
inside the my-apache-stack
directory using the nano
editor.
nano docker-compose.yml
Paste the following content into the docker-compose.yml
file:
version: "3.8"
services:
web:
image: httpd:latest
ports:
- "81:80"
deploy:
replicas: 1
restart_policy:
condition: on-failure
This docker-compose.yml
file defines a service named web
that uses the httpd:latest
image. It maps port 81 on the host to port 80 in the container to avoid port conflicts with the Nginx stack we will deploy later. It also specifies one replica and a restart policy.
Save the file by pressing Ctrl + X
, then Y
, and Enter
.
Before deploying, let's pull the httpd:latest
image.
docker pull httpd:latest
Now, deploy the Apache stack using the docker stack deploy
command. We will name this stack apache_stack
.
docker stack deploy -c docker-compose.yml apache_stack
This command deploys the Apache stack.
Next, let's go back to the my-nginx-stack
directory to deploy the Nginx stack again.
cd ~/project/my-nginx-stack
Deploy the Nginx stack using the docker stack deploy
command. We will name this stack nginx_stack
.
docker stack deploy -c docker-compose.yml nginx_stack
Now, let's verify that both stacks have been deployed. List the deployed stacks.
docker stack ls
You should see both apache_stack
and nginx_stack
listed.
You can also check the services for each stack individually.
docker stack services apache_stack
docker stack services nginx_stack
You should see the apache_stack_web
service and the nginx_stack_web
service, both running with 1/1 replicas.
You can access the Apache web server by opening a web browser and navigating to the IP address of your LabEx VM on port 81. You should see the default Apache welcome page. The Nginx web server should still be accessible on port 80.