Tutorial: Deploy a Full-Stack application on a Docker swarm

If you are looking to scale Docker application deployments, you should make sure to group a collection of nodes into a Docker Swarm. I already explained how to deploy a Docker Swarm (with persistent storage) in “Build a Docker Swarm with persistent storage using GlusterFS”. You don’t have to deploy the Swarm with persistent storage, but if you want to be able to retain your data (if something were to happen or you want to migrate the deployment), you’ll want to deploy the Swarm persistently.

Once your Docker Swarm is running, be sure to verify that all 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 can deploy to the stack. If not, you will need to figure out why until every 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 is a container image created specifically for this 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 docker service ls command, you should see the registry listed as such:

zhquhrodsirp   registry   replicated   1/1        registry:2   *:5000->5000/tcp

Note that your service ID will not be the same as the one you see above (the random string of characters in the first column). As you see it listed, you are good to go. You can also verify that the registry was deployed successfully by running the command:

curl http://localhost:5000/v2/

If the only output you see is {}everything is going as planned.

Create a sample application

Guess what we’re going to create? If you guessed “Hello World”, you’re right. Create a new directory to host the project with:

mkdir ~/swarmtest

Change to this new directory with:

cd ~/swarmtest

First, we will create a Python file, named app.py with the command:

nano app.py

In this file, paste the following:

Save and close the file.

Next, we will create a requirements file with:

nano requirements.txt

In this file, add the following:

Save and close the file.

Now we will create our Dockerfile with the command:

nano Dockerfile

In this file, paste the following content:

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

nano docker-compose.yml

In this 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 doing 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 could not be found, install it (on an Ubuntu-based distro) with:

sudo apt-get install docker-compose -y

Once the deployment is complete, test the application with:

curl http://localhost:8000

You should see something like this:

Run it again and the result will be:

Stop the application with the command:

docker-compose down --volumes

Deploy the app to Docker Swarm

For our next trick, we’re going to deploy the app to our Docker Swarm. Before doing so, we first need to push the newly generated image to our local registry with:

docker-compose push

At this point, our text image is available in our local registry and can be used to deploy to Swarm. We can deploy the stack with:

docker stack deploy --compose-file docker-compose.yml swarmtest

Verify that 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 192.168.1.60, 192.168.1.61, and 192.168.1.63. Run 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 come out like this:

Congratulations! You have just deployed a full stack application on a Docker Swarm. You can remove this stack with the command:

docker stack rm swarmtest

Done and done.

Band Created with Sketch.

Comments are closed.