Putting Multiple WordPress Containers into Production – Proxy Container

written by William Patton on March 28, 2017 in Docker with 2 comments

This is follow on to my adventures putting Docker containers into production. The previous article covered building WordPress containers for production. This article deals with how you would run multiple instances through a proxy on a single host.
I discovered running multiple WordPress sites using separate Docker containers in production wasn’t hard at all once you had an overview of the basic ideas involved. To break it down simply – WordPress sites and their databases run in a group that is started with docker-compose. They should not expose any ports to the Host OS. Instead users can connect to it through a separate proxy container, capable of connecting to several WordPress containers, which does expose its ports on the host.

The proxy allows us to run multiple WordPress containers on the same machine, and for each to bind to the ports they desire on their own private – docker assigned – IP address, without causing port collisions on the Host OS.

Things To Know Before Starting

The ideas explored here to run multiple applications behind a proxy could extend past WordPress to any kind of standard app that runs on port 80 or 443 and responds to http requests. By the end of the article you will have multiple WordPress instances running through a reverse proxy and the ability to add more just a few moments.

The prerequisites to follow this guide to power WordPress sites are:

  • At least 1 WordPress Instance you would like to run, preferably 2 or more.
  • A domain, or subdomain, you can point to the site and access to an external DNS that you can point them from.
  • A Docker Host. It needs to have ports 80 and 443 free to run on default configurations.

A few other things to note is that the proxy group will reside in its own directory, each WordPress instance will have its own directory also.

Your hosting box could be anything capable of running Docker on, mine is Ubuntu 16.04 with the latest Docker Engine. I covered Installing Docker on Ubuntu in another article about. It is not a fresh box but I have made sure to free ports 80 and 443 so I can let the proxy bind and expose them on the Host OS.

The overarching idea of this proxy through docker idea is easy to understand when visualized.

Users connect to the host machine on the ports they expect – either 80 or 443. Those connections are bridged right through to the proxy container. That proxy knows how to talk to the WordPress containers running behind it.

Container Groups To Be Run In Production

There are 2 main groups of containers that are to be run on the production server.

  • The proxy group. The contains an instance of NGINX, an image to write config files and an image to get letsencrypt certificates.
  • The wordpress group. This was covered in the previous article. It contains WordPress, served by apache using PHP7.1 with memcached available. There’s also a database container built with the MariaDB image and an image for running WP-CLI.

The Reverse Proxy Container

The container that will act as a proxy is a specially configured NGINX service. It uses a proxy configuration as the default vhost so once your setup adding additional domains and servers is quick and easy.

It is tweaked to work as a proxy by default and we will mount a series of volumes where it can store and access it’s configuration files. This volume configuration is crucial as it makes it possible to update configuration files on the fly through another separate container in the group.

In the same container group we’ll be loading another image called docker-gen and a 3rd that is the companion LetsEncrypt image. The LetsEncrypt image automatically gets SSL certificates for sites running through the proxy. This way the proxy can secure the connection between it and the end-user.

Docker-gen is a clever little image. While limited in the scope of what it does it is extremely useful. All it is does is write files based on a template. It fills that template with information from environment variables of containers you start and other information in queries about the container. It’s used here to build configurations files with the correct domains and forwarding addresses and to add the references to the domain certificate to ensure secure connections.

Make a Network – Communication isn’t Hard

By default docker-compose puts container groups into their own private network and bridges it on the host. The NGINX image has ways of working within this but for the sake of ease we’ll negate this issue by placing NGINX and the WordPress containers on the same private network. All of our containers will run on this same network.

Before starting any containers make the network (you only have to do this once and you can run the command from any directory).

docker network create nginx-proxy

The NGINX-proxy image

The first group of images we want to get running is the proxy and the rest of the supporting containers.

A good starter compose file is present in the repo of the proxy image that I used. Clone it and enter the directory. You will see there are 2 different docker compose files. We’ll be using the compose file for separate containers. Remove the existing docker-compose.yml file (it is for a single container with docker-gen embeded) and rename the file for separate containers docker-compose.yml.

