How to Dockerize Your WordPress Site
Using Docker and WordPress together is nothing short of beautiful
This article has also been posted on my blog, which you can find at http://brockheriondev.com/how-to-dockerize-your-wordpress-site/
I love Docker. I really do love this piece of technology. The ability to spin up any project in a container on any OS and deploy it to virtually any OS with little to no configuration is astounding. You can spin up an entire database or .NET app or, as I have been playing with recently, an entire WordPress installation.
I’m still relatively new to WordPress. PHP is not a language I use on a day-to-day basis and MySQL is definitely not my first choice in databases. But as I read about it and learned more about the platform, I decided that I wanted to get to a real, hands-on experience with it.
I decided the best way to learn WordPress was to develop a theme or a plugin. This meant I had to install PHP, WordPress, and MySQL or MariaDB on my desktop, and possibly my laptop, should I decided to learn on the couch or something. To make matters more difficult, my desktop PC is running Windows 10 and my laptop is a MacBook Pro.
Enter Docker, my knight in shining armor. I had played around with Docker plenty of times before and had used it once in a production environment. What I had not done was developed an entire stack using Docker Compose to manage the whole thing. This, as it would turn out, was the right move for me. It solved my cross-platform issues, I didn’t have to install anymore tooling or databases on my systems, and I was free to spin up as many instances as I wanted to without having to delete a ton of files.
I am going to assume if you are reading this that you have both Docker and Docker Compose all set up on your system. If you don’t, there are a lot of great tutorials on the internet for getting setup. Feel free to check out Docker’s website for more information on the platform or its documentation. Now, let’s get started!
Open up a command prompt or terminal and find the location you want your site to live locally. Then, run
mkdir my-wordpress-docker-site && \
This will put you in the folder you just created. We are now ready to get our Docker Compose file setup!
The docker-compose File
Open up this folder in your favorite editor. I prefer using Visual Studio Code, but Atom, PHP Storm, and others should do just fine.
Once in your folder, create a file named docker-compose.yml. Open your newly created file and let’s start adding our configuration!
The first line we want to add is
This tells Docker Compose we want to use version 3 for file formatting. At the time of writing this, the latest version is 3.8.
Next, we need to add two sections, one to define and configure our services and the other to mount our volumes. Let’s go ahead and add the following lines
Both of these should go under your version declaration.
Now, let’s add in our database service. For this install, I chose MariaDB over MySQL. MariaDB is an alternative database to MySQL, created by the guy who originally created MySQL. You can very easily do a MySQL configuration here as well if you prefer, the setup is nearly identical.
Let’s add the following section under services
This is all that’s needed to spin up a new MySQL/MariaDB instance in Docker. Pretty cool right?
We now need to go under our volumes section and add-in
What this will do is map the /var/lib/mysql folder in the Docker container to a folder called db_data. Our data will now be persisted whenever we spin up or shut down our containers.
We are now ready to get WordPress setup!
Under services, we need to add another section. Go ahead and add the following configuration
Note that we added depends_on:db here. We are telling Docker that this service requires our database to be ready to go before WordPress can spin up. This will prevent WordPress from trying to connect to a database that might or might not be ready to go. We are also mapping port 8000 on our system to port 80 in the container.
Fire it up
Now, all you have to do is run
in your command line and let Docker do its thing.
Once it’s done, you should be able to visit http://localhost:8000 and land on the default WordPress setup page. In your folder, you should now see a wordpress folder. This now will contain the entire WordPress install that you can manage. You can add or develop themes and plugins and add them in here.
One thing to note here, as mentioned in the WordPress official Docker page, is that this image does not contain any other PHP extensions or libraries that some plugins require. If you do need them, you might want to find a different image or create your own Dockerfile to add them in.
Final docker-compose file
I would like to thank you for taking the time to read through this and I hope I was able to help you get WordPress up and running in Docker. Docker really is an amazing piece of technology that makes development much easier. When you are ready to deploy now, you can deploy to something like Azure App Service or a Kubernetes cluster or, now that you have everything mapped locally, just package your site up and ship it to something like SiteGround or Digital Ocean. It’s that easy.
For more information on the Docker WordPress image, check out https://hub.docker.com/_/wordpress