How to create Docker image using Dockerfile

How to Create Docker image using Dockerfile

In our previous articles we have discussed how to create docker image mannually. We have also learn How to install docker and manage the docker. Today, we will discuss about How to Create Docker image using Dockerfile

A dockerfile is a text file that contains the necessary commands to assemble an image. Once a Dockerfile is created, the administrator uses the docker build command to create an image based on the commands within the file. You can use a Dockerfile to automate installation and configuration of an image and its dependencies.

What is a Dockerfile?

  • Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
  • A Dockerfile is a text configuration file written in a popular, human-readable Markup Language called YAML
  • It is a step-by-step script of all the commands you need to run to assemble a Docker Image.
  • The docker build command processes this file generating a Docker Image in your Local Image Cache, which you can then start-up using the docker run command, or push to a permanent Image Repository.

How to create Docker image manually

Let’s check some Dockerfile command which help us to write the Dockerfile

Dockerfile Commands

FROM – Select the base image to build the new image on top. Docker command will search this image from local image repository if it is not available in local repository then it will fetch from Registry server

FROM centos:latest

Maintainer  Optional field, This command is used to give the information about the author or manager who is managing this image. Define the full name and email address of the image creator.

MAINTAINER Ghansham Mahajan

RUN - Before building an image if want some configuration that needs to be present in the image. Inside the image we need to install Apache web server image the command to install that image is. Simple terms, RUN keyword will be executed during creation of Docker image. This includes updating packages, installing software, adding users, creating an initial database, setting up certificates, etc.

RUN yum –y install httpd

COPY– This command is used to copy a file from host os to docker container

COPY index.html /var/www/html

ADD – Defines files to copy from the Host file system onto the Container

ADD /var/www/html

There are two major differences between ADD and COPY:

  • ADD can also take a URL as <source>.
  • If the <source> parameter of the ADD instruction is an archive in a recognized compression format, it will be unpacked.
  • COPY instruction will only copy the archive file, without unpacking it.

EXPOSE – Define which Container ports to expose. This command is used to specify the port number in which the container is running its process. Anybody can come from outside and connect to this port. We can expose application port from Container. Apache webserver is launched at port 80 by default that is why we need to expose container at port 80.


CMD – To run a command as soon as container is launched. CMD command is different from RUN because RUN is used at the time of building an image and CMD used to run command when container is started.

/usr/sbin/httpd– This command is used to start the web server

-D FOREGROUND — This is not a docker command this is http server argument which is used to run webserver in background. If we do not use this argument the server will start and then it will stop.

CMD [“/usr/sbin/httpd”,” -D”,” FOREGROUND”]


CMD ["/bin/bash"]

ENV – With ENV keyword we can set the environmental or shell variables as per requirement, let’s suppose I want build Docker image where I will install java and need to set JAVA path, this can be achieved by using ENV keyword.

JAVA_HOME= /usr/jdk/jdk{version}



USER – Define the default User all commands will be run as within any Container created from your Image. It can be either a UID or username

USER ghansham

WORKDIR – The WORKDIR instruction is used to set the default working directory for the container.

WORKDIR /var/www/html/

VOLUME – With VOLUME keyword we can attach a folder from Docker engine host to a container.

 VOLUME  /home/ghansham/docker /var/www/html

Step 1: Create a directory

I have created the directory under user home directory.

~]$ mkdir newimage
~]$ cd newimage 
How to create Docker image using Dockerfile

Step 2: Now create a file `Dockerfile` (File name is hard coded do not change the file name) Make sure file name Dockerfile “D” should be always Capital.

]$ touch /home/ghansham/newimage/Dockerfile
How to create Docker image using Dockerfile

Step 3: Create a sample web page with name index.html

]$ vi index.html
How to create Docker image using Dockerfile

Step 4: Edit the file using following instructions

]$ vi Dockerfile

FROM centos:latest 

MAINTAINER Ghansham Mahajan 

RUN yum -y install httpd 

COPY index.html /var/www/html/ 

CMD [“/usr/sbin/httpd”, “-D”, “FOREGROUND”] 


Step 5: Build the image using docker build. /home/ghansham/newimage is the directory where Dockerfile is present and –t option is to tag or name the image.

]$ docker build /home/ghansham/newimage -t ghanshamdocweb:v2

Step 6: Run docker image

]$ docker run -dit -p 8080:80 ghanshamdocweb:v2

Step 7: To See the Result on web browser

http://<host ip>:8080/

Use your Host IP address and port no. I used 8080 port which will redirect to docker container apache 80 port.

Read Our Previous Post :

Working with Docker in CentOS 7/RHEL 7

How to manage Docker containers on CentOS/RHEL 7.x

How to install Docker on CentOS 7.x

Docker Document Reference : Docker Docs

Hopefully “How to create Docker image using Dockerfile”, this will helps your understanding. More post related to Docker

Tagged , , , , , , , . Bookmark the permalink.

Comments are closed.