Whenever we do any sort of development, it makes sense we can run our app locally to test it out before we publish it to production. However, setting up local server like XAMPP/MAMP takes some work. If you are working in a team, each of your teammate has to go through the steps setup on their local machine. Wouldn’t be nice if you can just have the same codebase, run a single command, and Voilà, you have Wordpress ready and running? Docker can help us with that and it is very simple to setup.
1. Download Docker Desktop
First, we need Docker Compose to run Wordpress in Docker. Docker Compose comes with Docker Desktop, which you can download it here — https://www.docker.com/products/docker-desktop.
After installing, you can open up Docker to validate it is successfully installed. (Don’t worry if you have no images yet, we will download them later)
You can also run the following command to check Docker Compose is installed.
2. Download Wordpress
Next, download Wordpress from here — https://wordpress.org/download/#download-install.
Unzip the Wordpress zip file and you will get a folder named
3. Create docker-compose.yml
wordpress folder with your code editor. Create a file named
docker-compose.yml at the root of
Wordpress requires MySQL database to run on, so let’s get our database ready. Paste the following code to
Let’s look at what the content of
docker-compose.yml line by line.
- Line 1, we define we want to use Docker Compose version 3.9
- Line 3,
servicesis where we can define what images we want to use.
- Line 4,
dbis the name we use to define the first service we want to use. The name can be anything.
- Line 5, we define the image of
mysql:5.7. Think of an image is like a blueprint for your application, or a recipe for the cake you want to make. So, what we are saying here is, use
mysql:5.7to be the blueprint for
- Line 6–7, we want the data in the DB to persist, if the container running our MySQL is restarted, the data within the container will be lost. So we want a persistent storage to store our data. Here we are mapping a volume called
var/lib/mysqlis the default location that MySQL will write its data file. So which means, whenever MySQL writes its data files, it will instead write those data to
db_data. We will define our volume later.
- Line 8, we set
dbcontainer stops running, it will always restart to keep the application alive.
- Line 9–13, we set the required environment variables for MySQL, we set everything to
wordpress. You can see all the available environment variables for MySQL here — https://hub.docker.com/_/mysql. Scroll down to the Environment Variables section.
Now that we have our MySQL service ready, let’s add Wordpress to our
- Line 15, we define the second service we want to have, which is
- Line 16,
depends_onallows us to control the sequence of services running. We set
wordpressto depend on
db. In this case,
dbwill run before
wordpresson start up, and
wordpresswill exit first before
dbon exit. This way, we can make sure
db, which is MySQL is ready before Wordpress starts to run.
- Line 18, use the latest version of Wordpress.
- Line 19, we map the current directory(where
docker-compose.ymlis located) to
/var/www/htmlinside the container.
/var/www/htmlis the default location Wordpress store its themes and plugins. We are telling Wordpress to store its data to our current directory
./. Whenever we add a new plugin/theme, we will see the plugin gets added to
wp-content/pluginsin our current directory. Don’t worry, we will see this in action later.
- Line 21–22, map port 8000 to 80. 80 is the default port that Wordpress runs on. So we are saying, when we go to port 8000 on your machine, it should be forwarded to port 80 inside the container. This way, we can visit Wordpress at localhost:8000.
- Line 24,
WORDPRESS_DB_HOSTis the host that we want Wordpress to connect. Remember that we set our database service name to be
dbon Line 5. Port 3306 is the default port that MySQL runs on.
- Line 26–28, remember that we set
MYSQL_PASSWORDon Line 11–13 to be
wordpress, Wordpress will be using these values to connect to the MySQL database.
- Line 29, we set the volume of
db_datato be empty initially, which is then used on Line 7 by the
Okay that’s a lot of stuff, but we are ready to get our applications running. Open a terminal at the
wordpress directory, then run:
docker compose up
What this command does is, it will look for
docker-compose.yml by default, start and run our entire app. After a while, you should see something similar.
Go to localhost:8000
And there you go! You can go through the steps to setup your Wordpress, it should be quite straightforward. You have Wordpress running on port 8000, inside a Docker container.
If you want to stop the app, you can just stop the app in your terminal using
CTRL+C on Windows, or
Command+C on Mac.
Try installing a plugin and see what happens to your current directory, currently I have nothing in the
Go to the Plugins section to download a plugin.
Go back to the code editor, you should see a new folder named
contact-form-7 being added. This proves that our mapping from current directory to Wordpress
We manage to run Wordpress in a Docker container. Next time you want to get it running, you can just run a simple
docker compose up . No more manual setup every time you use a different machine to work on your Wordpress project.
Thanks for reading!