Over the years, we’ve looked at several different systems for setting up local development environments, from applications like MAMP to a Varying Vagrant Vagrants workflow. I’m always looking for the most efficient way to create new WordPress instances, both for development and because I need an easily replicable WordPress environment for testing plugins and updates I want to write about.
Today, I’d like to talk about my current setup, which is based on the Docker container system. I considered using Docker a few years ago, but it was complicated compared to the alternatives, so I didn’t use it for long. But, on the recommendation of a friend, I recently took another look. The tooling has improved considerably and it’s now a good option for those of us who don’t want to spend too much time fiddling with our tools.
So, without further ado, let’s look at how to create a local WordPress development installation with Docker.
What Is Docker?
Docker is a tool for creating and managing containers. A container is a self-contained unit of software that includes all the libraries and other code required to run an application.
It’s important to understand that a container is not the same as a virtual machine — containers are much lighter than virtual machines, they “boot” a lot faster, and they’re more portable. Containers also don’t need a guest operating system.
To use Docker containers, you’ll need to set up Docker on your machine. Because the setup process is different depending on the operating system you use, I won’t go into detail here, but you can find full documentation on the Docker site.
I’m also going to assume you know how to use the command line on your OS.
Building A Local WordPress Container
There are several ways to go about building containers, but we’re going to use the excellent Docker Compose tool.
First, open a terminal, create a directory for your WordPress installation, and change to that directory:
mkdir testing-wordpress && cd testing-wordpress
Next, create the file we’ll use to tell Docker Compose what to do:
touch docker-compose.yml
We’re going to use the Docker Compose file from the official Compose documentation. Open “docker-compose.yml” in a text editor and paste the following text into it:
version: '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 volumes: db_data:
If you’re interested in what the file is doing, I encourage you to read the documentation, but, in brief, it tells Docker Compose to create two containers. The first is a container for the MySQL database, and the second is a container for WordPress itself.
Save the file, go back to your terminal, and run this command:
docker-compose up -d
Docker will download, configure, and launch the containers. It might take a few minutes, but when it’s finished, you’ll have a WordPress instance waiting at this URL:
http://localhost:8000
To stop the containers, use this command from inside the project folder:
docker-compose stop
You can restart with the “up” command you used previously.
If you want to remove the WordPress container, but leave the database intact:
docker-compose down
And if you want to blow away both WordPress and the database container:
docker-compose down --volumes
After removing the containers, you can start from scratch with the same “up” command as before:
docker-compose up -d
Because Docker containers are entirely self-contained, you can create as many WordPress installations as you like: just create a new folder, copy the “docker-compose.yml file to it, and repeat the process.