cp docker-compose.yml docker-compose-single-container.yml
rm docker-compose.yml
cp docker-compose-separate-containers.yml docker-compose.yml
nano docker-compose.yml

Edit the compose file so it matches this:

version: '2'
services:
  nginx:
    image: nginx
    container_name: nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/nginx/conf.d
      - /etc/nginx/vhost.d
      - /usr/share/nginx/html
      - ./certs:/etc/nginx/certs:ro

  dockergen:
    image: jwilder/docker-gen
    container_name: dockergen
    command: -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    volumes_from:
      - nginx
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro

  whoami:
    image: jwilder/whoami
    environment:
      - VIRTUAL_HOST=whoami.local

  nginx-letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
#    environment:
#      ACME_CA_URI: https://acme-staging.api.letsencrypt.org/directory
    container_name: nginx-letsencrypt
    volumes_from:
      - nginx
    volumes:
      - ./certs:/etc/nginx/certs:rw
      - /var/run/docker.sock:/var/run/docker.sock:ro

networks:
  default:
    external:
      name: nginx-proxy

There are 3 sets of modifications compared to the original.

  1. At the end I’ve configured it to use the nginx-proxy network.
  2. I added the nginx-letencrypt service.
  3. I am mounting some additional volumes in some of the services. Pay attention to what is mounted and where.

If you are testing you can bypass the 5 certs/per week/per domain rule and still use letsencrypt and SSL during testing – just uncomment the environment variables in the nginx-lestencrypt service.

Before you start-up the server note that the proxy will try bind to ports and expose them on the Host OS. Those ports need to be available for use on the public facing network interface.

On first run the image for grabbing certificates needs to generate a diffie-hellman group file used in key generation. That may take a few minutes but it’s a one time run thing so let it generate.

# from inside the directory of your nginx-proxy group
docker-compose up

Keep this terminal open so you can see the output when you start the next set of containers.

Note about DNS

Undoubtedly we could handle DNS with another group of containers but I am going to handle the DNS externally, in the way I am used to. Many hosting and domain providers have a DNS service you can access. Handle the DNS however you choose but the domain is going to need to point to IP address of the machine that is acting as the Docker host.

The WordPress Container

The next thing to do is get a docker-compose file together for running WordPress sites.

The WordPress image I’m using here I built with memcached support running PHP7.1. Guides for building that image is in the preview article about building WordPress containers for production. There is also another custom image here for adding WP-CLI support.

When you start these containers with compose it will pull the images, pre-built, from the Docker Hub on the production server.

version: '2'

services:

  wordpress:
    image: pattonwebz/wordpress-php7-1-apache-memcached
    ports:
      - 80
    environment:
      WORDPRESS_DB_USER: database
      WORDPRESS_DB_PASSWORD: kgB7yJCwGYq2jeQH
      WORDPRESS_DB_NAME: wp_database
      WORDPRESS_TABLE_PREFIX: wp_
      WORDPRESS_DB_HOST: mysql:3306
      VIRTUAL_HOST: example.com,www.example.com
#      VIRTUAL_PROTO: https
      LETSENCRYPT_HOST: example.com,www.example.com
      LETSENCRYPT_EMAIL: user@example.com
    volumes:
      - data_volume:/var/www/html
      - ./home/wp:/home/wp

  mysql:
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_RANDOM_ROOT_PASSWORD: "yes"
      MYSQL_DATABASE: wp_database
      MYSQL_USER: database
      MYSQL_PASSWORD: kgB7yJCwGYq2jeQH
    volumes:
      - db_data:/var/lib/mysql
      - ./homes/db:/home/db

  wp:
    image: pattonwebz/docker-wpcli
    volumes_from:
      - wordpress
    links:
      - mysql:mysql
    entrypoint: wp
    command: "--info"

