What news from AWS re:Invent last week will have the most impact on you?
Amazon Q, an AI chatbot for explaining how AWS works.
Super-fast S3 Express storage.
New Graviton 4 processor instances.
Emily Freeman leaving AWS.
I don't use AWS, so none of this will affect me.
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
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 with the command:


In that file, paste the following:

Save and close the file.

Next, we’re going to create a requirements file with:

nano requirements.txt

In that file, add the following:

Save and close the file.

Now, we’ll create our Dockerfile with the command:

nano Dockerfile

In that file, paste the following contents:

Finally, create a docker-compose.yml file with:

nano docker-compose.yml

In that file, paste the following:

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:

Run it again, and the output will be:

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:

Let’s make sure it’s running on all nodes. Let’s say your nodes are on IP addresses, and Issue the commands:




You should output like this:

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.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.