How to install WordPress with Docker on Ubuntu 16.04

WordPress with Docker on Ubuntu 16.04

Introduction

For those who don’t know, WordPress is a famous content management system based on PHP and MySQL, distributed under the terms of the GNU GPLv2 (or later).

Usually it is installed on a web server like Apache, but it is also possible to run it on an isolated environment built with Docker containers, in particular, using Docker Compose.

This is the subject of this tutorial, using Ubuntu 16.04 as the Operating System.

Getting started

First of all, it’s necessary to install Docker and Docker Compose. On Ubuntu 16.04, this can be done in two different ways:

  • Set up repositories and install from them, for ease of installation and upgrading tasks
  • Downloading the DEB package and installing it manually; also allowing you to manage upgrades completely manually

In this tutorial Docker will be installed using the repository method. So, you’ll need to install packages to allow

apt

to use a repository over HTTPS:

# apt install -y --no-install-recommends apt-transport-https ca-certificates curl software-properties-common

Next, add Docker’s official GPG key:

$ curl -fsSL https://apt.dockerproject.org/gpg | sudo apt-key add -

The key ID should be 58118E89F3A912897C070ADBF76221572C52609D, so verify:

$ apt-key fingerprint 58118E89F3A912897C070ADBF76221572C52609D

Set up the stable repository using the following command:

# add-apt-repository \
       "deb https://apt.dockerproject.org/repo/ \
       ubuntu-$(lsb_release -cs) \
       main"

Now it’s possible to install Docker.

First, update apt package index:

# apt update

Then:

# apt install docker-engine

This will install docker and its daemon should start automatically.

Install Docker Compose

After installing Docker, the next step is to install Compose, which is required for this process. Just execute the command:

# curl -L https://github.com/docker/compose/releases/download/1.11.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

Change permissions to docker-compose binary:

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

Test:

$ docker-compose --version

Now Docker and Docker Compose are installed and ready to be used.

Install MariaDB

Create an empty directory, for instance

docker_wordpress

.
Then change into it:

$ cd docker_wordpress

Create a docker-compose.yml file that will start your WordPress blog and a separate MySQL instance with a volume mount for data persistence.
In this file, enter the following text:

version: '2'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       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_PASSWORD: wordpress
volumes:
    db_data:

Next, in the 

docker_wordpress

folder, start the container using the following command:

# docker-compose up -d

It’s just that easy because the Docker team ensures that everything is well configured. In fact, there’s a script inside the WordPress Docker container that reads the MYSQL_ROOT_PASSWORD variable from the wordpress container and uses that to connect to WordPress.

Install PHPMyAdmin

Adding PHPMyAdmin it’s no different than adding a database. It is possible to use a community driven docker image. In the

docker-compose.yml

file, just add the following lines under the “services” section:

phpmyadmin:
image: corbinu/docker-phpmyadmin
  links:
    - wordpress_db:mysql
  ports:
    - 8181:80
  environment:
    MYSQL_USERNAME: root
    MYSQL_ROOT_PASSWORD: wordpress

Save these configurations and run the docker-compose command to create and start the container:

# docker-compose up -d

Configuration is almost complete! With a web browser, go to the URL: http://SERVER_IP:8181. It will show the login screen of PhpMyAdmin. Login using the same credentials that have been configured in the 

docker-compose.yml

file.

Conclusion

That’s all! Now the server is running WordPress in a secure and isolated container. Although Docker is a “tool for developers”, it can be used for various projects, just like the one shown here. Of course, configuration files can be edited and customized with more fine-grained details, like a DNS section and some hardware limits like CPU and memory usage. Have fun with it!