local WordPress development using Docker

Every local website needs an adequate environment to work properly. That typically means setting up a web server, a database, and support for all the languages you’ll need. The problem is that installing all these components can get tricky, and they may not play nice with each other. That’s why it’s worth giving local WordPress development using Docker a shot. 🔫

Docker is an application that enables you to set up entire self-contained environments with a few simple commands. It is compatible with WordPress, and can be highly useful if you need to set up various local environments with different types of web servers or tools. 💻 Because each of your containers is essentially a virtual computer, you’re free to set up different technology stacks as needed.

Why you should consider using Docker to create a local WordPress environment

The Docker homepage.
Docker enables you to set up self-contained local development environments with ease.

In most cases, setting up a local WordPress development website requires that you install an entire environment in one fell swoop. Once you do that, you’re stuck with the environment you’ve chosen unless you want to uninstall the components and add new ones. For example, if you set up an Apache server on your computer, installing NGINX alongside it can be a real pain. However, swapping components may be necessary to recreate your production environments.

This brings us to Docker, a tool that enables you to set up isolated ‘containers’ that include entire development environments. Think of it as setting up a Virtual Machine (VM) without an operating system. These containers provide almost all the same benefits as VM, without weighing your system down. In practice, that means you can set up multiple environments on a normal computer.

As if that weren’t enough, you can also turn containers on or off at will. That way, the impact to your system will be minimal. Starting them back up when you need them just takes a single command.

⚙️ Key Features:

  • Create containers with isolated development environments.
  • Use fewer resources than with VMs.
  • Share your containers to make collaborations easier.

💵 Price:

Docker has a free personal plan. Premium-level plans start at $5 per month (billed annually at $60) and go up from there. Month-to-month billing is also available at a higher rate for all plans except for the highest level business plan, which requires an annual subscription.

How to get started with local WordPress development using Docker (in three simple steps)

To take advantage of local WordPress development using Docker, you’ll ideally want to be comfortable using the command line. However, don’t worry – even if you’ve hardly used it, all you need to do is follow our instructions. Without a working knowledge of the command line, you might not be able to improvise. But you’ll be able to get started just fine.

For this tutorial, we’ll be using the Windows version of Docker, but it’s also available for Linux and Mac. The commands themselves are interchangeable between platforms, so the process remains largely the same regardless of your operating system. Let’s get to work!

Step 1: Download and run Docker 🏃

First, visit the Docker desktop page and look for the Download for [OS] tab. Then, choose the version you want based on your operating system:

Download Docker for Mac, Windows, or Linux.

On the next page, you can download the application and then run the installer. This part is very simple – all you need to do is keep moving through the screens and wait until the installation is complete. When it’s done, you’ll need to restart your computer.

Once your system is back up and running, open your command line. On Windows, you can just search for Powershell if you don’t want to install another program. Then, type in docker ps. If you installed Docker successfully, you should see something like this:

A list of your running Docker containers.

That’s a list of the containers currently running on your computer and as you can see, it’s empty. Let’s fix that right away.

Step 2: Set up a container with a WordPress environment 🗄️

There are two ways to go about this step – you can create a container and set up all the elements you need one by one, or you can do it all in one fell swoop. Naturally, the second route is the smart way to go, so let’s walk through it.

We’re going to use a tool called Compose V2, which is bundled in with Docker Desktop. It enables you to configure the services you want your container to have and set them up all at once. (Note that if you set up Docker before July 2023, you may need to migrate to Compose V2).

Start out by opening the command line again and entering this command:

mkdir wordpress-local && cd wordpress-local

This will create a directory called wordpress-local on your computer. Open that folder and create a new file called docker-compose.yml, using your favorite text editor.

Then, paste the following code snippet into the file:

version: '3'
services:
  wordpress:
    image: wordpress
    ports:
      - "127.0.0.3:8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: <your_database_name>
      WORDPRESS_DB_USER: <your_mysql_user>
      WORDPRESS_DB_PASSWORD: <your_mysql_user_password>
    volumes:
      - ./wp-content:/var/www/html/wp-content

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: <your_mysql_root_password>
      MYSQL_DATABASE: <your_database_name>
      MYSQL_USER: <your_mysql_user>
      MYSQL_PASSWORD: <your_mysql_user_password>
    volumes:
      - db-data:/var/lib/mysql

volumes:
  db-data:

You’ll need to replace the placeholders with your own details. For example, you’ll need to assign specific ports and individual passwords by editing the values in the file.

When you’re ready, save your changes and close your editor. What you just did is instruct Docker to set up a new database for WordPress and install the software, along with all the other services it needs.

Step 3: Get your WordPress container up and running 🎯

All that’s left now is to tell Docker to start up your container. To do that, navigate to the directory you’ve just created, using the following command:

cd wordpress-local

Then, enter the following command:

docker compose up -d

Now, sit back and wait for Docker to download and set up all the services you indicated in your docker-compose.yml file. This might take a few minutes, depending on your internet connection.

Once the setup is complete, you’ll be able to access your WordPress site from any browser by entering the ports you set up earlier. In our case, these were 127.0.0.3:8080:

Finishing up local WordPress development using Docker.

All that’s left to do now is follow the regular WordPress setup process in your browser.

The WordPress container you’ve just created will also appear in Docker Desktop:

WordPress container in Docker Desktop

Keep in mind that you can set up as many Docker containers as you want, and power them up or down at will using these two commands:

docker compose stop
docker compose up -d

Now you’re ready to get rocking with your local WordPress development using Docker!

Conclusion 🧐

There are plenty of ways to set up local environments for WordPress development and create simple testing sites, but Docker stands out thanks to its compartmentalization features.

With it, you’ll be able to set up multiple environments side by side using different components and turn them on and off at will, which is a huge plus.

🧗🏼‍♀️ Here are the three steps to setting up local WordPress development using Docker:

  1. Download and run Docker 🏃
  2. Set up a container with a WordPress environment 🗄️
  3. Get your container up and running 🎯

Do you have any questions about how to get started with local WordPress development using Docker? Feel free to ask us in the comments section below!

Yay! 🎉 You made it to the end of the article!

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!