Using Docker to host this WordPress blog

We use a tool called Docker to host this blog on a DigitalOcean server. Docker allows you to run processes on your server that are included in a pre-built image. It runs these programs in a contained environment, with no danger of harming other processes and files. We used a pre-built WordPress image for Docker to install this blog. This image includes nginx, MySQL and automatically installs WordPress when started. So we could instantly start this blog, without having to manually install WordPress.

Here is how that works:

Installing Docker on a Digital Ocean server

First, I created a machine at Digital Ocean and installed it with the standard Ubuntu 12.04.3 LTS x64 image.

Then, I installed Docker by following the instructions in the Docker documentation at http://docs.docker.io/en/latest/installation/ubuntulinux/#ubuntu-precise :

# install the backported kernel
$ sudo apt-get update
$ sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring

# reboot
$ sudo reboot

$ curl -s https://get.docker.io/ubuntu/ | sudo sh

This installs the Docker software. The next step is to install and run WordPress from a Docker image.

Installing and running WordPress from a Docker image

I cloned the docker-wordpress-nginx repository on GitHub made by Eugene Ware and built a Docker image from the files in this repository:

$ sudo apt-get install git-core
$ git clone https://github.com/eugeneware/docker-wordpress-nginx.git
$ cd docker-wordpress-nginx
$ sudo docker build -t="docker-wordpress-nginx" .

These commands run a script that builds an image from the commands in a Dockerfile, which is included in the repository. The resulting image has nginx, MySQL server, PHP and the WordPress files pre-installed so you can just run it and it will work.

When you run a Docker image, Docker will create a container and run the specified commands in that container. So, the files and processes in this container have no access to the filesystem and processes on the host system.

This also means that by default, the outside world does not have access to the TCP/IP ports in a container. That’s something that you specify yourself. You need to tell Docker which port from the container it needs to expose on the host system.

So, for this blog, I started the container with the following command:

$ docker run -d -p 8081:80 docker-wordpress-nginx

This will create a Docker container where port 8081 on the host is routed to port 80 in the container. It will also run the predefined commands in the docker-wordpress-nginx image that will start nginx, MySQL, PHP and WordPress.

I was now able to browse to my DigitalOcean server at http://95.85.61.92:8081 and see the WordPress installation page and set up this WordPress blog.

This is great, but I wanted this blog to appear on http://blog.intercityup.com/. So I configured to route all the web traffic for that domain name to the WordPress container. I did that with nginx:

Installing nginx as a load balancer/router for my container

First, I installed the standard Ubuntu version of nginx:

$ apt-get install nginx

nginx runs on port 80 by default, so with the standard nginx configuration, I was almost done. The only thing I needed to do now is install a virtual host configuration for the blog.intercityup.com domain and route all the traffic to port 8081, the web port inside my container.

I did this my creating the file /etc/nginx/sites-enabled/wordpress with the following contents:

server {
  listen 80;
  server_name blog.intercityup.com;

  location / {
    proxy_pass http://localhost:8081;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

And reload the nginx configuration with:

$ service nginx reload

With this configuration, the nginx on my server routes all the traffic for the blog.intercityup.com domain to port 8081 on the container that is running WordPress.