Creating WordPress development environment using Docker containers.
This will cover off setting up a Docker container for local WordPress development & mounting the container folder for easier development.
NOTE: Updated to note possible performance issue in Docker using Windows Containers and mounted volumes. See the end of this post.
Why do this?
Typically when doing any kind of WordPress work (Which is exceptionally rare for me), I would spin up a new WP instance on my host, or use something like a local LAMP/XAMP server.
Whilst this works, leveraging things like Docker mean almost zero configuration and more time developing. Better still, these then become throwaway environments!
The only requirement for this is the installation of Docker (https://www.docker.com)
Creating the WordPress instance
Firstly, create the folder where the configuration will live, for example
C:\Docker\Wordpress In this folder we need to make a file named
docker-compose.yml. This will be the YAML file detailing our wordpress installation – such as login information and MySQL setup.
In this file, copy and paste the content below:
version: '3.1' services: wordpress: image: wordpress restart: always ports: - 1234:80 environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: exampleuser WORDPRESS_DB_PASSWORD: examplepass WORDPRESS_DB_NAME: exampledb db: image: mysql:5.7 restart: always environment: MYSQL_DATABASE: exampledb MYSQL_USER: exampleuser MYSQL_PASSWORD: examplepass MYSQL_RANDOM_ROOT_PASSWORD: '1'
This will tell Docker that we need to use the image name “WordPress” from the Docker Hub (https://hub.docker.com/_/wordpress), forward port 80 of the container (Which will be the exposed website) to port number 1234 of the parent host. This means going to http://localhost:1234 would go to port 80 of the docker container.
Finally to use MySQL v5.7 as our database. You shouldn’t need to change this information, but if you do, make sure that the Database information in the
wordpress section matches the information in the
Once this file is saved, we can run
docker-compose up -d whilst in the same directory. This will take the YAML file, download any images that are not already on the local system and then set WordPress and MySQL up based on the YAML.
Note:: If you get the error
no matching manifest for windows/amd64 in the manifest list entrieswhen running this command, and you are on Windows 10, you will need to enable “Experimental Mode” in your Docker installation. To do this, right-click on Docker in the system tray and go to settings, checking the box named “Experimental mode”.
Note:: If you get an error like
ERROR: for wordpress_db_1 Cannot create container for service db:, this is usually caused by there being a conflict in ports. Try changing the external port (in this example “1234” for something else)
We can confirm if the docker container is running by running
docker ps to list all running containers, or by going to http://localhost:1234 (The port will be whatever you set it to in the YAML)
From here you can follow the standard WordPress installation steps.
Stopping and starting instances
You can stop and start instances by doing the following:
docker-compose stop and
docker-compose up -d. Stop may take a little while.
Note that these commands (in this syntax) have to be ran at the same level as the YAML file.
Working with the instance
If you just wanted to play with WordPress this would be enough, but what if you wanted to copy files to the instance, or edit files?
Option 1: SSH/ FTP
Although not personally a fan of this method, we could SSH to the container with a tool like PuTTY, see this guide: https://phase2.github.io/devtools/common-tasks/ssh-into-a-container/ We could also configure the container to have an FTP server. This won’t be covered as I believe the next option to be better.
Option 2: Mount a windows folder
This option will mount a specific folder within the Docker container to a folder on your own system.
Before continuing, we need to make sure that the container is currently stopped (
We will make a change to the YAML file so that we add a
volumes option, as shown below.
version: '3.1' services: wordpress: image: wordpress restart: always ports: - 1234:80 environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: exampleuser WORDPRESS_DB_PASSWORD: examplepass WORDPRESS_DB_NAME: exampledb volumes: - "C:/Docker/Wordpress/Mounted:/var/www/html" db: image: mysql:5.7 restart: always environment: MYSQL_DATABASE: exampledb MYSQL_USER: exampleuser MYSQL_PASSWORD: examplepass MYSQL_RANDOM_ROOT_PASSWORD: '1'
As we can see, the
volumes setting contains a string which is made up of 2 parts, seperated by a colon
:. These are:
- C:/Docker/Wordpress/Mounted – This is the local path you want to mount the folder against.
- /var/www/html” – This is the path within the container you want to mount.
Note that personally I like having the mount in the same directory as the configuration YAML for clarity, but it can live anywhere on your system.
Saving this file and re-running the docker-compose command will now map the specified volume.
I am a bit of a newbie when it comes to Docker, and I don’t use WordPress an awful lot so do take with a pinch of salt, and do let me know if this can be improved!
[UPDATE]: Performance Issues
It seems there are some well documented performance issues using volume mounts – especially on Mac and Windows. These caused the container to almost grind to a halt, even though there was hardly any CPU/RAM/IO use. There are many posts, including:
- https://github.com/docker/for-win/issues/188 (Yet another long-open Github issue for Docker)
- https://spin.atomicobject.com/2017/06/20/docker-mac-overcoming-slow-volumes/ (Some alternate suggestions)
For me, after doing some digging I found that doing the following helped greatly:
Switch to using Linux Containers if you are on Windows. This is a bit of a pain as I require Windows Containers for something else I am doing, but it seems like the only workaround.
Use the :cached flag on the mounted volume. This is specifically for an issue reported on Mac, but appears to have been successful on Windows using Linux containers too. ( https://docs.docker.com/compose/compose-file/#caching-options-for-volume-mounts-docker-desktop-for-mac )
This is done by changing the mounted volume line to append the :cached flag.
volumes: - C:/Docker/Wordpress/Mounted:/var/www/html:cached