Moving a WordPress site into a Docker Container

Things to note

  1. I will be using example.com as the domain in this article.
  2. I will use 12.34.56.78 as the IP address of the server.
  3. I am working with an Ubuntu 16.04 server.

Prerequisites

sudo apt install nginx

Step 1: Understanding Docker Compose

wget -O - https://bit.ly/docker-install| bash

Step 2: Export the WordPress site

  1. A zip archive of the entire contents of the WordPress site. (I’ll refer to this as site.zip)
  2. A dump of the MySQL database. (I’ll refer to this as database.sql)
  3. The database name. (I’ll refer to this as example_db_name)
  4. The database password. (I’ll refer to this as example_db_password)
  5. The database user. (I’ll refer to this as example_db_user)

Step 3: Set up the site source code

rm -r /var/www/example.com/*
mkdir -pv /var/www/example.com/src
cd /var/www/example.com
wget "http://example.com/site.zip" --no-check-certificate
unzip site.zip -d src/

Step 4: Set up the site database

mkdir -pv /var/www/example.com/database
mkdir -pv /var/www/example.com/database/data
mkdir -pv /var/www/example.com/database/initdb.d
cd /var/www/example.com/database/initdb.d
wget "http://example.com/database.sql" --no-check-certificate

Step 5: Start Docker containers using docker-compose

touch /var/www/example.com/docker-compose.yml
version: '2'

services:
example_db:
image: mysql:5.7
container_name: example_db
volumes:
- ./database/data:/var/lib/mysql
- ./database/initdb.d:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: aieububsnlVUOBWHLEINA # any random string will do
MYSQL_DATABASE: example_db_name # the name of your mysql database
MYSQL_USER: example_db_user # the name of the database user
MYSQL_PASSWORD: example_db_password # the password of the mysql user

example:
depends_on:
- example_db
image: wordpress:php7.1 # we're using the image with php7.1
container_name: example
ports:
- "1234:80"
restart: always
links:
- example_db:mysql
volumes:
- ./src:/var/www/html
cd /var/www/example.com
docker-compose up -d

Step 6: Use Nginx as a reverse proxy to our Docker Container

cd /etc/nginx/sites-available
touch example.com.conf
server {
listen 80;
listen [::]:80;

server_name example.com www.example.com;
location / {
proxy_pass http://0.0.0.0:1234;
proxy_set_header Accept-Encoding "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
/etc/init.d/nginx reload

Step 7: Fix wp-config.php

/** MySQL hostname */
define('DB_HOST', 'localhost');
/** MySQL hostname */
define('DB_HOST', 'mysql');
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) 
&& $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}

Step 8: Install a free SSL certificate and use HTTPS (Optional)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store