If you’re a web developer, you probably know the importance of having a local installation of yours and your clients’ sites available on your machine. After all, a local installation allows you to easily make changes, tweak the design, and test plugins or themes without affecting the live website.

Unfortunately, as useful as a local installation is, it can also be a tedious process to set it up because you need to install a local server environment that replicates your hosting server and live website as closely as possible.

In most cases, you need to install an Apache server, a database, and WordPress itself. If your client uses hosting with an NGINX server, installing NGINX alongside Apache can cause a myriad of problems and headaches.

What if there was a better way to create a local WordPress installation? It turns out there is. Instead of finding a server environment that meets all of your needs, you can easily install WordPress on your machine using a tool called Docker.

In this post, we’ll explain what Docker is, talk about the benefits of Docker, and show you how to use it to install WordPress on your machine.

 

What Is Docker?

Docker is a containerization tool that allows you to set up separate “containers” which include a complete development environment. These individual containers are similar to virtual machines, but they don’t require you to install a complete operating system to be able to run them.

Since these containers are usually more lightweight than a virtual machine, you can easily setup multiple containers without slowing down your computer. What’s more, each of these containers uses a configuration file which makes it easy to transfer the container to a different machine or upload it to the cloud.

You can use Docker to create a development environment for any type of application, WordPress included. Like WordPress, Docker is an open-source application and can be installed on Windows, Linux, and Mac computers.

 

Benefits of Using Docker:

Now that we’ve covered what Docker is, let’s go over the benefits of using it for local development.

No Need to Set Up (Multiple) Server Environments

As mentioned earlier, with Docker, there is no need to setup a server environment nor do you need to be careful about which server environment you install.

 

Less Resource Heavy Than a Virtual Machine

Docker doesn’t require a separate operating system to run which makes it less resource-heavy on your computer than a virtual machine or a server environment.

 

Can Be Used on Any Platform

Docker is cross-platform compatible.

 

Allows Collaboration on Containers

Lastly, you can easily collaborate with others on containers. This means you can allow your clients or other developers on your team to access and work on a local WordPress installation.

 

How to Install WordPress Using Docker

 

Installing WordPress using Docker is not difficult but it does require you to be familiar with the command line. For this tutorial, we’ll be using the Mac version of Docker, however, you will still be able to follow along if you use Windows or Linux as commands used in the tutorial work on any platform.

 

Download and Install Docker

Start by visiting the Docker homepage and download the appropriate version for your operating system by clicking on Products > Docker Desktop. On the next page, choose your operating process and wait until Docker is done downloading.

 

 

Once Docker has been downloaded, double-click the installer package and continue with the installation process as you would for any other program. Simply follow the prompts on your screen and wait until the installation is complete.

 

 

You can check whether the installation was successful by launching the terminal or the PowerShell and run the command docker ps. Alternatively, run the command docker run hello-word. Docker will fetch an image from their servers and create a new container using that image. You will then see the following message:

 

Create a Container for WordPress:

 

Now that you’ve verified that Docker was successfully installed and that it works as intended, it’s time to create the environment for your local WordPress install.

The easiest way to do this is to use Docker Compose which comes with Docker for Mac and Windows and allows you to configure all the services you need for your app at once instead of one by one. If you’re using Linux, you will need to install Compose by following the installation instructions for your Linux distribution.

Launch your terminal or PowerShell and enter the following commands:

mkdir wordpress-local
cd wordpress-local
touch docker-compose.yml

The first command creates a wordpress-local directory on your computer. The second command allows you to switch to the newly created directory. The last command creates a docker-compose.yml file which is the configuration file that will be used to create your local WordPress install.

At this point, use your regular folder tool and locate the docker-compose.yml file. Open it with your preferred text editor and paste in the following:

version: '3.3'

services:

   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: localwordpress
       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: {}

 

Feel free to replace the database name and password with your own information. Save changes. The code block above created a new database for WordPress and installed WordPress itself and the services needed for WordPress to run properly.

 

Get WordPress Up and Running

 

The last step of the process is to finish the WordPress installation by telling Docker to start up the container. Open your terminal or PowerShell and make sure you are in the directory that contains your docker-compose.yml file.

 

Run the following command:

docker-compose up -d

 

Docker will then download and setup WordPress and all the services. Once the installation is complete, open your browser and enter http://localhost:8000 as the address.

You will then see the famous WordPress installation screen and you can proceed with the final setup. Choose your language, enter WordPress information that you will use to login and let the setup complete.

 

 

You can then login to your local WordPress installation and proceed with your usual WordPress setup. If you want to stop the container, all you have to do is enter the following command:

docker-compose stop

You can just as easily start it back up with the docker-compose up -d command. Keep in mind that you can have as many containers as you need so start experimenting and give Docker a go.

Final Thoughts

 

Using Docker to install WordPress is a quicker and easier way of creating a local installation of your WordPress website. What’s more, you can use Docker to set up multiple WordPress installations without worrying about possible conflicts. Take advantage of this tutorial and start using Docker for local WordPress development.