How to install Wordpress using Docker

Overview

In this article we’re going to go through the steps how to install Wordpress using Docker. In other words, we’re going to install Wordpress with a Docker container and also with Docker compose. The scenario for the following procedure will look as follow: Singe host machine(one debian server) and a single docker engine running. We’re going to install an official Wordpress docker container on our host and setup the data persistency as well. This method of course will work on other linux host machines as long it runs docker engine.

Requirements:

  • Linux host
  • Docker installed and running.

Install Docker

For docker installation we can use the official auto-install Docker script if you’re using Debian, Ubuntu, Raspbian as a host. Otherwise refer to the Docker docs or this post.

curl -fsSL https://get.docker.com -o get-docker.sh  
sudo sh get-docker.sh

Add docker to your user group:

sudo usermod -aG docker <username>

Install Wordpress using Docker container

To install Wordpress docker container, we can use the following commands:

docker run --name some-wordpress -p 8080:80 -d wordpress

This will deploy one Docker container which will run the web server, Wordpress installation, PHP and the MySQL(MariaDB) database server. This option is not an ideal deployment, mostly being that everything on it, even website data will remain and be stored inside the container and every time the container is restarted, all the data will be lost. That’s why it’s a must to have persistant data for Wordpress(storing data on the host). For that kind of deployment, it’s suggested to run the Wordpress containers like the following:

DB container:  
  
docker run -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=wordpress --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mariadb:latest --restart:unless-stopped  
  
Wordpress container:  
  
docker run -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=<password> --name wordpress --link wordpressdb:mysql -p 80:80 -v "$PWD/html":/var/www/html -d wordpress --restart:unless-stopped

In this kind of deployment, we deployed database and wordpress containers separately. Breaking down the commands:

DB container:

-e: with -e argument we set the environment variables which will be in this case the database credentials and parameters(the password and the database name).

–name: name argument is how are we going to set the name for our database container name.

-v: with this argument we set and mount a directory on our host server where the container is going to save all database data and not within the container. This parameter is important as this will set the data persistancy.

-d: with this argument we select our docker container image, in this case we used the latest mariadb database image.

–restart: Argument which will instruct Docker daemon to restart the container every time when the container is stopped.

Wordpress container:

-e: for the Wordpress docker container we set environment variables which are also database container parameter so that the Wordpress can access the database.

–name: also an argument to set the name for the wordpress container.

–link: database link argument(to set our database connection from Wodpress to the MariaDB container).

NOTE: Regarding the –link argument, Docker announced this will be sometime in the future the legacy feature and it may be removed in the future Docker updates and it may break your database container connection. For that reason, if you plan to have this kind of deployment, it’s prefferred to deploy this Wordpress setup with the Docker compose or as a stack where the Wordpress will depend on the docker network to connect to the MariaDB database. This is covered in the next chapter.

-p: Exposed port variable. Which ports to expose on the host and on the container in order for the Wordpress to be accessible publicly.

** -v:** The mount point on the host machine where are we going to add a directory from the host and save the Wordpress files - aka data persistancy.

-d: The docker image we’ll be using. 

–restart: Argument which will instruct Docker daemon to restart the container every time when the container is stopped.

Docker compose version

Install Docker compose:

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  
  
sudo chmod +x /usr/local/bin/docker-compose  
  
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

install wordpress using docker compose

Refer to the docker docs for latest docker compose release.

You can create a separate directory for this compose file and inside the directory to created the Docker compose file:

mkdir wordpress && cd wordpress/  
  
touch docker-compose.yml && nano docker-compose.yml

You can use the following yaml configuration:

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:

install wordpress using docker

The config file is pretty similar the command lines to deploy the container from the previous chapter.

The deploy the stack, while still in the compose file directory, run the command:

sudo docker-compose up -d

It will take couple of minutes to finish the deployment. 

Deploy as stack

To deploy Wordpress as stack with Docker or docker compose, name the config file as stack.yml and run on of the following commands:

Docker  
  
docker stack deploy -c stack.yml wordpress  
  
Docker compose  
  
docker-compose -f stack.yml up

Regardless what step you choose for the deployment, the successful result should look something like this: 

install wordpress using docker

And after you can access the Wordpress installation via the browser:

install wordpress using docker

NOTE: If you have a running UFW or IPTABLES firewall, it’s necessary to open the proper ports(the ports you exposed for the host machine) in order to access the Wordpress via browser:

UFW  
  
sudo ufw allow 8080  
  
IPTABLES  
  
sudo iptables -I INPUT -p tcp -m tcp --dport 8080 -j ACCEPT  
  
or  
  
sudo iptables -A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT  
  

Summary

To summarize the article - we showcased the steps how to install Wordpress using Docker and also how to install Wordpress using Docker compose. As you noticed, we have several ways how to delpoy Wordpress using Docker and Docker compose. But overall, much preferred method is with having a separate container for Wordpress and for MariaDB/MySQL server. For simpler scalability in the future and also data persistancy is a must.

Thanks for your time…