Migrating WordPress to docker

Migrating WordPress to docker

I’ve been playing around with a setup for running docker locally for the last year. So now the times has come to move my blog to my own server.

First step was to select which docker images to use for my setup. Given that WordPress publishes their own official docker image, so this was a simple choice.

For selection of database, I had the choice of MySQL & MariaDB, both publishes official docker images. Using one of these images would have been a simple choice as well, however wherever possible I prefer to use image built by linuxserver, since they only provide a MariaDB image i choose this

Setup of containers

With the image selected, it’s to configure them, for my setup. In my setup that consists of a docker run command. Starting with the MariaDB container in use this command.

docker run -d \ --name='mariadb' \ -e TZ="Europe/Paris" \ -e 'TCP_PORT_3306'='3306' \ -e 'MYSQL_ROOT_PASSWORD'='ROOTPASSWORD' \ -e 'MYSQL_DATABASE'='wordpress' \ -e 'MYSQL_USER'='USER' \ -e 'MYSQL_PASSWORD'='PASSWORD' \ -v '/mnt/user/appdata/mariadb':'/config':'rw' 'linuxserver/mariadb'
Code language: JavaScript (javascript)

This will create a database named WordPress and a user with access to to said database. Next I’ll add the WordPress container.

docker run -d \ --name='WordPress' \ -e 'TCP_PORT_80'='8080' \ -e 'WORDPRESS_DB_HOST'='mariadb:3306' \ -e 'WORDPRESS_DB_USER'='USER' \ -e 'WORDPRESS_DB_PASSWORD'='PASSWORD' \ -v '/mnt/user/appdata/wordpress':'/var/www/html':'rw' 'wordpress'
Code language: JavaScript (javascript)

What’s notable for this configuration, is the database configuration. Because I already created a use for MariaDB, I want to make sure the login match.

Since the containers are part of the same docker network, they will be able to use local name resolution. Instead of using the IP of the MariaDB container, I can simple use the name variable from line 2 of the MariaDB run command.

WordPress migration

At this point I have a freshly installed WordPress instance and browsing to the containers IP will start the first run wizard

WordPress first run wizard

I’ll run thru the first run wizard, and since I’m migrating from and existing blog, I make sure to name the user identically with the one used on my previous blog.

Before moving the content, make sure to change the URL found in Settings -> General.

Configuring the URL for the WordPress site

The steps to move the content is straight forward, taken from the WordPress support site

  1. Go on old site Admin panel. Here, in Manage > Export select “all” in menu Restrict Author.
  2. Click on Download Export File
  3. In new site go on Manage > Import, choose WordPress item.
  4. In the page that will be shown, select the file just exported. Click on Upload file and Import
  5. It will appear a page. In Assign Authors, assign the author to users that already exist or create new ones.
  6. Click on Submit

That is it, with these step my blog was running in a docker container locally.

Final steps

Since I only want to make my blog available over https. For this I’ll reuse my existing reverse proxy, which also runs on a linuxserver image, which. I won’t cover the setup, but a comprehensive guide can be found here. With the setup the Nginx webserver will request a Let’s Encrypt certificate which will allow me to host https websites. So I only have to add a config file to forward connections to blog.nosnik.dk to the WordPress container.

server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name blog.*; include /config/nginx/ssl.conf; client_max_body_size 0; location / { include /config/nginx/proxy.conf; resolver valid=30s; set $upstream_wordpress wordpress; proxy_pass http://$upstream_wordpress; } }
Code language: PHP (php)

In case you missed it

I forgot this step which meant that image links on blog posts used the containers internal IP, I used the Better Search Replace plugin to update the URL for existing image

Replace base URL for image links with Better Search Replace plugin