WordPress Docker container with Compose

As I describe in my previous post, this site is running in a docker container on an Azure VM. In this post, I will explain how to configure WordPress docker container using Docker Compose.

WordPress Docker container

I wanted to deploy WordPress on my own host instead of using it as a service. In order to do this, you require a MySQL database in addition to the web host, unless you use Project Nami which enables you to use SQL Azure instead of MySQL. One option could be to use, for example, Azure App Service and a MySQL instance from ClearDB (you can find more information for this approach here).

I preferred to use another approach by taking advantage of Docker. With this approach I can have WordPress and MySQL running in separated containers in the same VM and I can add even more containers in the future at no extra cost (which is my idea).

The first step is to connect to the VM where Docker is running using ssh (if you have any questions about this, see my previous post). I wanted to use docker-compose to set up the environment, which is an orchestration tool that makes spinning up multi-container applications effortless. In a new folder (e.g. /home/user/dev/), create a new file named docker-compose.yml.

In the docker-compose.yml file, add the MySQL container based on the official mysql image under a configuration which I named db (you can choose any name instead of db). Additionally, set up the root password for MySQL specifying the MYSQL_ROOT_PASSWORD environment variable. The following snippet shows how the docker-compose file should looks like at this point.

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: YourWordPressPasswordHere

Now, add the WordPress container based on the official wordpress image, adding the link to the mysql container by specifying the name of the configuration under links as it’s shown in the following code snippet.

web:
  image: wordpress
  links:
    - db:mysql

Now, add the expose configuration under the WordPress container configuration (i. e. the web entry) to be able to access port 80 from the outside. Note that you could map the port to any other just by specifying “OutsidePort:80” (remember to use the double quotes).

web:
  image: wordpress
  links:
    - db:mysql
  expose:
    - "80"

This is enough for having the WordPress Docker container up and running, but there is another interesting thing to do before starting the containers. You can expose the wp-content folder in order to easily update themes, or other content from WordPress to the VM where docker is running. In order to do this, you will need to configure the working_dir and map the volumes as it’s performed below.

web:
  image: wordpress
  links:
    - db:mysql
  expose:
    - "80"
  working_dir: /var/www/html
  volumes:
    - "/pathInVM/wp-content:/var/www/html/wp-content"

The final version of the docker-compose.yml file should look similar to the following one:

web:
  image: wordpress
  links:
    - db:mysql
  expose:
    - "80"
  working_dir: /var/www/html
  volumes:
    - "/pathInVM/wp-content:/var/www/html/wp-content"

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: W0rdpressPassw0rd!

Once you have everything ready, save the changes to the file and execute docker-compose up to start both containers and try accessing the site which should be running in port 80. And that’s it, you have configured WordPress Docker container.

Creating a docker VM on Azure manually

In order to create this site, I take the opportunity to use Docker creating a new VM on Azure. In this article I will share the steps I used to create it by hand (without using the Docker VM Extension which makes it even easier).

First of all, I created an Ubuntu 14.04 virtual machine on Azure using the Azure Portal. I choose this ubuntu version because its the latest Long Term Support image available at the moment. In order to create the VM, just select new, choose the Ubuntu 14.04 image and follow the steps. In case that you need more details, you can find them in this article.

Once the VM was created, I configured the DNS name label for the VM in the configuration blade of it’s Public IP address (You can find it in the resource group). By specifying the DNS name, you will be able to locate your VM easily. Additionally, I configured the Inbound Security rules in the Network security group, adding port 80 for http access. Moreover, I configure my domain’s DNS (In this opportunity I choose Namecheap) to point to the VM’s URL using CNAME.

Then, I connected to the VM using ssh (don’t forget to append your username to avoid issues with your local username by using shh [email protected]) and started the configuration part. First of all, I update the dependencies by executing the usual commands for ubuntu:

sudo app-get update -y
sudo app-get upgrade -y

Docker logo, the key part of my Docker VM :P

 

Once everything is up to date, its time to install docker (else, it wouldn’t be a Docker VM :P). Docker documentation explains how to install and configure docker in Ubuntu in a really easy way. You can find this information here. To sum up, the steps are the following:

  1. Update package information, ensure that APT works with the https method, and that CA certificates are installed.
    apt-get update -y
    apt-get install apt-transport-https ca-certificates -y
    
  2. Add the new GPG key.
    sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
    
  3. Create the following file /etc/apt/sources.list.d/docker.list and add the following entry (for ubuntu 14.04)
    deb https://apt.dockerproject.org/repo ubuntu-trusty main
    
  4. Update the APT package index.
    apt-get update
    
  5. It’s recommended to install the linux-image-extra kernel package. The linux-image-extra package allows you use the aufs storage driver. To do so, execute the following command.
    sudo apt-get install linux-image-extra-$(uname -r)
    
  6. Install docker by executing the following command.
    sudo apt-get install docker-engine
    
  7. Verify docker was installed correctly, execute the following command. If it is needed, start docker server executing sudo service docker start.
    sudo docker run hello-world
    
  8. I also created the docker group and added my user to it, in order to avoid using sudo to run docker.
    sudo usermod -aG docker ubuntu
    

I also installed docker-compose following the instructions on this article.

That’s it. By following all the steps, I had my Docker VM up and running. In the following posts I will be explaining the architecture and the ideas and technologies behind this site.