using docker compose to build a container with
- nginx
- wordpress
- mysql
- mailhog
- underscores starter theme
- wp-cli
- letsencrypt
- certbot
/wp-content/
will be mapped to the container for easy access to the theme files and content.
Read the official docs for the respective docker images
Setting up SSL is optional and can be configured after setting up standard http.
For development Mailhog will catch all email sent from wordpress and serve as an inbox to test.
In the docker-compose
file comment out mailhog and uncomment certbot for production and SSL setup.
# start containers
docker-compose up -d
# stop containers
docker-compose down
# stop containers and remove all volumes
docker-compose down -v
# build images on start
docker-compose up --build
Once containers are running load up the browser http://127.0.0.1
Install wordpress it will use the db settings from the .env so you won't have to enter them. The installation will persist on db_data
volume until its removed. As you can see from the docker-compose.yml
we are referencing the container name db
as the wordpress containers DB_HOST
this will connect both containers without having to expose any ports.
You can deploy to docker swarm if you want to run containers on multiple hosts at the same time but this will require the images to be hosted on a registry like Docker Hub.
# deploying to swarm
docker stack deploy -c docker-compose.yml docker-wp
Set the file permissions of the wp-content
folder, since they are mounted from the host. Otherwise you will notice issues updating plugins.
chown -R www-data:www-data /var/www/html/wp-content
The wordpress user created with this build won't have permissions to export the database you will need to use root.
From the official mysql docker image https://hub.docker.com/_/mysql when MYSQL_RANDOM_ROOT_PASSWORD
is set to true "The generated root password will be printed to stdout (GENERATED ROOT PASSWORD: .....)." You can view your docker container log when the db image first boots up to get the root password.
Uncomment this line in the docker-compose file if you want to set a custom root password:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT}
set the .env var MYSQL_ROOT
with your desired root password. You can also save the randomly generated root password in .env for safe keeping.
Steps to export and import the mysql database:
# get the container id
docker ps
# gain a shell to the db container
docker exec -it containerid /bin/bash
# export the wp database
mysqldump -u root -p wordpress > backup.sql
# then enter your root password on prompt
# check the backup.sql file has been created
ls
# copy from docker container
docker cp container:./backup.sql ./backup.sql
# copy to docker container
docker cp ./import.sql container:./import.sql
# import existing database dump
mysql -u root -p < import.sql
- wp-mail-smtp plugin
Setup the plugin and configure smtp settings to handle mail.
- composer is installed
- phpmailer is installed
- mailhog is running on
http://localhost:8025
and catches mail on port 1025
We need to configure wordpress to route the mail to mailhog. I've found the solution between these two sources:
- https://gist.github.com/eduwass/039864b7dca85f06c3883b6fab0f7f2e
- https://developer.wordpress.org/reference/hooks/phpmailer_init/
- edit the theme functions file to configure the local smtp settings at
/var/www/html/wp-content/themes/_s/functions.php
add the following code to the theme functions.php file
/* MAILHOG SMTP */
function my_phpmailer_example( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = 'mailhog';
$phpmailer->Port = 1025;
}
add_action( 'phpmailer_init', 'my_phpmailer_example' );
This will configure phpmailer to use mailhog with port 1025
Note: I've tried automating this on the build of the dockerfile see mailhog-config.sh but this causes issues on build.
- get a shell to the container
cd /var/www/html
- activate the theme (with modified functions.php)
wp theme activate _s --allow-root
- activate the wp-mail-smtp plugin
wp plugin activate wp-mail-smtp --allow-root
- Send a test mail from the plugin dashboard
http://localhost/wp-admin/admin.php?page=wp-mail-smtp&tab=test
- check mailhog caught it!
http://localhost:8025/
configure the wp-mail-smtp plugin with your mailgun settings
- private API key
- domain
- enable permalinks
http://localhost/wp-json/wp/v2
command: certonly --webroot --webroot-path=/var/www/certbot --email your-email@domain.com --agree-tos --no-eff-email -d domain.com -d www.domain.com
docker-compose up -d
# ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
cd nginx
cp default.conf backup.conf
cat ssl.conf > default.conf
Now we can start the containers again using the ssl certificates certbot just created for the domain name and email we specified in the compose file.