Docker Swarm: Deploying Web Apps and Sites

You can use Docker Swarm to deploy fully isolated web applications in to your cloud infrastructure, without also having to configure a web server in it too.

Containers on a ship in a harbor.
Containers on a ship in a harbor. Source.

A couple of years ago, I’ve used LXC to do the same thing. However, over the years, I have experienced that Docker has more adoption and therefore more resources available online; including documentation and infrastructure. The reason I initially was hesitant to go with Docker was that I didn’t want to rely on a proprietary technology created by a single private company.

However, as there are related technologies like Kubernets and platforms by service providers like Google, Microsoft, and Amazon AWS that depend on Docker, I feel that Docker Inc (Who develops Docker.) won’t be able to close shop just because they feel like it. Even if they did, the big players would pick the project up. Plus, there’s the Open Container Initiative by The Linux Foundation, which has many big companies as members, including Docker Inc itself. This has given me confidence to adopt Docker.

I have been using Docker Swarm to run several projects of Sanmark Solutions PVT LTD and my own web sites. In fact, this very blog is running on the same infrastructure.

The following, is how I do it.

Creating a Docker Swarm

A group of whales.
A group of whales. Source.

First, we have to create a virtual machine in your choice of cloud infrastructure provider. My choice is DigitalOcean. If you would like to create an account there, consider using my referral link ; it will give you 10 USD free credit (Which you can use to run a VPS for 2 months for free!) when you sign up, and when you spend 25 USD in your account, it will give me 25 USD as a thanks.

I use the smallest level VPS available in DigitalOcean (They call them “Droplets”.), which costs 5 USD/month, which has following specs:

  • 1 GB RAM
  • 1 Virtual CPU
  • 25 GB SSD
  • 1 TB/month included transfer

Once you have a VPS, you can create a Docker Swarm in it following the instructions in You don’t have to setup Docker Machine; just SSH into your VPS and you can docker init. You don’t have to add more nodes either (Docker Swarm refers to the VPSes as “nodes”.); just the one is enough for now.

Deploying a Web App to Docker Swarm

Log in to the VPS, and create a directory named docker-deployments in the root directory. In it, create a directory with backward domain name for the service you wish to deploy, with dashes replacing periods. For an example, if you are going to deploy the application for, the directory name would be com-example.

Creating the docker-compose.yml

In the com-example directory, you have to create a docker-compose.yml file. It will contain all the services that our app needs. If our app is a WordPress installation, you’d probably need at least two services to run it: WordPress itself, and MySQL or MariaDB (Which is a clone of MySQL.). Docker Swarm calls a collection of services like this a stack.

Following is an example content for a docker-compose.yml file, with the essentials properties.

version: '3'
  image: wordpress:5.1.1-php7.1-apache
  - mariadb
  - ./volumes/wordpress/content:/var/www/html/wp-content
   WORDPRESS_DB_HOST: mariadb:3306
  - 8011:80
  image: mariadb:10.4.4
  - ./volumes/mariadb/data:/var/lib/mysql

I think you’d already know the basics of a docker-compose.yml, if you’re reading this, so I won’t waste time explaining every line. I’ll just describe some unique ones.

Line 8: As WordPress will be storing user uploaded content in its wp-content directory, we need to persist those files in it. Docker Containers are volatile, and in case there are multiple replicas of the service, they all need to share this directory in which user data would be stored. You will have to manually create the directory volumes/wordpress/content inside your com-example directory.

Line 10: Inside a Docker Swarm Stack, all the member Services of that Stack can call each other by just the name. No need to find internal IPs, which changes overtime anyways.

Line 17: Same as the Line 8. We need to persist the /var/lib/mysql directory of the MariaDB / MySQL container, in case the currently running one turns off.

Easy Re/deployment with a “Makefile”

To deploy or redeploy a Stack, you’ll have to type a quite long command, which is docker stack deploy -c docker-compose.yml --with-registry-auth com-example. As that is too long, we can shortcut it using a Makefile. Just create a file named Makefile in the com-example directory, and add the following code into it.

	docker stack deploy -c docker-compose.yml --with-registry-auth com-example

You’ll have to have the make program installed in your VPS as well. After that, from inside the com-example directory, run the command make deploy, and your longer command would be run.

Stopping a Docker Swarm Stack

To stop the Docker Swarm Stack we just deployed, just run the command docker stack rm com-example.


One response to “Docker Swarm: Deploying Web Apps and Sites”

Leave a Reply

Your email address will not be published. Required fields are marked *