How to Install WordPress with Docker, An Easily to Follow Guide

WordPress with Docker

This tutorial is going to show you how to run a WordPress installation in Docker containers by using Docker Compose.

WordPress is the most popular CMS (Content Management System) in the world, which makes it a top-drawer consideration when building a new site. It is built on a MySQL database with PHP processing.

Setting up a local WordPress installation involves multiple steps and can be a time-consuming process. Usually you need to setup a local web server, configure the server to be able to execute PHP code, and setup a MySQL database. However the most easiest way to setup a local WordPress environment is to use containerization with Docker.

Related: What is a Docker Container: An Introductory Guide for Beginners

If you are just starting with Docker and haven’t installed Docker yet, I can recommend you start with our tutorial Install Docker on Ubuntu: A Step-by-Step Guide.

1. Install Docker Compose

When using Docker extensively, the management of several different containers quickly becomes cumbersome. Docker Compose is a tool with which you can easily define and start multi-container applications in Docker.

With Compose you can define all services in single YAML file, and with a single command, can spin everything up or tear it all down.

In this tutorial we will run two containers (WordPress and MySQL) side by side in an isolated containerized environment by using Docker Compose.

Now let me show you how to install the latest version of Docker Compose to help you manage multi-container applications.

First, check the current release and if necessary, update it in the command below:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Then apply executable permissions to the binary:

sudo chmod +x /usr/local/bin/docker-compose

Finally you can verify that the installation was successful by checking the version:

docker-compose --version
docker-compose version 1.29.2, build 5becea4c

For more about Docker Compose, you can refer to the official documentation.

2. Prepare Directory

You need to create a new directory called wordpress under /srv to house the WordPress data. This way we achieve a certain data persistence.

sudo mkdir -p /srv/wordpress
cd /srv/wordpress/

As you know, the data in the Docker containers is not persistent. This means that you lose all data if you stop the container and run it again, there will no longer be any data inside. Of course, this can be avoided by adding a Docker volumes, as you’ll see later in this tutorial.

3. Creating the YAML File

In the Docker Compose realm, all the resources needed to run a container must be defined in a YAML file named docker-compose.yaml. Docker Compose will then read such file and communicate with the Docker daemon to create, configure, or destroy the defined resources.

In our case, the docker-compose.yaml file will contain the service definitions for our dockerized WordPress setup. In addition, Docker Compose allows us to link these services together with shared networks and volumes.

So let’s start with creating a new docker-compose.yaml file inside the /srv/wordpress directory with an editor of your choice. Populate it with this config:

version: '3'
services:
  mysql:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: my_password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress_user
      MYSQL_PASSWORD: wordpress_password
    volumes:
      - mysql_data:/var/lib/mysql
  wordpress:
    image: wordpress:latest
    depends_on:
      - mysql
    ports:
      - 8080:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: wordpress_user
      WORDPRESS_DB_PASSWORD: wordpress_password
    volumes:
      - ./wp-content:/var/www/html/wp-content
volumes:
  mysql_data:

A little bit of explanation here:

  • We define two services, mysql and wordpress, that are automatically linked with each other.
  • They use a Docker image, which will decide which version of MySQL and WordPress to use. In this case, their latest versions.
  • MySQL environment: WordPress will use these environment variables to connect to the MySQL container.
  • WordPress environment: For a WordPress container to run, you should set the database connectivity details.
  • WordPress image is based on Apache, which runs on port 80 by derfault. Map the default Apache port to port 8080 of the local computer.
  • mysql_data: Data flowing into this database will be persisted to a named volume mysql_data, so that even when you remove the container, the data will still live on your machine and can be mounted again in a new container.

In addition, the volumes parameter under wordpress is what tells Docker to surface the wp-content directory in the local file system. We now have persistent storage for parts of our WordPress site such as wp-content directory. The wp-content contains all user-supplied content. Basically anything you can upload to your site ends up here.

However, as long as you have both the database and your wp-content folder, you can always get your site back, even if everything else was lost.

4. Run WordPress with Docker Compose

Now you are ready to run the Docker command that will build your local environment. Make sure you are still in the wordpress directory. Run the following command:

sudo docker-compose up -d

The command will begin running scripts and you should see various “Downloading” and “Waiting” messages appear in terminal while Docker is pulling in the MySQL and WordPress images. This will take a little while to run.

When you keep a close eye on your local file system, you’ll see that after the Docker images have been pulled in, the /srv/wordpress/wp-content folder will be created, and filled with files and folders.

The WordPress installation with a MySQL container and a WordPress container has been successful.

5. Access Your WordPress Installation

Finally, with our containers running, we can finish the installation through the WordPress web interface. In your web browser, navigate to http://localhost:8080 or http://your_ip_address:8080.

You may need to find out your IP address by using the ifconfig command.

You will be greeted by the language selection page. Select your language and click Continue.

Select Language

In the next screen, fill out the necessary information and click Install WordPress.

Set Configuration

Finally, when prompted, click Login.

WordPress Had Been Successfully Installed

You will then be greeted by the standard WordPress login screen. Once you’ve successfully authenticated, you can start using WordPress.

WordPress Admin Panel

Additional Docker Compose Options

First, make sure you are in the /srv/wordpress directory.

To view the status of an Docker application, execute the following command:

sudo docker-compose ps

You can stop all the running containers, by using the following command:

sudo docker-compose stop

To starts existing containers that were started with docker-compose up:

sudo docker-compose start

The command shown below will stop and remove containers and all the networks:

sudo docker-compose down

Conclusion

We introduced you to the concept behind Docker Compose and you should now be able to define even simple multi-container applications.

In this tutorial, you used Docker Compose to install a WordPress with Docker. You now have WordPress up and running.

We hope you enjoyed reading this guide. Happy Dockering!

If this guide has helped you, please consider buying us a coffee.

Buy me a coffee!

Your support and encouragement are greatly appreciated!

Leave a Reply

Your email address will not be published.

Latest from Tutorials