This will cover off setting up a Docker container for local WordPress development & mounting the container folder for easier development.
Major edit: there is a performance issue with this which I have mentioned in.more detail on the original article http://yer.ac/blog/2020/01/06/using-docker-containers-for-easy-local-wordpress-development🐳/
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!
Requirements
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 db
section.
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 entries
when 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 (docker-compose stop
)
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.
Footnote
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!
The post Using Docker Containers for easy local WordPress development🐳 appeared first on yer.ac | Adventures of a developer, and other things..
Top comments (0)