DEV Community

Cover image for What is .dockerignore and why you need it
Parvez M Robin
Parvez M Robin

Posted on

What is .dockerignore and why you need it

Yesterday, I was trying to replicate a tool from a conference paper. Unfortunately, it is using python 2. I wish one day researchers will know python 2 is dead. Anyway, I dockerized it, built it, ran it. It went fine. Today, I came back to my lab. Tried to build it again. LACKADAY! The build took more than 10 minutes. It kept showing the following message.

Preparing build context archive...
[==================================================>]79/79 files
Enter fullscreen mode Exit fullscreen mode

So, I did what a computer scientist should do — Google. And I found a gem — .dockerignore. Before jumping into detail. Let me share what was the actual problem in my case. Before building a docker image, the docker CLI compresses all the files in the current directory and sends them to the docker daemon. These files are called the build context. In your Dockerfile you can only refer to the files/directories from the build context. The scripts I ran yesterday downloaded some files having a size of nearly 10 GB. Hence, when I was building the docker image today, it was taking forever.

There comes .dockerignore for the rescue. All the heavy files were inside two directories — data, and embeddings. So, I created a file in the project named .dockerignore and put the following content there.

data/
embeddings/
Enter fullscreen mode Exit fullscreen mode

Bingo! The build time was reduced from 10 minutes to 10 seconds.


However, how could this be useful for your particular case? If you are a modern-day developer, surely you are familiar with dependencies. Could be npm, yarn, composer, nuget, gradle, gem, pip, conda, cargo, dep, or anything else. But there is definitely one. Now, for example, you are using npm, in most cases, your Dockerfile contains the following line —

RUN npm install
Enter fullscreen mode Exit fullscreen mode

So, you are installing your dependencies inside your image. Now, if you have a node_modules directory outside your image (i.e., in your actual machine), that is not actually needed. However, by default, it will be added to the build context with the weight of a black hole and make the build process slow. However, if you put the node_modules directory in the .dockerignore, it will have no impact on the build process. The same goes for any other dependency management tools.

I Hope, from now on, your docker builds will be a little bit faster, if not gigantic.

Top comments (0)