Migrating an existing WordPress + nginx + php5-fpm + mysql website to Docker containers: lessons learned

I’ve covered in previous posts why I wanted to Dockerize my site and move to containers, you can read about it in my other posts shared here. Having played with Docker for personal projects for several months at this point, I thought it was going to be easy, but ran into several issues and unexpected decisions that I needed to make. In this post I’ll summarize a few of these issues and learning points.

Realizing the meaning of ‘containers are ephemeral’, or ‘where do I put my application data’?

Docker images are the blueprint for a container, while the container is a running instance of an image. It’s clear from the Docker docs and elsewhere that you should treat your containers as ‘ephemeral’, meaning they only exist while they’re up and running, their state is temporary, and once they are discarded their state is also lost.

This is an easy concept to grasp at a high level, but in practice this leads to important and valid questions, like ‘so where does my data go’? This became very apparent to me when transferring my existing WordPress data. First, I have data in MySQL tables that needs to get imported into the new MySQL server running in a container. Second, where does the wordpress/wp-content go that in my case contains nearly 500MB of uploaded images from my 2,000+ posts?

The data for MySQL was easy to address, as the official MySQL docker image is already set up to use Docker’s data volume feature by default to externalize your MySQL data files outside of your running container.

The issue of where to put my WordPress wp-content containing 500MB of upload files is what caused my ahah moment with data volumes. Naively, you can create an image and use the COPY command to copy any number of files into an image, including even 500MB of images, but when you start to move this image around, like pushing it to a repository or a remote server, you quickly realize you’ve created something that is impractical. Making incremental changes to a image containing this quantity of files you quickly find that you’re unable to push it anywhere quickly.

To address this, I created an image with nginx and php5-fpm installed, but used Docker’s bind mount to reference and load my static content outside the container.

Now I have my app in containers, how do I actually deploy to different servers?

Up until this point I’ve built and run containers locally, I’ve set up a local Docker Repository for pushing images to for testing, but the main reasons I was interested in for this migration was to enable:

  • building and testing the containers locally
  • testing deployment to a VM server with an identical setup to my production KVM hosted server
  • pushing to my production server when I was ready to deploy to my live site

Before the Windows and MacOS naive Docker installations, I thought docker-machine was just a way to deploy to a locally running Docker install in a VM. It never occurred to me that you can also use the docker-machine command to act on any remote Docker install too.

It turns out even setting a env var DOCKER_HOST to point to the IP of any remote Docker server will enable you to direct commands to that remote server. I believe part of the ‘docker-machine create’ setup helps automate setting up TLS certs for communicating with your remote server, but you can also do this manually following the steps here. I took this approach because I wanted to use the certs from my dev machine as well as my GitLab build machine.

I used this approach to build my images locally, and then on committing my Dockerfile and source changes to my GitLab repo, I also set up a CI Pipeline to run the same commands and push automatically to a locally running test VM server, and then manually to push to my production server.

I’ll cover my GitLab CI Pipeline setup in an upcoming post.

How do you monitor an application running in containers?

I’ve been looking at a number of approaches. Prometheus looks like a great option, and I’ve been setting this up on my test server to take a look. I’m still looking at a few related options, maybe even using Grafana to visualize metrics. I’ll cover this in a future post too.

Installing Windows 8.1 guest on Linux Mint KVM

On my first attempt booting from the Windows 8.1 DVD to install as a guest in KVM, I got the initial Windows 8 splash screen, the DVD would spin for a few seconds but then spin down, and it would appear to be stuck at the logo screen, never reaching the spinning circle stage below the Windows icon.

There’s numerous posts of Windows 8 hanging at the logo screen, most of the conclusions seemed to be unless you didn’t have an error, just leave it until you get to the language selection dialog. I left mine about 10 minutes and got to the language dialog ok (I don’t remember a bare metal install taking that long before).

For my KVM vm settings, I left everything as defaults, apart from these changes based on numerous other posts on installing Windows 8 and 10 on KVM:

Processor: 1 CPU, and ‘copy host CPU configuration’

Disk: virtio disk bus, raw format, cache mode = node (not default)

Nic: virtio

Video: vga

After selecting ‘Custom install’ the ‘Where do you want to install Windows’ dialog says it could not find any drives. This is where you mount the virtio iso in the dvd for the vm, and then continue.

I hadn’t added a cd drive with the virtio iso to my vm before starting the install, and it looks like Virtual Machine Manager won’t let you add a device while the vm is running. Luckily, following as answer on this post, you can add a device on the fly with this command:

virsh attach-disk vmname /dev/sr0 hdc --type cdrom

I then loaded the virtio driver from this location on the mounted iso:

Next I got this rather cryptic error message:

"Windows is unable to install to the selected location. Error: 0x80300001."

Apparently this is a common error regardless of whether you’re installing Windows 8 in a VM or not. The quick explanation – unmount the virtio drivers iso, put back the install iso (or actual DVD) and refresh. Select partition (or create one) and continue. See here.

After completing all the prompts during install, success, Windows 8.1 virtualized using KVM on Linux Mint!

Post install, to get the virtio network card drivers install, mount the virtio iso disk, use the Control Panel/Drivers to view devices, pick the network card, then point to the NetKVM dir.

Next challenge, getting better video drivers installed (taking a look at Spice).