volumes:
    db_data:
    data_volume:

networks:
  default:
    external:
      name: nginx-proxy

Make sure that a unique password is replaced in the file for the DB user. Because this file contains a plaintext password it needs to be stored in a place that is secure. When running this in swarm mode you are able to use docker-secrets for this. I’ll cover that another time when I explore putting containers like this into a swarm.

The main difference between this file and the one I originally built is the addition of a network (it’s the same network as used in the nginx-proxy image) and 4 lines in the WordPress service. These 4 lines are used in combination with the proxy container.

      VIRTUAL_HOST: example.com,www.example.com
#      VIRTUAL_PROTO: https
      LETSENCRYPT_HOST: example.com,www.example.com
      LETSENCRYPT_EMAIL: user@example.com

The LETSENCRYPT_HOST should be the same as the VIRTUAL_HOST – which will be your domain name. Also add the correct email address you want tied to the certificate. You can add multiple hosts, here we have the root and www. subdomains.

The default connection method from nginx-proxy works with that – without any additional configuration, so long as the application you’re proxying runs on ports 80 or 443. If you had a base image that served through https only then you could instruct the nginx-proxy to connect to the backend with https by uncommenting the VIRTUAL_PROTO: https line.

Starting A WordPress Container in Production

The above docker-compose.yml file is enough to get a fresh WordPress site running ready to have a username added and perform the web-based install. It handles data persistence between sessions using volumes and has a local directory mounted inside the containers home directories to make passing files between the container and the Host OS painless.

So long as you have the nginx-proxy group of containers running when you start a container it’ll check for the environment variables added vibrating VIRTUAL_HOST etc and docker-gen will write it’s config files. If the LETSENCRYPT environment variables are set it’ll queue it for a certificate check and fetch. If a change has occurred to the config’s then a restart will be triggered on NGINX.

# from inside your WordPress instance folder
docker-compose up

If you watch the proxy group terminal while the WordPress containers start you will see several messages about it detecting new containers. There will be lines about containers starting and stopping. Not all of the containers need action so don’t worry that most of them out stopped status.

The output from the wordpress group on first run will show the container has to grab an archive containing WordPress, unpack it, and also create it’s database. This takes a few moments, and you may see some MySQl connection errors during the start-up. Next time will be a lot faster.

NOTE: That if your site doesn’t already point to the domain you are running the site for then letsencrypt certificate challenge will fail and no certificate will be issued for the site.

If your domain is already pointed at the correct IP you can access the site right now at its domain. It’s live.

If not you could add an entry to your local hosts file and point it there. For example IP 192.0.1.251 at domain example.com would look something like this:

192.0.1.251 example.com
192.0.1.251 www.example.com

Configuring the WordPress instance – A Fresh Site

Let’s say you were starting a fresh site. The quickest way to get this running is by using the included WP-CLI tool available through each groups wp service.

If you enter the directory of a WordPress instance you can issue WP-CLI commands as if you were local inside the container. You are able to run any WP-CLI command you want – you just need to prefix with docker-compose run --rm .

After the prefix you can run commands exactly like you would usually. The service is named wp, which matches the name usually give to the WP-CLI php executable when it’s added to a user’s PATH.

# this will print the info wp-cli usually gives so you can confirm it works working
docker-compose run --rm wp
# this will reset the database
docker-compose run --rm wp db reset --yes

You can enter this series of commands at the terminal sequentially or add them to a script that can run them all with a single command. I suggest you use a script for the sake of efficiency. A script like this does a lot of things. It first resets the database and configures a fresh instance of WordPress – based on the values provided. It then runs a core update, gets some plugins and a theme then does some work to regenerate images and the permalink structure.

#!/bin/bash

