Building an HTTP server image

In this section, we are going to craft a Docker image in order to install Apache2 on top of the Ubuntu 16.04 base image, and configure an Apache HTTP server to run as an executable, using the ENTRYPOINT instruction.

In Chapter 3, Building Images, we illustrated the concept of Dockerfile to craft an Apache2 image on top of the Ubuntu 16.04 base image. Here, in this example, we are going to extend this Dockerfile by setting the Apache log path and setting Apache2 as the default execution application, using the ENTRYPOINT instruction. The following is a detailed explanation of the content of Dockerfile.

We are going to build an image using ubuntu:16.04 as the base image, using the FROM instruction, as shown in the Dockerfile snippet:

########################################### 
# Dockerfile to build an apache2 image
###########################################
# Base image is Ubuntu
FROM ubuntu:16.04

Set the author's detail using the MAINTAINER instruction:

# Author: Dr. Peter 
MAINTAINER Dr. Peter <[email protected]>

Using one RUN instruction, we will synchronize the APT repository source list, install the apache2 package, and then clean the retrieved files:

# Install apache2 package 
RUN apt-get update &&
apt-get install -y apache2 &&
apt-get clean

Set the Apache log directory path using the ENV instruction:

# Set the log directory PATH 
ENV APACHE_LOG_DIR /var/log/apache2

Now, the final instruction is to launch the apache2 server using the ENTRYPOINT instruction:

# Launch apache2 server in the foreground 
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

In the preceding line, you might be surprised to see the FOREGROUND argument. This is one of the key differences between the traditional and the container paradigm. In the traditional paradigm, the server applications are usually launched in the background either as a service or a daemon because the host system is a general-purpose system. However, in the container paradigm, it is imperative to launch an application in the foreground because the images are crafted for a sole purpose.

Having prescribed the image building instruction in the Dockerfile, let's now move to the next logical step of building the image using the docker build subcommand by naming the image as apache2, as shown here:

$ sudo docker build -t apache2 .  

Let's now do a quick verification of the images using the docker images subcommand:

$ sudo docker images

As we have seen in the previous chapters, the docker images command displays the details of all the images in the Docker host. However, in order to illustrate precisely the images created using the docker build subcommand, we highlight the details of apache2:latest (the target image) and ubuntu:16.04 (the base image) from the complete image list, as shown in the following output snippet:

    apache2             latest            1b34e47c273d        About a minute ago   265.5 MB
ubuntu 16.04 f753707788c5 3 weeks ago 127.2 MB

Having built the HTTP server image, let's now move on to the next session to learn how to run the HTTP service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset