Setting Up a WordPress Docker

This is a starter docker-compose.yml file. Put this in your projects root directory. You’ll notice that you can put in custom themes and plugins as volumes.


version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8028:80"
       - "8029:8029"

     volumes:
       - ./theme:/var/www/html/wp-content/themes/custom-theme
       - ./message-board:/var/www/html/wp-content/plugins/message-board

     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       VIRTUAL_HOST: leasepilot.local

volumes:
    db_data:

To start up image


docker-compose up

To shut down the image you can either hit Control C or docker-compose down and if you would like to delete the volumes and start from scratch docker-compose down --volumes

To login to Docker image


docker exec -it id_container bash

if you are using the wordpress image you can simply use this command


docker-compose exec wordpress bash

To install nano within your Docker image


apt-get update
apt-get install nano
export TERM=xterm

To turn off debug messages simply nano into your docker and add these settings to your wp-config.php


ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_DISPLAY', false);

To access your database in this particular case you can use Sequel Pro and input this info (see MYSQL_ROOT_PASSWORD and MYSQL_DATABASE in your docker-compose.yml for password and database).
Sequel Pro Image

An experimental technique to make the URLs better for multiple site set up is to go to your host file and point localhost at a certain domain so that it will come up as something like http://yourdomain:8028.

If you are copying a database from a production site you can just import the backup via SequalPro, SSH into your docker env then at the root of the machine pull down https://github.com/interconnectit/Search-Replace-DB then visit yourdomain/Search-Replace-DB/ and use the GUI to search and replace all instances of the old URL to the new one.

If you need to copy assets from your local machine to your docker machine you can use a command like this docker cp c:\path\to\local\file container_name:/path/to/target/dir/

If this is a wordpress site and you don't feel like downloading all of the assets you can place this in the .htaccess file for get all of the images from the production site.


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^wp\-content\/uploads(.*)$ https://production-domain.com/wp-content/uploads$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^wp\-content\/blogs(.*)$ https://production-domain.com/wp-content/blogs$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^wp\-content\/sites(.*)$ https://production-domain.com/wp-content/sites$1 [L,R=301]