WordPress Docker Local Development
Photo by Sebastian Herrmann on Unsplash
Quick Docker config file and tips for setting up WordPress local development for an existing WordPress website.
I needed to make some quick code updates to an existing WordPress website so I wanted to spool up a local instance using Docker but I didn’t find an example of anyone doing the two things I consider critical: importing a database SQL dump and importing the WordPress files to edit in the wp-content directory. Here’s what I think is best practice:
Install Docker
Add your local domain name
Edit your hosts file to add your local domain:
1 |
127.0.0.1 wordpress.localhost |
Your existing website files
Create a new directory named mywebsite-wordpress
to host your local development and open it.
Create a new subdirectory database/docker-entrypoint-initdb.d/
and copy your database SQL dump into it.
Create a new subdirectory wp-content/
and copy your WordPress wp-content (e.g. themes, plugins, uploads) into it.
docker-compose.yaml
This is the docker-compose.yaml configuration file that I suggest to make everything work:
Create this docker-compose.yaml
in your local development directory and paste this in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
version: '3.1' services: db: command: '--default-authentication-plugin=mysql_native_password' container_name: wordpress_database environment: - MYSQL_ROOT_PASSWORD=wordpress - MYSQL_DATABASE=wordpress - MYSQL_USER=wordpress - MYSQL_PASSWORD=wordpress expose: - 3306 - 33060 # MariaDB image that supports both amd64 & arm64 architecture image: mariadb:10.6.4-focal # Uncomment if you want to use MySQL instead #image: mysql:8.0.27 restart: always volumes: - 'wordpress-database:/var/lib/mysql' # Execute *.sql, *.sql.gz, *.sh e.g. import SQL dump - ./database/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d wordpress: container_name: wordpress_app environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress WORDPRESS_DEBUG: 1 WORDPRESS_CONFIG_EXTRA: | // Change domain name to local development define('WP_HOME', 'http://wordpress.localhost'); define('WP_SITEURL', WP_HOME); // Enable logging to ./wp-content/debug.log define('WP_DEBUG_LOG', true); image: wordpress:latest ports: - 80:80 restart: always volumes: - ./wp-content/:/var/www/html/wp-content/ - 'wordpress-app:/var/www/html' volumes: wordpress-app: wordpress-database: |
Notes
Your SQL dump(s) can be named anything so long as they are in the ./database/docker-entrypoint-initdb.d
directory.
Your WordPress website domain name is changed to wordpress.localhost
(without the need to edit the SQL dump or database).
Debugging is enabled (WordPress debug errors are displayed as messages on the local website and in a log file in ./wp-content/debug.log
Go
Start Docker with:
1 |
docker compose up |
This will import your SQL dump and your WordPress files and use Docker volumes for the database and WordPress standard files.
Edit
Visit http://wordpress.localhost in your browser to confirm your website is working locally.
Edit your files (e.g. themes, plugins) in the wp-content/
directory.
Stop
Stop Docker with:
1 |
docker compose down -v |