Docker Swarm is a popular infrastructure tool for running websites and web applications online. Here’s how to deploy WordPress on Docker Swarm.

A journal.
A journal. Source.

This post extends https://blog.budhajeewa.com/docker-swarm-deploying-web-apps-and-sites/. We’ll refer to that as the “parent post” in this post.

Setup Docker Swarm

Set up a Docker Swarm as explained in the parent post.

That post had an example that showed how a WordPress installation was done on Docker Swarm, but the focus of the post was introducing Docker Swarm; in this post, we’ll focus on setting up a WordPress installation on an already running Docker Swarm. So, if you have created a Stack based on that post, stop it, and delete the directory created for that Stack.

Setup the WordPress Stack

Assume the domain we’re going to use for your WordPress installation is example.com, following the naming schema introduced in the parent, create a directory called /docker-deployments/com-example.

Feel free to change occurrences of com-example in this post with your own reversed domain name.

Create a file named Makefile in it, and add the following content.

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

Then create a file named docker-compose.yml and add the following content.

version: '3'
services:
 wordpress:
  image: wordpress:5.1.1-php7.1-apache
  depends_on:
  - mariadb
  volumes:
  - ./volumes/wordpress/content:/var/www/html/wp-content
  environment:
   WORDPRESS_DB_HOST: mariadb:3306
   WORDPRESS_DB_PASSWORD: root
  ports:
  - 8001:80
 mariadb:
  image: mariadb:10.4.4
  volumes:
  - ./volumes/mariadb/data:/var/lib/mysql
  environment:
   MYSQL_ROOT_PASSWORD: root

In this docker-compose.yml, which defines the Stack for our WordPress installation, we’ve specified two services: WordPress itself and MariaDB, which will be the database management system used for WordPress.

For WordPress, we’re using the Docker Image wordpress:5.1.1-php7.1-apache, which is one of the editions available in Docker Hub for WordPress’ latest version. You can browse all the Images for WordPress in https://hub.docker.com/_/wordpress.

Likewise, we’re using mariadb:10.4.4 for MariaDB. Its Docker Hub page is https://hub.docker.com/_/mariadb.

We specify that the wordpress service will depend on mariadb.

We mount a persistent volume into /var/www/html/wp-content, for the reasons described in the parent post.

Then the environment variables for wordpress are declared. We only need to set WORDPRESS_DB_HOST and WORDPRESS_DB_PASSWORD for it to run properly.

Then we map the port 8011 of the host to the port 80 of the wordpress service.

For mariadb, we mount a persistent volume to /var/lib/mysql.

For mariadb, the environment variable MYSQL_ROOT_PASSWORD is the only one required to be set.

Map a Domain to the WordPress Installation

You’ll need to install NGINX, as we’re going to use it as a reverse proxy.

Just an engine. Not NGINX.
Just an engine. Not NGINX. Source.

Inside /etc/nginx/sites-available, create a file named com-exampleand add the following content into it.

server {
        listen 80;
        server_name example.com;

        location / {
                proxy_pass http://host.public.ip.address:8001;
                proxy_set_header Host $host;
        }
}

Make a symlink to the new site configuration file in /etc/nginx/sites-enabled directory.

sudo ln -s ../sites-available/com-example .

Then restart Nginx.

sudo service nginx restart

If everything worked as expected, you have now successfully deployed WordPress on Docker Swarm, and you should be able to view your WordPress installation in example.com.