Skip to content
Related Articles

Related Articles

How to Use a .dockerignore File?
  • Last Updated : 29 Oct, 2020

If you are a Docker Developer, you might have noticed that when you build a Docker Image either using a dockerfile or directly pull an image from the Docker registry, the size of the image can be considerably large depending upon your Docker Build Context.

Since Docker is a client-server application, we know that the Docker Client is the actual Command Line Interface that we used to access the Docker Containers and the Docker Server is actually called the Docker Daemon which helps you in maintaining the Containers. When we are trying to build a Docker Image, we need to send some files to the Docker Daemon or the server so that those files can be used and included inside the Docker Container that we are trying to build. This set of files and directories is called the Docker Build Context. 

It’s obvious that the larger the size of the files and folders inside the Docker build context, the larger is the size of the Docker Image. Now, inside the folder where you keep your dockerfile and through which you are trying to build the Image, there might contain some files and folders that are although a part of the project and you don’t want to include those files in the Docker Build Context. Let’s look at some reasons for not including these files and directories.

  • Security Issues – Some important files such as passwords, secret keys, .git folders, etc contain a lot of information about your project and you might now want to expose those details to the outside world to prevent intrusion.
  • Cache Invalidation – When you write the Dockerfile, it’s a general practice to use the COPY instruction to copy the files and folders inside the Docker build context. Each statement inside the Dockerfile results in building a new intermediate image layer. Hence, when you make changes in your dockerfile again and again, this might lead to multiple Cache Invalidation and leads to wastage of resources.
  • Also, excluding unnecessary large files from your Docker Build Context will lead to lower Docker Image size.
  • It speeds up the process of building the Docker Image.

Due to all these reasons, you might want to exclude some files and folders from your Docker Build Context.

Now, similar to a .gitignore file that is commonly used when you build Git repositories, a .dockerignore file is used to ignore files and folders when you try to build a Docker Image. You can specify the list of files and directories inside the .dockerignore file. 

Let’s look at an example of .dockerignore file.


Let’s take a look at a practical example of using a .dockerignore file.

Step 1: Create a directory containing a dockerfile where you specify the instructions and a folder that you want to ignore (say ignore-this).

In this case, the dockerfile simply pulls the Ubuntu Image from the repository and copy the build context.

FROM ubuntu:latest
COPY . .

Step 2: Inside the same directory, create a .dockerignore file and include the name of the folder you want to exclude from the Docker Build Context.


Now, the main directory contains a dockerfile, a .dockerignore file, and a folder called ignore-this.

Directory structure

Directory structure

Step 3: Build the Docker Image.

sudo docker build -t sample-image .
Building the Docker Image

Building the Docker Image

Step 4: Run the Docker Container and check the folder.

sudo docker run -it sample-image bash
Running the Container

Running the Container

You will find that the Container only contains the dockerfile and not the “ignore-this” folder. Not that it also does not contain the .dockerignore file.

What if you ignore the dockerfile?

It is true that you can also mention the dockerfile inside the .dockerignore file and exclude it from the Docker build context. In fact, it is a common practice than you might have thought. This allows you not to expose the entire blueprint of your Docker application.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :