CodeX
Published in

CodeX

CODEX

How to Dockerize Your WordPress Site

Using Docker and WordPress together is nothing short of beautiful

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

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.

Project Setup

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 && \
cd 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

version: ‘3’

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

services:volumes:

Both of these should go under your version declaration.

Database Setup

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

db:
image: mariadb:10.5
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: <your-root-password>
MYSQL_DATABASE: <your-database-name>
MYSQL_USER: <your-user-name>
MYSQL_PASSWORD: <your-user-password>

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

db_data:

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!

WordPress Setup

Under services, we need to add another section. Go ahead and add the following configuration

wordpress:
depends_on: db
image: wordpress:latest
ports:
- “8000:80”
restart: always
volumes:
- ./wordpress:/var/www/html/
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: <your-db-user>
WORDPRESS_DB_PASSWORD: <your-db-password>
WORDPRESS_DB_NAME: <your-database-name>

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

docker-compose-up

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

version: ‘3’services:
db:
image: mariadb:10.5
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: <your-root-password>
MYSQL_DATABASE: <your-database-name>
MYSQL_USER: <your-user-name>
MYSQL_PASSWORD: <your-user-password>

wordpress:
depends_on: db
image: wordpress:latest
ports:
- “8000:80”
restart: always
volumes:
- ./wordpress:/var/www/html/
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: <your-db-user>
WORDPRESS_DB_PASSWORD: <your-db-password>
WORDPRESS_DB_NAME: <your-database-name>
volumes:
db_data:

Wrap up

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brock Joseph Herion

Brock Joseph Herion

I am a software developer who love coding in Python, Javascript, and C#. I am a sucker for learning new technologies and tooling to make development easier.