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:
- Checkout your WordPress project.
- Add the following code to a docker-compose.yml file in the root of your project.
- Create a /data folder in the root of your project.
- Add a dump of your database to the /data folder.
- Add a migrate.sql file to the /data folder containing the sql commands below (this will update the URLs in your database once it’s been imported).
- Update the oldUrl placeholder in the migrate.sql file.
Once this has been done you should be able to run docker-compose up via the command line and view your site at http://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.
- Creating a MySQL database container, setting the root password, creating a database called ‘wordpress’ and creating a user.
- Mapping our container database port (3306) to 3400 so we can access the database on our host machine via a third party app. I use Sequel Pro.
- Downloading the latest Version of the WordPress Docker Image.
- Mapping the containers port 80 to 3500 so we can access it locally in browser at http://marclloyd.co.uk
- Updating the wp-config file with the user and database details we created above.
- Mapping our theme, plugins and uploads folder to the container.
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:
Port: 3400 (We mapped the default port 3306 to port 3400 on our host machine in the 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
UPDATE wp_options SET option_value = replace(option_value, 'oldUrl', 'http://marclloyd.co.uk') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'oldUrl','http://marclloyd.co.uk'); UPDATE wp_posts SET post_content = replace(post_content, 'oldUrl', 'http://marclloyd.co.uk'); UPDATE wp_postmeta SET meta_value = replace(meta_value,'oldUrl','http://marclloyd.co.uk');