Using Docker Containers With WordPress

Running WordPress websites can involve using a wide number of configurations and software dependencies to produce high-quality products. You may have wondered if there is an easier way to develop these kinds of projects locally without running so many different software packages on your own machine.

Fortunately, Docker is a product that offers a solution for a quicker and smoother WordPress setup. More than just a virtual machine, Docker provides containers for developing WordPress websites in addition to other applications and software. Docker can also help you prepare applications for shipment to other platforms easily, streamlining your workflow.

In this article, we’ll take a look at what exactly containers are and how they affect development. Then we’ll walk through how to set up a container for local WordPress development, and prepare it for production. Let’s get started!

Understanding Docker for WordPress: A Comprehensive Guide to Containers

Installing WordPress is no walk in the park, and it can be overwhelming to develop a WordPress website. In terms of web development, containers are standardized units of software that package up your code and all of its dependencies, so deploying WordPress becomes much easier. Multiple containers also make it possible to do this reliably from one environment to another. 

While Docker is a kind of virtualization, it’s different from regular Virtual Machines (VMs). Whereas VMs use up precious space, adding the host OS to the userspace for each application, containerized development shares that across all containers.

The real beauty of Docker is that it will run the same no matter what kind of OS you’re on. Additionally, you can run many Docker containers on a machine that would otherwise struggle with the same number of VMs. You also don’t have to worry about any of the errors local development can sometimes bring about. This is otherwise known as the ‘but it works on my machine’ conundrum in application development.

Getting Started With Docker and WordPress

Docker has many different products, which combine to create a powerful suite of developer tools. You can get started with the Docker Engine by downloading the Community version for free:

You’ll need to create a Docker account in order to download the engine. Then you’ll want to familiarize yourself with Docker’s ‘get started’ documentation. You’ll have eight different versions of Docker to choose from. For this example, we’re going to download Docker Desktop for Mac:

You’ll need to log in to your Docker account to download the engine. Additionally, you’ll want to make sure you have a terminal application on your computer ready to go.

Using a Container for WordPress (With Docker)

To use Docker containers to develop a WordPress website locally, you’ll also use another Docker application. We’ll cover how to install Compose in a moment, but it’s important to know that this tool is used for defining and running multi-container applications in Docker. Then, you’re ready to jump right in!

Step 1: Install Compose

Before you launch into action with Docker, you’ll want to make sure you’ve installed Compose. If you’re running Docker Desktop for Mac or Windows, Compose comes pre-installed:

If you’re working on Linux, you’ll need to use Terminal to execute the installation commands. Just keep in mind that Compose is dependent on the Docker Engine. You’ll need to have both installed in order to engage in any WordPress development. 

Compose uses a YAML Ain’t Markup Language (YAML) file to configure all of your application’s services. This is a human-friendly way to present data serialization, and it works with all programming languages. In this case, the YAML file will set up the necessary database information for WordPress.

Step 2: Define the Project

Once you install Docker Engine and Compose installed, you can get started on your first WordPress development project. You’ll need to create a new, empty project directory using the ‘make directory’ command:

mkdir new_wordpress/

The directory you set up will be the context for your development, and will contain only the necessary items, including your docker-compose.yaml file. We’ll create that file in the next step.

Next, you can move over to your new directory using the following command: 

cd new_wordpress/

You’ll now be in your new project directory, and ready to get to work. 

Step 3: Create a Docker File for YAML

Now, you’ll need to create a new docker-compose.yaml file. This will create your new WordPress installation and the necessary MySQL database. Your file will contain all of the information for both WordPress and your database, including necessary ports, databases, hostnames, and passwords.

Your YAML file will look something like this: 

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}

It’s worth noting that Docker volumes contain persistent data, and you may want to refer to the documentation on volumes to get fully up to speed.

Step 4: Build the Project

Now that you’ve configured the YAML file, you’ll need to execute the command that will pull in all the required images and set up your WordPress project. From within your project directory, you’ll run the following command: 

docker-compose up -d

You should now see that Docker is ‘pulling’ in the MySQL and WordPress images

It’s worth noting the Docker Hub image pages contain a lot of valuable information regarding the MySQL and WordPress containers: 

Consider these your all-in-one reference guide with helpful commands for the container image. They are worth bookmarking for later use.

Step 5: Access Your WordPress Installation

Because you’re installing this site locally, you’ll need to use the port indicated in your YAML file rather than the typical WordPress /install.php script. Since we’re using Docker Desktop for Mac in our example, we’ll use https://localhost as the IP address and open port https://localhost:8000. This was the port designated in your YAML file.

You’ll likely be in familiar territory now if you’ve installed WordPress before, and you can complete the ‘famous five-minute installation’ process: 

You might need to give Docker a few minutes to completely load and initialize the images, but then you can access WordPress in a browser to complete the standard installation steps

It’s recommended that you ‘shut down and clean up’ once you’ve completed the installation as well. To do this, you can use the Docker ‘down’ command. This will stop and remove the containers, but preserves the WordPress database. There are quite a few things to understand about pausing, killing, and stopping containers if you’re using Docker, so it’s worth reading more on the topic.

Using WordPress and Docker for Streamlined Deployment to Production

It’s important to note here that sites hosted on WP Engine servers do not have the root access that is necessary to setup a new docker image. However, if your site is on a production server you can follow the steps below to ship your image.

Now that you’ve created a WordPress image locally, you might be wondering how you can use the same image on a production server. Some of the steps you just completed will be the same, but you’ll need to create a private repository on Docker Hub first. 

To take your WordPress Docker container to production, you’ll need to use your Docker account and push the image to a private repository. From there, you can follow the same steps we previously went over, but you’ll pull the image from your repository directly to your production server by changing the port information in your YAML file. 

There is a free option for using private repositories, but you can only create one. Fortunately, Docker has many pricing tiers to suit just about any developer’s needs:

Ultimately, if you are looking to improve your development workflow or are just getting your feet wet in this area, Docker is an excellent tool to have in your resource kit. It provides plenty of useful documentation and support as well. 

Build Something Great With WP Engine

Now that you’ve been oriented to Docker, you might be eager to jump in and start developing some new applications to set up a WordPress site. Keep in mind that one of the biggest benefits of Docker is that you’ll know your applications won’t run into quirky machine-specific errors. 

At WP Engine, we love sharing the best developer resources so you can create incredible digital experiences for your customers. Whether you’re in the development, staging, or production phase of your project, we also have fully managed hosting for WordPress and flexible hosting plans that can help! 

Get started

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.