Running Wordpress in Docker (Goodbye XAMPP/MAMP)

Felix Lee
6 min readJun 13, 2021

--

Photo by Stephen Phillips — Hostreviews.co.uk on Unsplash

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.

docker compose

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 wordpress .

3. Create docker-compose.yml

Open up wordpress folder with your code editor. Create a file named docker-compose.yml at the root of wordpress folder.

Wordpress requires MySQL database to run on, so let’s get our database ready. Paste the following code to docker-compose.yml .

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, services is where we can define what images we want to use.
  • Line 4, db is 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 db to be 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.7 to be the blueprint for db service.
  • 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 db_data to var/lib/mysql . var/lib/mysql is 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 restart to always . Whenever db container 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 docker-compose.yml .

  • Line 15, we define the second service we want to have, which is wordpress .
  • Line 16, depends_on allows us to control the sequence of services running. We set wordpress to depend on db . In this case, db will run before wordpress on start up, and wordpress will exit first before db on 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.yml is located) to /var/www/html inside the container. /var/www/html is 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/plugins in 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_HOST is the host that we want Wordpress to connect. Remember that we set our database service name to be db on Line 5. Port 3306 is the default port that MySQL runs on.
  • Line 26–28, remember that we set MYSQL_DATABASE , MYSQL_USER , and MYSQL_PASSWORD on 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_data to be empty initially, which is then used on Line 7 by the db service.

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.

Install Plugins

Try installing a plugin and see what happens to your current directory, currently I have nothing in the plugins directory.

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 var/www/html works!

Conclusion

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!

--

--

Felix Lee

Software engineer in Singapore. Write random tech stuff. Still figuring out this “life” thing in life.