Local WordPress Development Environment using Docker

Docker Containers

The following are code snippets to enable a localized container for WordPress.  The effort here is to eliminate specific folders from development and leave that up to the production server to serve.

You also don’t have to worry about WordPress core as that is handled by the official WP Docker store.

.htaccess

By utilizing this file, you’ve effectively eliminated the need of having to download your whole /wp-content/uploads folder (which is typically the largest content on your WP site.

# ==========================================================
# Redirects uploads to production.
# ==========================================================
<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^wp-content/uploads/(.*)$ https://www.DOMAINNAME.com/wp-content/uploads/$1 [R=301,NC,L]
</IfModule>

# ==========================================================
# Docker WordPress
# ==========================================================
# BEGIN WordPress
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
</IfModule>
# END WordPress

 

docker-compose.yml

To automate the creation of the containers, put this on your root project folder:

Gist: docker-compose.yml

Create Folders

Create the following folders from the root of your project:

  • wp-content
  • wp-content/themes
  • wp-content/plugins
  • wp-content/uploads

 

Docker Commands

Use the following commands to start / stop / create / remove your new WP development containers:
docker-compose up -d Builds your containers.
docker-compose down Removes containers.
docker-compose down -v Removes containers and associated volumes stored on your computer.
docker exec -it container_name bash SSH into the container.

 

Next Steps

From here, you’ll have the assets and local DB/Apache required to make these work.  You’ll STILL get an error message.  The next steps are required to get your production DB + Assets on your computer:

  • Export a fresh copy of your database and make sure you don’t select “create schema” if you’re using something like MySQL Workbench.  I use the following settings in MySQL Workbench:
    • Select all under: Objects to Export
    • Select: Export to Self-Contained File
    • Check: Create dump in a single transaction (self-contained file only)
  • Import the fresh self-contained file using your importer. Important: select “wordpress” as the schema/database you’re importing to.
  • Make sure to update wp_options from your MySQL client/CLI to reflect your local development URL: http://localhost:8080

This should be sufficient to see your website on your own dev computer.  However, if you’ve hardcoded your URLs on your content, there could be issues.  If you’re reading this, I’m sure you already know how to fix those 🙂

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: