VOOZH about

URL: https://thenewstack.io/tutorial-deploy-a-full-stack-application-to-a-docker-swarm/

⇱ Tutorial: Deploy a Full-Stack Application to a Docker Swarm - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-09-12 01:00:30
Tutorial: Deploy a Full-Stack Application to a Docker Swarm
tutorial,
Cloud Native Ecosystem / Containers

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

Here is everything you need to do to scale up an application across multiple nodes, using Docker Swarm.
Sep 12th, 2022 1:00am by Jack Wallen
👁 Featued image for: 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:

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:

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:

# 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:

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:

Hello NewStack! I have been seen 1 time.

Run it again, and the output will be:

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:

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:

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.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.