Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Docker – Concept of Dockerfile

  • Last Updated : 28 Jul, 2021

Dockerfile is a simple text file with instructions to build a Docker image.  As shown below dockerfile is a simple text file where we give some instructions to build an image. And when we run docker build command a file image gets created.

If you want to create your own image you can use a dockerfile. So dockerfile is basically automation of docker image creation and there are some basic instructions that you use in the docker file. Let’s check out how it’s done.

Creating a Dockerfile:

Follow the below steps to create a dockerfile:

Step 1: Create a file name called “Dockerfile”.By default when you run the docker build commands docker searches for a file named Dockerfile. However, it is not compulsory, you can also give some different names, and then you can tell the docker that this particular file is a local file but for now we will go with the Dockerfile.



Step 2:  The very first instruction that a dockerfile starts with is FROM. Here you have to give a base image. So for example, if you want to get a base image from ubuntu we will use FROM ubuntu.  

FROM ubuntu

Then the other instruction is you have to give a MAINTAINER. This is optional but its best practice that you give the maintainer of this image so that it is very easy to find out who is the maintainer and you can give your name and email as well. And if you want you can just give the email as well without giving the name. But here we are giving the entire thing here. 

MAINTAINER YOUR_NAME <YOUR_EMAIL_ID>

Next, we want to run something so we will say run any command we can use RUN and add the command that you need to run.

RUN apt-get update

. And if you want to run something on the command line during container creation you can give CMD and inside square brackets, we add the command. Here it ba as shown below:

CMD ["echo", "Hello Geeks!"]

At this point the file will have the following commands:

FROM ubuntu
MAINTAINER YOUR_NAME <YOUR_EMAIL_ID>
RUN apt-get update
CMD ["echo", "Hello Geeks!"]

Step 3: Now we have to build the image so here are the commands you can use:

docker build /<FILE_LOCATION> 

Or,

docker build . -f Dockerfile.txt

It says docker build and you have to give the location of your docker file.  This will start building the image.

Now if we use the below command, we can check if the docker image has been created:

docker images

So we have successfully created a Dockerfile and a respective Docker image for the same.

My Personal Notes arrow_drop_up
Recommended Articles
Page :