Moving a WordPress site into a Docker Container

Things to note

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


sudo apt install nginx

Step 1: Understanding Docker Compose

wget -O -| 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
  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/*
mkdir -pv /var/www/
cd /var/www/
wget "" --no-check-certificate
unzip -d src/

Step 4: Set up the site database

mkdir -pv /var/www/
mkdir -pv /var/www/
mkdir -pv /var/www/
cd /var/www/
wget "" --no-check-certificate

Step 5: Start Docker containers using docker-compose

touch /var/www/
version: '2'

image: mysql:5.7
container_name: example_db
- ./database/data:/var/lib/mysql
- ./database/initdb.d:/docker-entrypoint-initdb.d
restart: always
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_db
image: wordpress:php7.1 # we're using the image with php7.1
container_name: example
- "1234:80"
restart: always
- example_db:mysql
- ./src:/var/www/html
cd /var/www/
docker-compose up -d

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

cd /etc/nginx/sites-available
server {
listen 80;
listen [::]:80;

location / {
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');
&& $_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