Recently I have started using Docker more and more, and for practice, I decided to containerize this WordPress blog. The blog was originally set up on a VPS running Apache, PHP, and MySQL. Multiple other domains were hosted on that server as well. Backups are hard, and if I ever needed to switch servers, the configuration and initial setup would be a nightmare. The machine at the time was running PHP 5.5 on Ubuntu 14.04. With the release of Ubuntu 16.04 and PHP 7, I had wanted to upgrade, but it would have been a very tedious task involving much manual labor. However, if I converted everything to run in containers, then the upgrade process becomes trivial.
I did not want to manually set up a new machine, so I chose Ansible to provision my new server since it is extremely easy to get started on, and does not require a server/client model as other tools do (such as Chef). I wrote a few playbooks and roles to provision web servers that perform all the basic tasks like user creation, adding SSH keys, installing packages, etc. Then it was time for the real work.
First, I committed initialized a new git repository and committed all the site source code into a directory called
src. The Dockerfile is relatively simple:
RUN apt-get update && apt-get install -y mysql-client libmysqlclient-dev \
&& docker-php-ext-install mysqli
RUN a2enmod rewrite
COPY src/ /var/www/html/
COPY config/php.ini /usr/local/etc/php/
COPY config/httpd.conf /etc/apache2/sites-available/000-default.conf
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
Since there are multiple domains hosted on the same server, I needed to use my custom Apache config file. This uses the official PHP 7 image with Apache to build a base image for the site. Now we need another container to run the database.
Docker Compose is an awesome tool that makes linking containers together very simple. I created a
web container builds the Dockerfile above, and the
db container is linked together with Docker Compose. After running
docker-compose up and restoring the MySQL database backup, I now have this blog running in containers, on PHP 7. The best part is that with the
restart: always option, the containers will automatically restart themselves, even across system reboots.