Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Install WordPress with Docker on Ubuntu 16.04  (Read 1012 times)

0 Members and 1 Guest are viewing this topic.

jibinw

  • Guest
Install WordPress with Docker on Ubuntu 16.04
« on: April 04, 2018, 12:42:41 am »
WordPress is a famous content management system based on PHP and MySQL, distributed under the terms of the GNU GPLv2.
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.

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:

Code: [Select]
# apt install -y --no-install-recommends apt-transport-https ca-certificates curl software-properties-common
Next, add Docker’s official GPG key:

Code: [Select]
$ curl -fsSL https://apt.dockerproject.org/gpg | sudo apt-key add -The key ID should be 58118E89F3A912897C070ADBF76221572C52609D, so verify:
Code: [Select]
$ apt-key fingerprint 58118E89F3A912897C070ADBF76221572C52609DSet up the stable repository using the following command:

Code: [Select]
# 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:
Code: [Select]
# apt update
Then:

Code: [Select]
# 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:

Code: [Select]
# 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:

Code: [Select]
# chmod +x /usr/local/bin/docker-compose
Test:

Code: [Select]
$ 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:
Code: [Select]
$ 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:

Code: [Select]
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:

Code: [Select]
# 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:

Code: [Select]
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:

Code: [Select]
# 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

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.