Site icon The Perpetual Student

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:

 

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:

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 🙂

Exit mobile version