Using Docker for local WordPress Development

docker-compose for local development with an existing Wordpress site

Posted on May 31, 2019

I’ve worked extensively with WordPress in the past but my main focus over the last few years has been Javascript – Because of this I didn’t want to setup an apache server locally or install MAMP, so I decided Docker was the way to go for local WordPress development.

The first thing you need to do is install Docker desktop which can be found here: https://www.docker.com/products/docker-desktop

Once you’ve installed docker you’ll need to:

Once this has been done you should be able to run docker-compose up via the command line and view your site at https://marclloyd.co.uk.

As we’re mapping the template directory from your local files into the docker image, you can update your local template files and refresh the browser to view the changes.

I’ve commented the docker-compose.yml file to try and make it as obvious as possible whats going on, but here’s a break down of what we’re doing.

And there we have it – a simple docker setup for local WordPress development without the need for setting up a local http server or installing MAMP.

The code for your docker-compose.yml and migrate.sql files can be found below.

Accessing the database using Sequel Pro

You can access the MySQL database in the docker container with the following settings:

Host: 127.0.0.1
Username: user
Database: wordpress
Password: password
Port: 3400 (We mapped the default port 3306 to port 3400 on our host machine in the docker-compose.yml file)

docker-compose.yml file

version: '3.3'

services:
    db:
        image: mysql:5.7
        volumes:
            - ./data:/docker-entrypoint-initdb.d # This will import DB data from an sql file in your /data folder
            - ./data/migrate.sql:/docker-entrypoint-initdb.d/data/migrate.sql # run sql commands in migrate.sql to update site urls in DB
        restart: always
        ports:
            - "3400:3306" # mapping our ports for networking
        environment:
            MYSQL_ROOT_PASSWORD: testRootPassword
            MYSQL_DATABASE: wordpress
            MYSQL_USER: user
            MYSQL_PASSWORD: password
    wordpress:
        build:
          context: .
        depends_on:
            - db
        image: wordpress:latest
        ports:
            - "3500:80" # mapping our ports for networking
        restart: always
        environment:
            WORDPRESS_DB_HOST: db:3306 # Docker will automatically update the wp-config file with these details when using a WordPress image
            WORDPRESS_DB_USER: user
            WORDPRESS_DB_PASSWORD: password
            WORDPRESS_DB_NAME: wordpress
        volumes: # this is where we tell Docker what to pay attention to
            - ./wp-content/themes/my-theme-name:/var/www/html/wp-content/themes/my-theme-name # map theme to container
            - ./wp-content/plugins:/var/www/html/wp-content/plugins # map plugins to container
            - ./wp-content/uploads:/var/www/html/wp-content/uploads # map uploads to container

migrate.sql file

UPDATE wp_options SET option_value = replace(option_value, 'oldUrl', 'https://marclloyd.co.uk') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'oldUrl','https://marclloyd.co.uk');
UPDATE wp_posts SET post_content = replace(post_content, 'oldUrl', 'https://marclloyd.co.uk');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'oldUrl','https://marclloyd.co.uk');