Containerisation with Docker – Part One

Running applications and services in their own containers has become popular recently. Containers have several advantages over virtual machines including the lack of requirement of another licence and no need to dedicate resources to the virtual machine.

Docker is a tool that allows us to create containers.

I recently obtained a VPS server from IONOS to use to do a little bit of research on various topics and decided that Docker would be a good starting point. The VPS server administration panel included Plesk and I was able to install Docker using Plesk.

Using the GUI in Plesk I created containers for MariaDB and WordPress because my first intention was to create a WordPress website that I could use to host a blog (wonder how that turned out?!).

Whilst the GUI allowed me to do a certain amount of configuration of the containers for each of the applications it soon became apparent that I would need to use the command line to really get the most out of Docker.

As an aside it’s interesting how much more prevalent the use of command line tools is these days, when I started working in IT command line tools were already practically out of fashion but they are back with a vengeance these days. I guess Powershell might have kickstarted that in the Microsoft stack and obviously working with Linux or Unix based systems has always been easier with the command line.

The first thing that I needed to do was to create folders on the host machine that could be mapped across into the containers to provide persistent storage. A container effectively starts from scratch each time it is started so for anything to persist it needs to be stored on the host machine and the location of that storage needs to be mapped into the container. Using an SSH connection to the VPS I created directories that could be used to hold data for the MariaDB and WordPress containers.

I could then use the environment variables configuration in Plesk to map these new directories so that they were available to the Docker containers. Starting the containers everything seemed to be good but WordPress needs to know where the database it can use is as it starts up and currently I had two containers and needed to get them to talk to each other.

The way to establish connection between the two containers was to create a network and then add the two containers into that network using the following three commands:

docker network create [networkname]
docker network connect [networkname] [dbcontainername]
docker network connect [networkname] [wordpresscontainername]

Now both containers can talk to each other using the logical names attributed to them in the environment variables.

All that was left was to configure a rule in Plesk to forward requests to the domain name through to the WordPress Docker container and to then start both containers. When I accessed the domain name I was then able to complete the WordPress installation.

The final test was to stop both containers and check that when they restarted the installation was still all set up – I tested it and it was so my first Docker containers were up and running.