Goal:
In this how-to, we are going to learn how to set up a new wordpress docker project from scratch. This will be useful for anyone who is looking to build a new wordpress site from new or simply run an existing wordpress site locally in a development environment.
Prerequisites:
Wordpress requirements
To run wordpress in a local environment(or any environment), we must first workout what wordpress needs to run.
This can be simply broken down into the following requirements :
wordpress-docker-folder:
This can be based on the list of requirements we worked out at the start (web server, programming language / compiler, database).
As this is a simple starter guide to running wordpress using docker, we will be using pre-configured docker images that Wordpress.org has officially released : https://hub.docker.com/_/wordpress
From this default configuration file, we can see there are two services ‘worpdress’ & ‘db’. Effectively these will be the two containers that we need to run our website. Both of these containers are built based on images. These images are basically a blueprint of commands that install all the required dependencies.
Here is a brief break down of each image:
Wordpress:
Mysql:5.7
Before we create and run our containers for the first time, we will need to make a slight adjustment to our docker-compose.yml configuration. This slight change will allow us to link our virtual docker volumes to folders within our project.
For both services make the respective changes:
Wordpress:
Mysql:
As we are going to be using the default wordpress docker-compose.yml file, there will be little to no further configuration to creating and running a new wordpress site locally.
To run the docker-compose configuration, type ‘docker-compose up’ in your terminal. Make sure that your terminal is in the same directory as your project & docker-compose file.
As this will be your first time running these images / containers, docker will have to download all the required images in order to build the containers. These images once downloaded, are stored locally and can be used again and again. So if you feel that this might not be as fast as you were expecting, don’t worry this is just a once off.
After running the ‘docker-compose up’, you will have noticed that both folders (‘wordpress’ and ‘db’) were populated with files and folders. This is done automatically by the commands inside both wordpress & mysql images.
To view your local wordpress site, visit http://localhost:8080/
Once visiting localhost, you should be prompted with the ‘Wordpress Installation’ screen. This means that you have successfully created all the docker containers in order to run wordpress locally. Congratulations!
*Note: we will be skipping a few steps as they were already covered in Part One. This section will be continuing off from Step 3 - Part One.
If you completed all steps from Part One, before attempting the following, you will need to do some cleaning up. This will prevent the previously generated files from causing any issuses.
In order to run an existing wordpress site in our local environment, we will need two things. A database export, and wp-content’s folder from the site we want to run locally.
Under the volumes for the mysql service add ‘- ./schema/:/docker-entrypoint-initdb.d’
Now that we have added this line, we will need to create a folder called ‘schema’ in our project and copy our .sql file into it.
Our project should already have a ‘wordpress’ folder. Make sure it is empty. Copy across the wp-content folder that you gathered from your live site.
Now that all our required content from our live site is in our project, we can run our containers.
Run ‘docker-compose up’.
Visiting http://localhost:8080/ should now show us our local instance of our existing site.
However, if this does not show you your existing site, there may be issues with php, and wordpress versions. If this is the case you will need to further customise your docker instance, or update your existing site to the latest versions before trying to import it into this local environment. Note this can cause issues with old themes & plugins, I will be releasing a future guide on how to customise your local docker instance to run older sites.
Q: What happens when you ‘ctrl + c’ the terminal that docker is running in?
Q: How do I completely remove these containers?
Q: How do docker volumes work?