Technologie im Fokus

Port WordPress into a Docker-enabled environment – Part 1

P

docker-wordpressCommon content management systems, like WordPress, are very widespread applications. For this tutorial let’s assume you have an older version of WordPress up and running. The system is hosted on an Ubuntu server or similar and all necessary prerequisites are installed directly on the host. Part one will illustrate how the transformation of our previous company website into our current one was accomplished with Docker. You’ll learn how to take advantage of docker-compose for harmonizing all the needed applications in one single command.

The Old Environment

In our old environment, we are using an Ubuntu server. On this machine, there are multiple websites from our customers, including our own. These sites are static as well as dynamic and are accessing the same resources, like an Apache or SQL server. Whenever you’re working in a shared environment like this, you have to keep in mind that each problem might affect every website on the system! This is one of the major topics we’re going to tackle in the upcoming change to the Docker universe.

The Cool New Docker Environment

Our new environment is also an Ubuntu server, this time with Docker installed on it. So far so good, but what’s the benefit? As mentioned earlier, in order to run WordPress we need Apache or any other Reverse-/Proxy and an SQL database. Docker container technology provides the means to isolate every application in one container, instead of using one application for every website. In a setup like this, if a website has a problem or is attacked, all the other websites are not affected! There are further advantages like scalability, re-usability, maintainability and more. You can check out the Docker Website for more information.

Prepare your server

In order to get Docker up and running, you need only a few commands:

sudo apt-get -y install \
     apt-transport-https \
     ca-certificates \
     curl

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 

sudo add-apt-repository \
     "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
     $(lsb_release -cs) \ 
     stable"

sudo apt-get update

sudo apt-get -y install docker-ce

Next, we also need to install „docker-compose“:


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

That’s it! The environment is set up and ready to use.

Preparing images

The key applications for our website are split into 3 containers. One for WordPress, one for the database and one for the Nginx container.  The database doesn’t need any special settings, but WordPress and Nginx do need some tweaks. For simplicity reasons, we won’t discuss this in detail, but the configurations are well-known best practices and can be found online.

For WordPress and Nginx, we are going to create two Docker files with custom configurations.


FROM wordpress:php7.1-apache

COPY config/php.ini /usr/local/etc/php/


FROM nginx:1.11.13

COPY config/nginx.conf /etc/nginx/nginx.conf
COPY config/default.conf /etc/nginx/conf.d/default.conf

Keep in mind that you need to provide the config files, relative to the directory of the Dockerfiles. If you want, you can leave out the „nginx.conf“ and the „php.ini“ file by uncommenting the lines before you go on. You must use the default.conf file though, otherwise Nginx can’t find the WordPress instance.

The default.conf file looks like this:

server {
 listen 80;
 server_name *.YOUR_DOMAIN.COM;
 return 301 https://$host$request_uri;
}
server {
 listen 443;
 server_name YOUR_DOMAIN.COM;
 location / {
    proxy_pass http://wordpress:80/;
    proxy_set_header Host $host;
    index index.php index.html index.htm;
 }
 
 # redirect server error pages to the static page /50x.html
 #
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }

}

Harmonizing with docker-compose

Now we have to put all the files and images into containers and stick them together, so they can see each other, but no one else can. We do this with docker-compose. It’s a command structure, which executes instructions from a docker-compose.yml file.

version: '3.1'

services:
#########################################
# SQL # 
#########################################
   db:
     image: mariadb:latest
     hostname: db
     volumes:
      - ./data/db:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: admin
       MYSQL_DATABASE: wp
       MYSQL_USER: wp
       MYSQL_PASSWORD: wp
#########################################
# WORDPRESS # 
#########################################
   wordpress:
     build:
       context: dockerfiles
       dockerfile: dockerfile-wordpress
     hostname: wordpress
     restart: always
     depends_on:
       - db 
     ports:
       - "80"
     volumes:
       - ./data/wordpress:/var/www/html
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wp
       WORDPRESS_DB_PASSWORD: wp
       WORDPRESS_DB_NAME: wp
       VIRTUAL_HOST: YOUR_DOMAIN.COM

 
#########################################
# Nginx-Proxy # 
#########################################
   nginx:
     build:
       context: dockerfiles
       dockerfile: dockerfile-nginx
     hostname: nginx
     restart: always
     depends_on:
       - wordpress
     ports:
       - "80:80"
     volumes:
       - /etc/nginx/conf.d:/etc/nginx/conf.d

Change the highlighted domain name to your custom one and make sure, you have your dockerfiles in a sub-directory relative to the docker-compose.yml file! Otherwise, Docker can’t find them and will abort the build process.

Start the engines

The next step is easy as only one command will do the trick. Before you proceed, make sure your folder structure looks like this:

├── docker-compose.yml
├── dockerfiles
│             ├── config
│             │             ├── nginx.conf
│             │             └── php.ini
│             │             └── default.conf
│             ├── dockerfile-nginx
│             └── dockerfile-wordpress 

With the following command, Docker will build the images, create all containers and put them on a segregated network. Only Port 80 will be visible on the host machine. The -d flag signals to run the containers in the background.

docker-compose up -d --build

After a few seconds – depending on how fast your internet connection is – the download and image building process is completed and your application will start. To reach the website, adapt the hosts file with the IP address of YOU_DOMAIN.COM and you’re done. I used a special formatted command to show a shorter version of the running containers.

As you can see, all three containers are up and running:

Wordpress Docker-Containers
Shows all running containers

You can stop your application with the following command:

docker-compose down

Where are my files?

Containers are ephemeral with the major advantage that there’s no residue and no overlapping with other containers whatsoever. Usually, we don’t want to lose our data and want to keep it in a safe place with possible backups, so we need volumes.  To keep things simple, we will use host-mounted volumes, relative to the path of your docker-compose file.

    ...
    volumes: - ./data/wordpress:/var/www/html
    ...

So that’s where our data is residing. Feel free to change it to any other location best suiting your needs.

What´s next?

In this basic setup, we have learned to set up a WordPress instance. To keep things tight, we skipped any SSL security whatsoever in our example. Part 2 will explain how to set up a „https“ environment and provide state-of-the-art safety to all your data streams. We will also show you how to port the old WordPress Instance to the new one.

Port WordPress to Docker-enabled environmentAdvanced

Über den Autor

Andreas Schmidhuber

Andreas Schmidhuber arbeitet seit 2016 bei artindustrtial it als System & Data Center Manager und Presales Engineer.
Seine Schwerpunkte liegen auf Cloudcomputing, Docker sowie Betrieb und Modernisierung der Infrastruktur.

Von Andreas Schmidhuber
Technologie im Fokus

Hier finden Sie uns

Adresse
Mariahilferstrasse 111/6-7
1060 Wien
Kartenansicht

Öffnungszeiten
Montag – Freitag: 9–17 Uhr

Kontakt
Mail: it@artindustrial.com
Tel.: +43 1 5954023

Folgen Sie uns