What is Dockerfile?

What is Dockerfile?

A piece of detailed information about Dockerfile like the role of this file and how it works.

Β·

4 min read

Dockerfile, A file named with this title lies in every project's relationship with docker. This file contains a bunch of lines that look like pseudo code. Hence this blog will cover what exactly it does and how it works.

What is Dockerfile?

This file consists of simple instructions for building a docker image. It also has some syntax we must maintain to work it as expected. The sample picture is attached below to reference how the docker file looks.

You must be wondering why we need this file in our project.

Why do we need Dockerfile?

First of all, this file helps automate the building of any image. Automating means that we do not need to build the image by firing commands one by one. We simply need to hit only one command "docker build" and bam... your build will be ready to use in a minute or so.

Where Dockerfile resides?

This file lives in the root folder of every project, so it is easy to look up and work with. Also, by default when we run docker build it grabs this file from the root folder itself, just like where all other config files reside.

When is Dockerfile called or triggered?

Dockerfile is triggered whenever we build a new image, This file helps in giving the step-by-step instruction to build up the image in the form of layers stacking on top of it. It’s like firing commands one after another.

How Dockerfile works?

As mentioned previously that there are some steps/instructions mentioned in this file, hence when the final build is created of the project the commands given in this file will run step by step.

Just like we have layers of the atmosphere, docker images are created just like stacking layers of instruction, With every successful command a new layer is added to building that image.

Basics of Writing a Dockerfile.

Now, what are the things that we need to add to this file to create a proper working docker image?

  • Add a base image like ubuntu in which our application will run.

  • Specify the working directory

  • Add a copy statement to reduce the time to install existing files.

  • Specify a Cache layer as well so that the container installs only changed/modified files.

  • Run installation process

  • Start the process of execution of our logic.

When you build an image with the help of a docker file, you can see the final image shown below image.

List of Commands used to write Dockerfile.

Now Let's start by understanding the technicalities of this file.

  • Comments - With the help of "#" we add comments in this file.

  • FROM - a base image that we want to use that is mentioned along with this command.

  • PULL - If we want to pull/fetch a file from any remote repository and add it to our docker image.

  • RUN - It'll execute building a container or can also be used to print a message by adding echo alongside.

  • CMD - If we want to fire a command inside our docker container then we use this command. For Example, if we want to run any script inside a container then this will be handy.

  • MAINTAINER - This will provide an author name who maintains this project.

  • ENV - With the help of this we can set an environment which the container should use like development, production, or UAT.

  • ENTRYPOINT - It's like a start-up application that runs whenever a container is initiated.

  • ADD - This command with be useful in copying data to the docker image.

  • COPY - This command copies the file from a source location to the destination location.

  • WORKDIR - To define a present working directory

  • EXPOSE - It mentions on which the application should be exposed.

  • ONBUILD - This will be executed when the current image is being used as a base for another image.

  • LABEL - It adds meta-data to an image in a key-value pair.

  • ARG - It will define a variable that we can use at our build time.

Hence, These are some of the commands that are used while writing a docker file.

By Vishwas Acharya πŸ˜‰


Checkout my other content as well:

YouTube:

Podcast:

Book Recommendations:

Did you find this article valuable?

Support Vishwas Acharya by becoming a sponsor. Any amount is appreciated!

Β