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:
/docker
├── 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- https://get.docker.com/ | sh #or sudo curl -fsSL get.docker.com | 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 blog.travisrunyard.us: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 blog.travisrunyard.us:mariadb10.0