When building docker images, it is easy to include files that aren’t necessary for your application to run successfully. Which increases our image size and can potentially cause deployment issues. In this post, we will discuss the value of the .dockerignore file and how it can be used to cleanup your docker images.

What is a .dockerignore file?

A .dockerignore file is a docker specific file that you create in your build context directory. Typically, this is the same directory as your dockerfile. The .dockerignore file is a text file, similar to a .gitignore file, where you can specify glob patterns to indicate which files or directories you want to ignore (exclude) when your image is built. You can also specify, exceptions to these rules within the same file.

Why is this valuable?

Build time. The smaller your image, the faster it builds. Which proves critical in continuous delivery environments where images are constantly being built.

Security. When using a docker copy command to copy the files in your build context into your image, you may unintentionally copy files that contain passwords. An example of this may include application config files or your .aws directory that contains your login credentials.

How to create a .dockerignore file?

To create a “.dockerignore” file, navigate to your build context and create a new file. Within this file is where you will specify build rules, that will be used every time your image is built. Visit Docker’s official page for syntax help. However to start we recommend adding “.git” and “*.md” to your file, as these are common files that can be excluded and provide an easy way for you to see how the file works. The .git will remove your .git directory from your image when built. If you’re unfamiliar with the .git directory, it contains all of the metadata associated with your branch. The “*.md” will remove any markdown files in your build context directory.


Dockerignore files provide a simple solution to minimizing image size and maintaining optimal build times. Mastering the syntax and understanding the contents of your image, will enable you use this file to its fullest potential.

Have questions? Contact us, we would love to hear from you.

About the author