Docker: Using “Dockerfile” to Automate Image Building

Share At:

In my previous article titled “Docker: Advance Docker Commands and Pushing Image to Docker Hub,” we have learned about some advanced docker commands and how to push your image to “Docker Hub”.

In this article, we are going to learn about automatic image building using Dockerfile.

Dockerfile is a script/text configuration that contains a collection of commands and instructions that will be automatically executed in sequence in the docker environment for building a new docker image. This file is written in YAML.


  1. For our demonstration, we will be using centos-07.
  2. We will be using 1 machine for our lab with the below IP details:

3. The memory should be at least 2 GB and there should be at least 2 core CPU

4. We should have already installed Docker (Please read my previous tutorial titled “Docker: Installing Docker and Understanding basic docker commands”on how to configure docker”).

Building an image from a Dockerfile to configure Nginx Webserver

Please perform below steps:

  1. Please create a directory called “nginx”.

2. Now change to nginx directory and create our homepage called “index.html” with below details:

3. Now create a “Dockerfile” inside the same nginx directory. The content of Dockerfile should look like below:

Let’s understand the Keywords used in the Dockerfile:

  • FROM: The FROM keyword defines the base image to use to start the build process. It can be any image, including the ones you have created previously. If a FROM image is not found on the host, Docker will try to find it (and download it) from the Docker Hub or other container repository. It needs to be the first command declared inside a Dockerfile.
  • MAINTAINER: This non-executing command declares the author, hence setting the author field of the images. It should come after FROM.
  • RUN: The RUN Keyword is the central executing directive for Dockerfiles. It takes a command as its argument and runs it to form the image. Unlike CMD, it actually is used to build the image (forming another layer on top of the previous one which is committed).
  • COPY: The role of the COPY command is to duplicate files/directories in a specified location in their existing format.
  • ENV: The ENV keyword is used to set the environment variables (one or more). These variables consist of “key-value” pairs which can be accessed within the container by scripts and applications alike.
  • EXPOSE: The EXPOSE keyword is used to associate a specified port to enable networking between the running process inside the container and the outside world.
  • CMD: The Keyword CMD, similarly to RUN, can be used for executing a specific command. However, unlike RUN it is not executed during build, but when a container is instantiated using the image being built. Therefore, it should be considered as an initial, default command that gets executed with the creation of containers based on the image.

4. Now run “docker build -t webserver:v1 .” from the nginx directory where we have created our “Dockerfile”. Here, we are building an image called “webserver” with tag as “v1”.

5. Now we can see that an image called “webserver” has been created:

6. Now run the “docker run -d -p 84:80 webserver:v1” command to start a container in background mode. Here we have used “p 84:80” , which means it will map the port 80 of our container to port 84 of our host machine.

7. The “docker ps” command shows that the container is up and running.

8. Now executing the “curl” command on our host machine displays our homepage.

9. Alternatively, you can open your web browser and type “” . It should display our homepage:

That’s it, we are done. Hope you have enjoyed the tutorial.

Happy Learning !!!

Share At:
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Back To Top

Contact Us