Use WordPress locally with Docker using Docker compose
- A
Dockerfilefor extending a base image and using a custom Docker image with an automated build on Docker Hub - PHP 7.4
- Custom domain for example
myapp.local - Custom nginx config in
./nginx - Custom PHP
php.iniconfig in./config - Volumes for
nginx,wordpressandmariadb - Bedrock - modern development tools, easier configuration, and an improved secured folder structure for WordPress
- Composer
- WP-CLI - WP-CLI is the command-line interface for WordPress.
- MailHog - An email testing tool for developers. Configure your outgoing SMTP server and view your outgoing email in a web UI.
- PhpMyAdmin - free and open source administration tool for MySQL and MariaDB
- PhpMyAdmin config in
./config
- PhpMyAdmin config in
- CLI script to create a SSL certificate
Requirements
Install mkcert:
brew install mkcert
brew install nss # if you use Firefox
Setup
Easily set your own local domain, db settings and more. Start by creating .env files, like the examples below.
Copy .env.example in the project root to .env and edit your preferences.
Example:
IP=127.0.0.1
APP_NAME=myapp
DOMAIN="myapp.local"
DB_HOST=mysql
DB_NAME=myapp
DB_ROOT_PASSWORD=password
DB_TABLE_PREFIX=wp_Edit ./src/.env.example to your needs. During the composer create-project command described below, an ./src/.env will be created.
1. Use HTTPS with a custom domain
Create a SSL cert:
cd cli
./create-cert.shNote: mkcert needs to be installed.
This script will create a locally-trusted development certificates. It requires no configuration.
2. Use a simple config
- Edit
nginx/default.conf.confto use this simpler config.
server {
listen 80;
root /var/www/html/web;
index index.php;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
client_max_body_size 100M;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass wordpress:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
- Edit the nginx service in
docker-compose.ymlto use port 80. 443 is not needed now.
nginx:
image: nginx:latest
container_name: ${APP_NAME}-nginx
ports:
- '80:80'
- Edit
./src/.env.exampleand set
WP_HOME='http://localhost'
- Run using
docker-compose up - Open http://localhost/
Install
docker-compose run composer create-projectRun
docker-compose upDocker Compose will now start all the services for you:
Starting myapp-mysql ... done
Starting myapp-composer ... done
Starting myapp-phpmyadmin ... done
Starting myapp-wordpress ... done
Starting myapp-nginx ... done
Starting myapp-mailhog ... doneπ Open https://myapp.local in your browser
PhpMyAdmin comes installed as a service in docker-compose.
π Open http://127.0.0.1:8082/ in your browser
MailHog comes installed as a service in docker-compose.
π Open http://0.0.0.0:8025/ in your browser
Tools
docker-compose run composer updatedocker exec -it myapp-wordpress bashLogin to the container
wp search-replace https://olddomain.com https://newdomain.com --allow-rootRun a wp-cli command
You can use this command first after you've installed WordPress using Composer as the example above.
You can, but I recommend to use Composer for this only. But to enable this edit ./src/config/environments/development.php (for example to use it in Dev)
Config::define('DISALLOW_FILE_EDIT', false);
Config::define('DISALLOW_FILE_MODS', false);When making changes to the Dockerfile, use:
docker-compose up -d --force-recreate --buildLogin to the docker container
docker exec -it myapp-wordpress bashStop
docker-compose stopDown (stop and remove)
docker-compose downCleanup
docker-compose rm -vRecreate
docker-compose up -d --force-recreateRebuild docker container when Dockerfile has changed
docker-compose up -d --force-recreate --buildChangelog
- Use
NGINX_ENVSUBST_TEMPLATE_SUFFIX. Use a template and better substution of ENV variables in nginx config.
- Added mariadb-client (Solves #54)
- Updated Bedrock. Update WordPress to 5.5.1 and other composer updates.
- Added Mailhog. Thanks @mortensassi
- Added nginx gzip compression
- Added Windows support for creating SSH cert, trusting it and setting up the host file entry. Thanks to @styssi
- Remove port number from
DB_HOST. Generated database connection error in macOS Catalina. Thanks to @nirvanadev - Add missing ENV variable from mariadb Thanks to @vonwa
- Added phpMyAdmin config.Thanks to @titoffanton
- Readme improvements. Explain
/etc/hostsbetter
- Use
Entrypointcommand in Docker Compose to replace the domain name in the nginx config. Removing the need to manually edit the domain name in the nginx conf. Now using the.envvalueDOMAIN - Added APP_NAME in
.env-exampleThanks to @Dave3o3
- Added
.envsupport for specifying your own app name, domain etc in Docker and cli scripts. - Added phpMyAdmin. Visit http://127.0.0.1:8080/
- Added Linux support. Thanks to @faysal-ishtiaq.