How to Use Docker to Install WordPress Locally on Windows

Posted on

Docker is a virtualization platform that enables the development and distribution of bundled applications inside “containers.” These containers are hosted by a software called “Docker Engine.” It’s responsible for managing and running the applications found in the containers.

Thus, with Docker, it is possible to deploy a preconfigured AMP (Apache, MySQL, and PHP) stack to host a WordPress site. You don’t need to know the basics of web hosting and install Apache, MySQL, and PHP manually on your workstation.

Docker is not just for deploying AMP stacks – the Docker Hub offers hundreds of free, ready-to-use containers for multiple applications.

In this tutorial, we’ll see how to use Docker to install WordPress locally on your Windows desktop.

Step #1: Installing the Docker Desktop

The first step is to install the Docker Desktop application on your workstation. Docker is available in two editions: Docker CE, for Community Edition, and Docker EE, for Enterprise Edition. Therefore, we will use the Docker CE.

Important: Docker can not co-habit with VirtualBox. Therefore, if you use VirtualBox, it will no longer be functional after installing Docker.

To download Docker Desktop for Windows 10, go to:
https://hub.docker.com/editions/community/docker-ce-desktop-windows

Note that you will need to register to download the software.

Run the Docker Desktop installer leaving all the default options. When the installation is complete, you will be prompted to log out of your Windows session and log back on.

docker desktop installation on windows 10

After opening a new Windows session, start the “Docker Desktop” application. You will then be prompted to enable “Hyper-V” and “Containers” so that Docker can work properly. Click on “Ok” to proceed.

windows docker enabling hyper-v

Once the activation of “Hyper-V” and “Containers” is complete, your computer will restart automatically.

Step #2: Modifying the “hosts” File

For this tutorial, I’ll install WordPress for the “webmestre101.home.lan” domain. We’ll add an entry to the Windows “hosts” file so that we can use this domain name to access the WordPress site.

Right-click on the Notepad application icon and select Run as administrator:

windows notepad run as administrator

Now open c:\windows\system32\drivers\etc\hosts in Notepad. Insert a new line at the very end of the file. This line must contain your domain name and needs to point to the IP address 127.0.0.1.

windows hosts file

Save the file and close Notepad.

Step #3: Creating the Docker Compose File

To install WordPress locally, we’ll need two Docker containers:

  1. WordPress: The WordPress container contains Apache and PHP, which simplifies the task.
  2. MySQL: This is the official Docker container of the MySQL database server.

When multiple Docker containers are running simultaneously, it’s best to use Docker Compose. On Windows, Docker Compose is included in the installation of Docker Desktop, so no need to install it manually.

Docker Compose is used to provide container configuration parameters and to establish dependencies between them. So, in our case, the WordPress container depends on the MySQL container. The WordPress container must wait for the MySQL service to be active before starting.

Create the following folders on your “C:” drive:

c:\docker\wordpress

Then create a file named docker-compose.yml into c:\docker\wordpress and copy the following content:

version: '3.3'

services:
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - ./wordpress_files:/var/www/html
    ports:
      - "80:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: my_wordpress_db_password
  db:
    image: mysql:5.7
    command: --init-file /data/application/init.sql
    volumes:
      - ./db_data:/var/lib/mysql
      - ./init.sql:/data/application/init.sql
    restart: always
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: my_wordpress_db_password
      MYSQL_ROOT_PASSWORD: root_password
volumes:
  db_data:
  wordpress_files:

For the database to be created automatically when the MySQL container starts, create a file named init.sql also into “c:\docker\wordpress.” Insert the SQL statements below:

CREATE DATABASE IF NOT EXISTS wordpress;
CREATE USER IF NOT EXISTS 'wordpress'@'%' IDENTIFIED BY 'my_wordpress_db_password';
GRANT ALL ON `wordpress`.* TO 'wordpress'@'%' IDENTIFIED BY 'my_wordpress_db_password';
FLUSH PRIVILEGES;

Thus, the “wordpress” database will be created when the MySQL container is started if it does not already exist. The same is true for the user account of the same name.

Step #4: Starting the Docker Containers

Open a command prompt window by pressing the Windows+R keys and type cmd:

windows open command prompt

Type this command to change the directory:

cd c:\docker\wordpress

Then start the containers in the background using docker-compose with the “-d” switch:

docker-compose up -d

During the first startup, Docker downloads the images for WordPress and MySQL, which may take a while:

docker-compose download container images

When the downloads are complete, the containers will start:

starting docker containers

When you start the containers, Docker Desktop may ask you if you want to share the “C:” drive on your computer. You must click “Yes” so that MySQL can create the database files.

If you have error messages while starting the containers, type this command to stop them first:

docker-compose down

Start the containers again but this time omitting the “-d” switch:

docker-compose up

The “-d” parameter is used to run the containers in the background. By eliminating this parameter, the run logs are displayed on the screen:

docker-compose up logs

In some cases, this can help you diagnose problems. To stop the Docker containers, press CTRL+C:

docker-compose down

Step #5: Configuring WordPress

At this point, all you have to do is set up WordPress as you usually do. Use your web browser to access the site:

wordpress configuration wizard

Note that there is no SSL certificate installed so you must access the site at “http://yourdomain.com.”

In conclusion

This type of installation is ideal for working offline or for showing a website mockup to a customer, for example. All files are contained in one directory (c:\docker\wordpress\), so it’s easy to copy or move the site to another computer.

As for WordPress, all the files are in c:\docker\wordpress\wordpress_files. It is, therefore, possible for you to edit a theme locally and then copy it to the production server when the development is complete.

However, if you need to install and manage multiple WordPress sites locally, it would be easier to use tools such as CapRover and EasyEngine. These tools automate the deployment of WordPress sites (and even other types of sites).

Leave a Reply

Your email address will not be published. Required fields are marked *