How to Migrate WordPress Nginx with Docker Compose on Ubuntu Xenial 16.04

This guide will explain how to migrate and dockerize an existing WordPress installation without running Apache. Most other tutorials I’ve seen combine multiple services inside of one container, while this article tries to maintain the standard of one service per container for easier isolation, troubleshooting, performance, and scale-out. This consists of configuring multiple Docker containers with docker-compose on Ubuntu 16.04 Xenial to run MariaDB 10.0.31 or MySQL, Nginx 1.13, PHP7.1-FPM and WordPress 4.8.

There are many tutorials for creating new WordPress sites with Docker containers such as this one, however there aren’t many for those who already have a site and want to migrate existing WordPress installations to Docker containers.

Directory structure:

├── nginx_proxy
├── nginx_src
│   ├── conf.d
│   ├── h5bp
│   ├── sites-available
│   ├── sites-enabled
│   └── ssl
├── php_fpm
│   └── 7.0
├── wp_compose
├── wp_content
│   └── src
├── wp_img
│   └── src
└── wp_db
├── etc
└── var

Install Docker

Use official Docker Installation or use the code below to install since the version in the standard repo’s are dated.

wget -qO- | sh
sudo curl -fsSL | sh

Backup WordPress

First step is to backup your MySQL database and Tar your WordPress wp-content directory. Unlike most DB backup tutorials, this creates the database if it does not exist with the -B switch. My WordPress Docker image (wp_img) contains the rest of the files of the latest version. This site will help if you don’t have the ability to create a backup but do have the database files.

#Backup wordpress db
mysqldump -u [user] -p -B -h [hostname] [database] --triggers --routines > ~/wordpress.sql
#Backup wp-content
tar -cvzf ~/wp-content.tar.gz /var/www/html/wp-content
#Or backup entire site directory
tar -cvzf ~/wp-full.tar.gz /var/www/html

Dockerfile wp_db

FROM mariadb:10.0
ADD *.sql /docker-entrypoint-initdb.d/
ADD *.sh /docker-entrypoint-initdb.d/

The official MariaDB/MySQL Docker images will execute .sh, .sql and .sql.gz files in the containers /docker-entrypoint-initdb.d directory, which contains the wordpress.sql backup file along with any shell scripts.

Build and Run wp_db

docker build . -t sysinfo-io:mariadb10.0
docker run -d -e MYSQL_RANDOM_ROOT_PASSWORD=yes -p 3306:3306 -v /docker/wp_db/var:/var/lib/mysql -v /docker/wp_db/etc:/etc/mysql/conf.d --name wp_db -t sysinfo-io:mariadb10.0
To access the shell inside of a running container: docker exec -it <container-name> bash
Disqus Comments Loading...

Recent Posts

Bittorrent IP Blocklists

In addition to using a VPN service, as an extra precaution I've been using the blocklist feature of my bittorrent… Read More

October 26, 2019 3:31 pm

FreeNAS Error Creating Pool

command '('gpart', 'create', '-s', 'gpt', '/dev/da8')' returned non-zero exit status 1. If you get this error while trying to create… Read More

June 7, 2019 3:44 pm

Change Grub Default Boot Entry on Linux Mint

I'm dual booting Windows and Linux Mint on my laptop. The grub default is to boot into Linux Mint, however… Read More

April 23, 2019 7:45 pm

How to Reset Secure Channel On Active Directory Domain Controller

When you're a little too careless about virtualizing your domain controllers, cloning, migrating, backing up and restoring, returning from vacation… Read More

April 21, 2019 8:14 am

Run Systemd Script Before System Shutdown

I tried to retain the NGINX FastCGI cache and have it persist across system reboots instead of being ephemeral by… Read More

April 20, 2019 10:14 am

Learn Systemctl Usage to Manage Systemd Service in Linux

Systemd is new service manager for Linux. It's a replacement for all previous init systems (SysV/SysVinit & Ubuntu's Upstart) and… Read More

April 20, 2019 7:55 am