docker-compose run --rm wp db reset --yes
# change values on this line
docker-compose run --rm wp core install --url=http://example.com --title="This is the site title" --admin_user=admin --admin_password=admin --admin_email=example@example.com
# change value on this line
docker-compose run --rm wp option update blogdescription "This is the tagline."
docker-compose run --rm wp core update
docker-compose run --rm wp plugin install customizer-theme-resizer jetpack --activate
docker-compose run --rm wp plugin update --all
# set your theme choice on this line
docker-compose run --rm wp theme install https://downloads.wordpress.org/theme/best-reloaded.0.14.0.zip --activate
docker-compose run --rm wp media regenerate --yes
docker-compose run --rm wp rewrite structure '/%year%/%monthnum%/%postname%' --hard

In the script above replace the setup details with your own – the url, title, email etc. Also update the theme url with the download link of a theme of your choosing. In he example above I picked a random new theme from the WPORG repo. You can install any valid theme so long as it is available at the url you provide.

Save it in a file called fresh.sh and execute it like this:

# create the file and open for edit
nano fresh.sh
# enter the script from above replacing your own values where needed and save it
# set the scripts execute but and run it
sudo chmod +x fresh.sh
sh fresh.sh

When it’s complete you will have a freshly installed WordPress site ready to add your content.

Configuring the WordPress instance – A Site Import

A more realistic situation is you’re importing an existing site into a wordpress group. You will need to pull over your files and the database from the old host. We have mounted volumes pointing for convenience inside the home directory to make this easy. Through a shell inside the containers we’ll be able to extract any archives and run the import tasks.

First you’ll need to grab the site files – usually just the wp-content directory. Remember the container installs a recent WP version and you can use WP-CLI to update core, that will save you transferring that the migration. You’ll also need a copy of the database. A mysqldump file is fine, or you could use phpMyAdmin or any other MySQL management tool. You’ll be able unzip or otherwise decompress files inside your containers so archive them up to save on data transfer and time.

Inside your WordPress container directory you’ll have a home folder. Inside will be a directory that is mounted inside the wordpress container and the database container. Save the files you’ll need in each container to the correct directory and let’s get started the import. In the following commands you will be connecting via name. You can check the names of currently running containers using docker ps command.

#Remember to use your own container name here and replace any other items inside [square brackets] with your own values.
docker ps
sudo docker exec -it [wordpress_container] /bin/bash
# from inside the container
cd /home/wp/
unzip [wp-content.zip]
# note might be permission issues to deal with due to running cp as root
cp wp-content/* /var/www/html/wp-content/ -r
# set correct user/group on the moved files to prevent any issues with root owned files
chown www-data:www-data /var/www/html/wp-content/ -R

Importing the database is a simple mysql command. Just give it a username, tell it to prompt for password, set the database and direct the output of the sql file to it. It’s a one liner but it can take some time for large imports.

#Remember to use your own container name here and replace any other items inside [square brackets] with your own values.
docker ps
sudo docker exec -it [db_container] /bin/bash
# from inside the container
cd /home/db
mysql -u [db_username] -p [db_name] < [db_importfile.sql]

Your site import should be finished and accessible via domain (if you’ve already pointed it that is). You could always use the hosts file trick as a temporary way to access it via domain and test that it functions.

If you wanted to ensure WordPress core and the plugins were at their latest versions at this point you could run this command from the WordPress instance folder.

docker-compose run --rm wp core update
docker-compose run --rm wp plugin update --all

Wrapping up WordPress Docker Containers In Production

So as it happens running WordPress in production with an infrastructure built around Docker wasn’t nearly as hard as it sounds. Once you understand the whole overview shown in the visualization it just becomes a case of deciding on your base configurations. Once you reach the point of deploy it takes only seconds to get a fresh instance. And that’s totally scalable. You can continue to spin up more instances in just a few seconds, you can stop them even faster.

Containerized infrastructure built from individual isolated services is where a lot of momentum in web development world is focused. Deploying WordPress Containers in this way through a proxy and relying on Docker to run your services isolated is a great way to get to grips with many of the overarching concepts of working with Docker.