Deploy a stack to a swarm (original) (raw)
When running Docker Engine in swarm mode, you can use docker stack deploy
to deploy a complete application stack to the swarm. The deploy
command accepts a stack description in the form of aCompose file.
Note
The
docker stack deploy
command uses the legacyCompose file version 3format, used by Compose V1. The latest format, defined by theCompose specificationisn't compatible with thedocker stack deploy
command.For more information about the evolution of Compose, seeHistory of Compose.
To run through this tutorial, you need:
- A Docker Engine running inSwarm mode. If you're not familiar with Swarm mode, you might want to readSwarm mode key conceptsandHow services work.
Note
If you're trying things out on a local development environment, you can put your engine into Swarm mode with
docker swarm init
.If you've already got a multi-node swarm running, keep in mind that all
docker stack
anddocker service
commands must be run from a manager node. - A current version ofDocker Compose.
Because a swarm consists of multiple Docker Engines, a registry is required to distribute images to all of them. You can use theDocker Hub or maintain your own. Here's how to create a throwaway registry, which you can discard afterward.
- Start the registry as a service on your swarm:
- Check its status with
docker service ls
:
Once it reads1/1
underREPLICAS
, it's running. If it reads0/1
, it's probably still pulling the image. - Check that it's working with
curl
:
The app used in this guide is based on the hit counter app in theGet started with Docker Compose guide. It consists of a Python app which maintains a counter in a Redis instance and increments the counter whenever you visit it.
- Create a directory for the project:
- Create a file called
app.py
in the project directory and paste this in: - Create a file called
requirements.txt
and paste these two lines in: - Create a file called
Dockerfile
and paste this in: - Create a file called
compose.yaml
and paste this in:
The image for the web app is built using the Dockerfile defined above. It's also tagged with127.0.0.1:5000
- the address of the registry created earlier. This is important when distributing the app to the swarm. - Start the app with
docker compose up
. This builds the web app image, pulls the Redis image if you don't already have it, and creates two containers.
You see a warning about the Engine being in swarm mode. This is because Compose doesn't take advantage of swarm mode, and deploys everything to a single node. You can safely ignore this. - Check that the app is running with
docker compose ps
:
You can test the app withcurl
: - Bring the app down:
To distribute the web app's image across the swarm, it needs to be pushed to the registry you set up earlier. With Compose, this is very simple:
The stack is now ready to be deployed.
- Create the stack with
docker stack deploy
:
The last argument is a name for the stack. Each network, volume and service name is prefixed with the stack name. - Check that it's running with
docker stack services stackdemo
:
Once it's running, you should see1/1
underREPLICAS
for both services. This might take some time if you have a multi-node swarm, as images need to be pulled.
As before, you can test the app withcurl
:
With Docker's built-in routing mesh, you can access any node in the swarm on port8000
and get routed to the app: - Bring the stack down with
docker stack rm
: - Bring the registry down with
docker service rm
: - If you're just testing things out on a local machine and want to bring your Docker Engine out of Swarm mode, use
docker swarm leave
: