What is .dockerignore and why we need it

 

Let’s say that your docker build command on a “heavy” project has a lot of delay time.  .dockerignore is what you missed and what you needed.

Docker build context

The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

You have a 10GB file in the current context. Unless you wanna COPY that file to your docker image, you should ignore it to speed up the build process.

What is .dockerignore file?

The .dockerignore file is very similar to the .gitignore file in that it allows you to specify a list of files or directories that Docker is to ignore during the build process.

.gitignore file ignores files to your project while .dockerignore ignoring files to the docker build’s context.

How to ignore files to the build’s context using .dockerignore

1. Ignore the files that you don’t need

file_a.ext
file_b.ext
/path/to/folder_a/
/path/to/folder_b/

!/path/to/folder_a/dont_ignore.ext

2. Ignore all files but some files that you need (recommended)

*
!dont_ignore_1.ext
!dont_ignore_2.ext
!/path/to/dont_ignore_folder_1/
!/path/to/dont_ignore_folder_2/

 

Refs:

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x