Dockerfile: To automate Docker image creation

We have already learned how to install Docker and create an image for the container. In this article, we will learn how to automatically build docker images with the help of Dockerfile.  Dockerfile is a text file that contains the codes to build a Docker image. Before you read this article I recommend you to go through the below article. It gives you an introduction to Docker containers.

Part1: Docker installation and configuration

Part2: How to create an image from the container

Part3: Dockerfile: To automate Docker image creation

Here, we will create a Docker image for running a static website. As you know to run this static website we need an OS and apache webserver. To create this image we will user Dockerfile.

Create a file named Dockerfile and add the following commands

#vi Dockerfile
FROM ubuntu:12.04

MAINTAINER Techiescorner

RUN apt-get update && apt-get install -y apache2 && apt-get clean

ENV APACHE_LOG_DIR /var/log/apache2

RUN echo "Website loading from Docker" > /var/www/index.html


CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

We have started with the command FROM, This command downloads the base image which is ubuntu 12.04

Next, we have declared the MAINTAINER name

RUN instruction is used to execute Linux commands. Here we used  RUN command for downloading apache packages and execute echo.  The echo command copies the sentence “Website loading from Docker” to /var/www/index.html file. So we can easily understand the site is loading the container that we created.

ENV command sets the environmental variable to the value. Here ENV command set the value “www-data”  to the variables APACHE_RUN_USER and APACHE_RUN_GROUP

EXPOSE instruction is used to open the port for the container.

CMD  Finally we use CMD to tell Docker which command will be run on startup of the container.

Now we learned all the instructions used in our Dockerfile, next we will build an image with the help of this Dockerfile.  Run the following command to build an image

#docker build -t ubuntu/apache .

The “dot” stands for the Dockerfile location. You have to execute the command from the Dockerfile location or mention the complete Dockerfile path.

sample output:-

[root@localhost ~]# docker build -t ubuntu/apache .
Sending build context to Docker daemon 121.9MB
Step 1/9 : FROM ubuntu:12.04                      
 ---> 5b117edd0b76                                 
Step 2/9 : MAINTAINER Techiescorner
 ---> Using cache
 ---> 98e66ff376ab
Step 3/9 : RUN apt-get update && apt-get install -y apache2 && apt-get clean
 ---> Running in f679470b35df
Get:1 precise Release.gpg [198 B]
Setting up apache2-mpm-worker (2.2.22-1ubuntu1.11) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up apache2 (2.2.22-1ubuntu1.11) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
 ---> c469b4a1624e
Removing intermediate container f679470b35df
Step 4/9 : ENV APACHE_RUN_USER www-data
 ---> Running in 808671033a0a
 ---> aecf55934fab
Removing intermediate container 808671033a0a
Step 5/9 : ENV APACHE_RUN_GROUP www-data
 ---> Running in 9f6e2e7f9e20
 ---> 2fe59a11358e
Removing intermediate container 9f6e2e7f9e20
Step 6/9 : ENV APACHE_LOG_DIR /var/log/apache2
 ---> Running in 4a795b3db4a3
 ---> 3ad4e8d39647
Removing intermediate container 4a795b3db4a3
Step 7/9 : RUN echo "Web site loading from Docker" > /var/www/index.html
 ---> Running in 2f6b503c98f7
 ---> b8ab4516f668
Removing intermediate container 2f6b503c98f7
Step 8/9 : EXPOSE 80
 ---> Running in c4619f329fbb
 ---> 2aa92fa24e8d
Removing intermediate container c4619f329fbb
Step 9/9 : CMD /usr/sbin/apache2 -D FOREGROUND
 ---> Running in 3036fda0ca09
 ---> a79751964035
Removing intermediate container 3036fda0ca09
Successfully built a79751964035
Successfully tagged ubuntu/apache:latest

To see the newly created image, execute

Here Ubuntu is our base image and ubuntu/apache is the newly created image with the help of Dockerfile.

To create a container from this image execute the following command

# docker run -d -p 80:80 --name webserver ubuntu/apache

It creates the container “webserver ” and expose the port 80. “-d” used to run in the background

To confirm our site is loading from the container access the IP address in the browser

The content that we copied to index.html in Dockefile displaying on the browser.



  1. Pingback: How to create an image from the container |

  2. Pingback: Docker container installation and configuration |

Leave a Reply

Your email address will not be published. Required fields are marked *