Working with Docker – IV

In this chapter we will learn how to build custom docker images using Dockerfile. Basically Dockerfile is a file which describes all the steps that docker needs to prepare an image. For an example required packages to install, directories those are required to create, files required to copy from host VM etc…

Lets start with a simple example by injecting an Environment variable to the docker image and after completing the image build we will run a docker container to verify that the environment variable is available or not.

Create a Dockerfile with the below content:

First we need to understand the meaning of the above two lines before building Docker image. The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. A valid Dockerfile must start with FROM instruction. If the base image isn’t available in Local then it will pull the Base Image from the public repository.

The EVN instruction sets the environment variable <key> to the value <value>. Now we are ready to build and create a container using the newly built Docker image. Follow the below command to build and validate the docker image.

Great!! We just created a Docker image and validated about the environment variable and found it’s presence in the container created using the newly build image. Let’s remove that test image and create an image with Wordpres application on top of CentOS 7

Build Docker image to run Worpress Blog site on CentOS

To run WordPress, you will need to install MySQL, Apache 2 (i.e., httpd ), and PHP, and grab the latest WordPress release. You will need to create a database for WordPress.

In this example the WordPress database user is wordpress , its password is linuxfunda123 , and the database is wordpress . Change these settings to your liking in the  wp-config.php  file and edit the wordpress.sql accordingly.

Let’s start building our Dockerfile.

With this file, we can get a clean CentOS image, but we don’t have MySQL, PHP and Apache 2 installed. To install these we need to configure EPEL repo. For this we need to download the EPEL release from dl.fedoraproject.org . Then install it:

After configuring EPEL repository now we can install the required packages to run the WordPress.

Now we have MySQL installed, but to connect to it we need to configure it. For this we required server.cnf  file with required configurations. Create a server.cnf  file with below content:

To put this configuration file in it’s proper place use the below code in Dockerfile:

Now it’s time to create user, database for the wordpress application. For this we will create a sql file name wordpress.sql

To apply the sql file and start the MariaDB  we will create a shell script named wordpress.sh

Now in our Dockerfile we will use these files.

So, we are done with our database and database user creation. Now after this we will download the latest wordpress from wordpress.org . After downloading the compressed file we will extract it and will put to Apache 2 default document root directory. We will use our own modified wp-config.php  file with database information to connect MySQL database. Use below code to download and extract the wordpress:

Create a wp-config.php  file with below content for the wordpress application:

Now use the below code in Dockerfile to put the wp-configure.php  file to it’s proper location:

We are ready with all the required application and configurations. Just need to start the services i.e. MySQL  server and Apache 2  server. To start these services we will use supervisord .  To installed supervisord  we need to install python-setuptools  which we already installed. Now just to install supervisord  using easy_install . After installing we need to configure the supervisord . Use the below block in Dockerfile for that purpose:

We will put a supervisord.ini file with the instructions to start MySQL and Apache 2. Use the below code and create a file named supervisord.ini

Now we are done with all the installation and configuration of WordPress application. Finally we should expose the port 80 to the host VM and use CMD for the entry point of the Docker container.

At the end your Dockerfile should looks like this:

Save the Dockerfile and execute the below command to build the Docker image:

After completing the build process successfully now we will able to see the image available in our local system.

Now it’s time to test our newly built Docker image. Use the below command to spin up a new container using linuxfunda/wordpress::latest  image.

Port forwarding will be setup between your host and docker container for port 80. You just need to access http://<Your Docker Host IP>/ and you should able to see the WordPress configuration page.

In the next chapter we will learn how to run the WordPress blog using two linked containers.

The following two tabs change content below.

Tapas Mishra

Sr. Engineer (DevOps)
Loves to work on Opensource products. Having experience on Linux environment. Knowledge on Public cloud services like AWS, Rackspace, DigitalOcean, Linode. Please don't hesitate to give a comment on the posts. Your comments are my strength.

Latest posts by Tapas Mishra (see all)

Leave a Reply