Tutorial: Deploy a Full-Stack Application to a Docker Swarm

If you’re looking to scale Docker app deployments, you’ll want to make sure to cluster a collection of nodes together into a Docker Swarm. I’ve already discussed how to deploy a Docker Swarm (with persistent storage) in “Create a Docker Swarm with Persistent Storage Using GlusterFS.” You don’t necessarily have to deploy the Swarm with persistent storage, but if you want to be able to retain your data (should something happen or you want to migrate the deployment), you’ll want to deploy the Swarm with persistence.
Once you have your Docker Swarm running, make sure to verify all the nodes are connected and ready by running the command (on the controller):
docker node ls
In the output, you should see something like this:
tpsl7enzswhkeef3dh8uswkxp * docker1 Ready Active Leader 20.10.17
xnye548afhe1hc832kulh5sui docker2 Ready Active 20.10.17
cammaze2fcfcomjpdo0fwz105 docker3 Ready Active 20.10.17
If all nodes are listed as Ready and Active, you’re okay to deploy to the stack. If not, you’ll need to troubleshoot why until each node is listed as such.
Deploy a Local Registry
With the Swarm up and running, your next task is to deploy a local Docker registry. Fortunately, there’s a container image that was created specifically for that purpose. On the Docker Swarm controller node, deploy the registry with the command:
docker service create --name registry --publish published=5000,target=5000 registry:2
If you issue the command docker service ls, you should see the registry listed as such:
zhquhrodsirp registry replicated 1/1 registry:2 *:5000->5000/tcp
Note that the ID of your service will not be the same as you see above (the random string of characters in the first column). As you see it listed, you’re good to go. You can also verify that the registry was successfully deployed by issuing the command:
curl http://localhost:5000/v2/
If the only output you see is {}, everything is running as expected.
Create a Sample Application
Guess what we’re going to create? If you guessed “Hello World,” you are correct. Create a new directory to house the project with:
mkdir ~/swarmtest
Change into that new directory with:
cd ~/swarmtest
First, we’re going to create a Python file, named app.py with the command:
nano app.py
In that file, paste the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
from flask import Flask from redis import Redis app = Flask(__name__) redis = Redis(host='redis', port=6379) @app.route('/') def hello(): count = redis.incr('hits') return 'Hello New Stack! I have been seen {} times.\n'.format(count) if __name__ == "__main__": app.run(host="0.0.0.0", port=8000, debug=True) |
Save and close the file.
Next, we’re going to create a requirements file with:
nano requirements.txt
In that file, add the following:
1 2 |
flask redis |
Save and close the file.
Now, we’ll create our Dockerfile with the command:
nano Dockerfile
In that file, paste the following contents:
1 2 3 4 5 6 |
# syntax=docker/dockerfile:1 FROM python:3.4-alpine ADD . /code WORKDIR /code RUN pip install -r requirements.txt CMD ["python", "app.py"] |
Finally, create a docker-compose.yml file with:
nano docker-compose.yml
In that file, paste the following:
1 2 3 4 5 6 7 8 9 10 |
version: "3.9" services: web: image: 127.0.0.1:5000/swarmtest build: . ports: - "8000:8000" redis: image: redis:alpine |
Save and close the file.
Deploy the App
With all the pieces in place, we can now deploy the stack to our Docker Swarm. However, before we do that, let’s test it to make sure it works with:
docker-compose up -d
If you get the error that the docker-compose command isn’t found, install it (on an Ubuntu-based distribution) with:
sudo apt-get install docker-compose -y
Once the deployment is complete, test the app with:
curl http://localhost:8000
You should see something like this:
1 |
Hello NewStack! I have been seen 1 time. |
Run it again, and the output will be:
1 |
Hello NewStack! I have been seen 1 time. |
Take down the app with the command:
docker-compose down --volumes
Deploy the App to Docker Swarm
For our next trick, we’ll deploy the app to our Docker Swarm. Before we do, we must first push the newly generated image to our local registry with:
docker-compose push
At this point, our text image is available to our local registry and can be used to deploy to the Swarm. We can deploy the stack with:
docker stack deploy --compose-file docker-compose.yml swarmtest
Verify the stack is running with:
docker stack services swarmtest
The output of the above command should look like this:
1 2 |
ocfddicaivol swarmtest_redis replicated 1/1 redis:alpine hbfv50ayxwrs swarmtest_web replicated 1/1 127.0.0.1:5000/swarmtest:latest *:8000->8000/tcp |
Let’s make sure it’s running on all nodes. Let’s say your nodes are on IP addresses 192.168.1.60, 192.168.1.61 and 192.168.1.63. Issue the commands:
curl http://192.168.1.60:8000
curl http://192.168.1.61:8000
curl http://192.168.1.63:8000
You should output like this:
1 2 3 |
Hello New Stack! I have been seen 1 times. Hello New Stack! I have been seen 2 times. Hello New Stack! I have been seen 3 times. |
Congratulations! You’ve just deployed a full stack application to a Docker Swarm. You can take that stack down with the command:
docker stack rm swarmtest
Done and done.