Building your Docker Images with Docker Files

Your First Docker File

Rishab Batra
6 min readMay 16, 2023


Docker is a software platform that helps in building, testing, and deploying applications quickly and takes care of the pain of deployments and version mismatch.

This tool can help change the developer’s pain of sharing code with others. Here is my small list of why to choose Docker —

  • With Docker, the Containerization of applications becomes easy. Containerization means packaging up all that is required to run an application, including all the runtime dependencies, configuration files, and settings, and then running the application in an isolated, preprepared environment.
  • The process with Docker is very fast. You can start your container in just a few seconds and then delete it whenever you no longer need them. In comparison to virtual machine containers are faster to build and remove.
  • Deployments become hassle-free. It becomes easier to deploy your project on the live server.
  • Setting up your working environment and facing trouble. If you have to set up the environment in a new Joinee PC or migrate to a different computer, you need to install all the dependencies manually again. But if your Docker is configured, you will never have to reinstall your dependencies manually again; give them your config file and see the magic. No more difficulties setting up your working environment.

What is Docker File?

A Docker file can help execute commands automatically written in a file to build a specific docker image. It takes them as a set of instructions to make that particular image.

And after that, you can quickly start the container.

Image Source

What is Task all about?

It will print “My First File” on the console when you start the container using the docker image containing MyDockerFile.

Let us see all the steps involved in creating your first docker file.

Dockerfile Example

Let’s see what’s inside written in a Dockerfile. I am demonstrating all the things required to build one docker image step by step. So, from a developer perspective, you’ll describe your environment’s build steps in the docker file. From there, you can quickly get up your container.

Step 1)

Create an empty directory for this task. You can easily do this by using the mkdir command. mkdir is the command that allows users to create directories in Linux. With the help of mkdir command, you can create a new directory at whatever location you are aiming for.

mkdir MyDockerDirectory

Step 2)

Navigate and create a new file inside the directory you created. Touch command is used for this purpose. The touch command creates a file without any content, which means the file would be empty initially. And it is great when you don’t want to store the data at the time of the creation of the file.

touch MyDockerFile

Step 3)

Now you have created your own docker file. Edit the newly created file in your own way, I am using the vi Editor command to edit the newly created docker file. Using vi editor, you can edit an already existing file or create a new file from the scratch.

Add the following instructions

FROM ubuntu:18.04
MAINTAINER: Type your Name
RUN apt-get update
cmd [“echo” , “My First File”]

Each instruction creates one layer:

  • FROM creates a layer from the ubuntu:18.04 Docker image. Every file must start with the form instructions. This instruction is a starting point to build a docker image.
  • MAINTAINER specifies the author of the image. This instruction allows the authors to set the details in an image.
  • RUN builds your application. Here in this example, the system is searching for repo updates.
  • CMD specifies what command to run within the container.

Step 5)

So you have added instructions in your docker file. It’s time to save and exit your file. Type: wq! Instruction in command mode, this instruction will save and exit the file. This command’s purpose is Save, edit, and force exit your file.


Note: You can quickly enter command mode by pressing the escape command. And also, you must have permission on the file, whether it is read-only or read-write before you can use: wq!

Step 6)

In this step, you will make sure that changes are getting appropriately saved. Cat command has various purposes, but here we are using it to display the file’s content and see docker file is saved as expected.

cat MyDockerFile

Step 7)

Build Image with Docker File

docker build location of your docker file

docker build -t my_docker_image .
  • docker build is the command used to build a Docker image from a Dockerfile
  • -t defines the tag (-t) of the image, which will basically be the image’s name.
  • Notice the. (dot) at the end of the line. You need to specify the directory where the docker build should look for a Dockerfile. Therefore. It tells Docker to build to look for the file in the current directory.

Congratulations. After running the above command, your image is ready, and you can now make your container ready by using the image you’re created.

Create a New Container

Creating and running a container based on the image you created.

docker run — it — name test_container my_first_image

Output on console

My First File

So the next question popping into your head is where your docker images are stored on the host.

Where are Docker Images get Stored on the Host?

So, after building your image, you would have thought, now my image is ready. Now, you thought to use a docker pull command, but you are wondering where the image is stored on the ubuntu server.

You can find your image in /var/lib/docker in the Linux environment.


What is this Overlay 2?

It is the default Docker storage driver on Ubuntu.

Confirm this with the following command -


Now check for Storage Driver.

What did you see?

If it is different, that means a different storage driver is used for Docker.

How to know specific Image Locations?

So you want to know the locations of a specific image. Docker provides us inspect command to get info about the image locations.

You created the my_first_docker_image above.

And now, you can inspect it with the docker command shown below.

docker inspect my_first_docker_image
"VirtualSize": 112542671,
"GraphDriver": {
"Data": {
"LowerDir": "/data/docker-data/overlay2/990a8366d3a830e780b26167229e94c4378a6248b2c5bf061da4ecb52d83c01a/diff",
"MergedDir": "/data/docker-data/overlay2/44b6692603018934d1faf8a3157e6be3e374acfe36121514b0923533df796237/merged",
"UpperDir": "/data/docker-data/overlay2/44b6692603018934d1faf8a3157e6be3e374acfe36121514b0923533df796237/diff",
"WorkDir": "/data/docker-data/overlay2/44b6692603018934d1faf8a3157e6be3e374acfe36121514b0923533df796237/work"
"Name": "overlay2"

Inside the Data subsection under GraphDriver here, you will see some fields. They are telling us the physical locations of your image on the host system.

It contains four fields LowerDir, UpperDir, WorkDir, and MergedDir.

Image Source

1] LowerDir: This is known as a base layer where actual original files reside. They are the read-only layers of an overlay filesystem.

2] UpperDir: It represents the read-write layer of an overlay filesystem. UpperDir will contain all the modifications done by you on the file.

  • The new version will be written on this layer if anything changes within the file.
  • If the file is deleted, it is marked on this layer.
  • A New file created is stored on this layer.

Note this is after the building image. Before building an image, original files reside in LowerDir.

For Docker, the container-specific layer contains changes made by that container.

3] WorkDir: This is a required directory for overlay2; it needs an empty directory for internal use.

4] MergedDir: It is a result of the overlay filesystem(LowerDir and UpperDir), which Docker uses to run the container. It is the final view of all merged layers.


In this article, we discussed the creation of docker files from scratch; Docker files can help you automate the process of executing instructions and is a faster and more effective way of building Docker images. Also, now you know how to know about the locations of a Docker Image.

Key Takeaways:

  • Docker is a great tool to ship, test and deploy code quickly and easily. You can significantly reduce the delay between writing and running code in production. Docker has numerous benefits and can help you construct containerized applications easily.
  • The Dockerfile contains a set of instructions that Docker will execute on issuing the docker build command.
  • Docker Files are used to create customized images with your custom configurations to be used in a docker container.
  • There will always be a time when the already built images are not satisfying your project needs, and you need to create your own docker images with the docker files.
  • If default storage driver overlay2 is used, then your Docker images are stored in /var/lib/docker/overlay2, and you can inspect them and learn more about the information.

Let me know in the comments section if anyone needs help. You can also reach me on medium or linkedin

Thank you!