Open In App

How to Use a .dockerignore File?

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.

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.

passphrase.txt
logs/
.git
*.md
.cache

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.

ignore-this

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

Directory structure

Step 3: Build the Docker Image.

sudo docker build -t sample-image .

Building the Docker Image

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

sudo docker run -it sample-image bash
ls

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.

Article Tags :