Docker is quickly becoming the preferred way to setup an environment for applications on your local machine. Along with Docker Compose you can now easily run WordPress in an isolated environment built with multiple Docker containers. You can even add a phpMyAdmin container, so you can easily get access to your WordPress database. This is quick guide on how to use Docker and Docker Compose to set up and run WordPress with phpMyAdmin. Before starting, make sure you have Docker and Docker Compose installed.
Let’s start with the folder structure. Download the WordPress application and extract it into a staring folder called “wordpress“. Also create a folder called “docker“. Create an empty php.ini file in this “docker” folder. Also create an empty docker-compose.yml (yaml syntax) file for your compose configuration in your base project folder (jjwdesign for my example). I’ll go over some of that shortly. Inside of your “wordpress” folder should be your typical WordPress directories, such as wp-admin, wp-content and wp-includes. The structure should look similar to the screen shot image below. Your base folder path will differ from ~/SItes/jjwdesign.
First, let’s go over how to add custom php.ini settings. Inside the docker directory, you should have created a php.ini file. This file will be copied to /usr/local/etc/php/conf.d/wp-php.ini on the WordPress service. Any ini files place in conf.d will be parsed by PHP. This will provide us a nice custom ini file for your PHP application (WordPress).
Below is an example of what I used for my custom PHP ini settings. I increased the memory and max execution time. I also increased the POST max size and UPLOAD max filesize to allow large files to be uploaded without problems occurring. This will allow us to run WordPress migration plugins such as the “All-in-One WP Migration”. Even though we will be using docker containers, it’s still handy to export and import backups using this tool.
docker/php.ini
# Custom PHP settings memory_limit=500M max_execution_time=900 post_max_size=128M upload_max_filesize = 128M
Note, you can change the filenames to whatever you wish, but make sure to change the volume references in the docker-compose.yml configuration.
Next, lets go over the fun part of this tutorial post. The docker-compose.yml file handles the creation of three services; a MySQL database, a phpMyAdmin server and a WordPress server. I’m not going to go over every detail, but I will point out a couple of tricky parts. See below.
docker-compose.yml
version: '3.7' networks: mynet: volumes: db_data: {} services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress networks: - mynet phpmyadmin: image: phpmyadmin/phpmyadmin ports: - "8181:80" environment: PMA_HOST: db networks: - mynet depends_on: - db wordpress: image: wordpress:latest volumes: - ./docker/php.ini:/usr/local/etc/php/conf.d/wp-php.ini - ./wordpress:/var/www/html ports: - "8080:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress networks: - mynet depends_on: - db
Network “mynet”
Notice how all three services were placed on the same network called “mynet”. This prevents an annoying error notice from phpMyAdmin where it complains about the PMA_HOST. Also notice how both the “phpmyadmin” and “wordpress” services use/depend on the “db” service.
Database: MySQL
Concerning the database “db”, you may decide to use MySQL or MariaDB. Just make sure to use the correct image name from DockerHub such as “mariadb:latest”. There are many different database options. For phpmyadmin, the image name is “phpmyadmin/phpmyadmin” which will pull from the latest image. Note, that the database passwords must match up for the “wordpress” service to connect to the “mysql” database. You may change them to be more secure, but they should remain consistent in your docker-compose.yml.
Docker-Compose Volume for WordPress
Another very important setting is the wordpress volume “./wordpress:/var/www/html”. Volumes allow you to save (persist data) files and directories between containers and your local (host) file system. This is basically how you save changes of your WordPress application on your local file system. For example, if you install a new Theme or Plugin you would want to save those added files in the wp-content directory. Without this volume setting you would loose your Theme & Plugin changes after your stopped docker-compose.
With this volume you can also make changes to your .htaccess file(s) and have them automatically reflected on the wordpress server. This should allow you to add PHP ini configuration settings to your .htaccess file, but I have not tested the server configuration. It may require an additional Apache2 configuration change.
Ports
One more thing to note is the HTTP ports. WordPress will run off of port 8080, where as phpMyAdmin will run off of port 8181. Again, you can change them to whatever you wish. Just make sure to pick a port that is not in use. If you are
To startup docker compose:
docker-compose up -d
In this tutorial post, I’m not going to go over docker-compose in detail or explain what images and containers work. There are plenty of tutorials already written that can explain. I will point out that the first time you run docker-compose, it will take a fairly long time to download all of the necessary images from the internet. Eventually, if all goes well, it will create and start the three service containers.
Then to visit (or install) WordPress, visit:
http://localhost:8080/
To view the phpMyAdmin tools:
http://localhost:8181/
Note, the username and password for the phpMyAdmin tool can be found in the docker-compose.yml file MySQL settings. I just used username: wordpress and password: wordpress. Note, you would never want to use something this simple in a production environment. This is only for local development usage.
To stop docker compose:
docker-compose down
This will stop and remove the service containers and network.