What are Docker File Learns To Use Them? | Simple Demo – Part 6

In the previous article, we learned different things like downloading images from Docker Hub, creating our custom images, staring services inside the container, delete services and much more. We also learned to spin up containers downloaded from Docker hub such as CentOS.

We all know that Docker facilitates to create your own Docker images, and the process of creating or building Docker Images can be done with the help DockerFiles also. Docker File is a simple text file with instructions on how to build your images.

Just in case, if you did Miss Previous tutorials regarding Docker, You can read here:

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 3B: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B
Part 4: Learn To Install, Run And Delete Applications Inside Docker Containers – Part 4
Part 5: Learn To Push Custom Build Docker Image On Docker HUB (Repository) 

Scenario :

Host OS: CentOS 7 x86_64
Host IP: 192.168.1.188
RAM: 4GB memory,
Network Port: 1GB/s
HDD: 1000GB of disk space.


This article will give you a small example/explanation/introduction to create and use DockerFile. We will also take a deep dive in creating Docker Images with DockerFiles in next Article. So read and wait for next article for a deep and bigger example.

So let’s start

Take one more step ahead to understand the Docker and working with DockerFiles. Follow the following steps.

Step 1:  Create Docker File

We will keep our directory specified so we need to create one and While creating a Docker file is it important to create it starting with a Capital “D”. Let’s say our Docker file name is “DockerFile1”

# mkdir -p /var/docker/customImages/centos1/ 
# touch /var/docker/customImages/centos1/Dockerfile
# vi /var/docker/customImages/centos1/Dockerfile

Step 2: Configure Your DockerFile

Now to create a Docker file there are certain command and keywords which are used to create DockerFile. Every line and keyword have its own meaning and parameter. For this example, I am using few simple commands and keyword for an understanding so use the following instruction to create it.

#This is a sample Image
FROM centos
MAINTAINER  kapendra  [email protected]
RUN yum -y install dos2unix
RUN echo "Done Successfully"

Now save and quit this file using :wq!

Let’s Understand what we have just done

  1. The line starting with “#”   “#This is a sample Image” is a comment. And you can comment as many as comment you require, all you have to do is to start with a #
  2. Keyword “FROM”  tell the name of the base image. In this example, we are using centos to create our new image.
  3. Keyword “MAINTAINER” , tells that who will maintain this image so just mention the name and email ID.
  4. Command “RUN” is used to run instructions against the image like in this example install dos2unix on our CentOS image.
  5. Command “CMD”  is just to print a message to the user like your are done or Successfully Completed.

Step 3: Build Our Image

As in Step 2, we have created and saved our DockerFile and now we need to build this DockerFile. Use the following command to build or Docker Image.

# docker build -t centos-cmd /var/docker/customImages/centos1/

This command will download the CentOS image from DockerHUB. If there is no image available locally on the machine.

Let’s Understand what are we doing here:

  1.  “-t” is to mention a tag to the image
  2. centos-cmd is the Image Name you want to give to your image.
  3. 1.0 is the Tag Name you want to give to your image.
  4.  /var/docker/customImages/centos1/ is the directory where the Your DockerFile is present.

If you are in the same directory where DockerFile is present then you should use “.” at the end of the command in place of “/var/docker/customImages/centos1/”  to signify that you are in present working directory.


Step 4: Check your work

After the successful completion of the command, you would see that new Docker Image with dos2unix installed inside so to check out our work follow the commands.

[root@localhost ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-cmd latest 074ec8634650 6 minutes ago 299.1 MB
docker.io/centos latest 36540f359ca3 16 hours ago 192.5 MB
docker.io/ubuntu latest d355ed3537e9 2 weeks ago 119.2 MB
[root@localhost ~]# docker run -it centos-cmd whereis dos2unix
dos2unix: /usr/bin/dos2unix

Now you can you can now build containers from your new Image.

In next article, we will go little advance for to Automate the Build and Configure Custom Docker Images with DockerFile.

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 3B: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B
Part 4: Learn To Install, Run And Delete Applications Inside Docker Containers – Part 4
Part 5: Learn To Push Custom Build Docker Image On Docker HUB (Repository)