Video in progress

What is the .dockerignore file for?

It excludes unneeded files from what is sent to the Docker daemon during docker build. The build gets faster, the image gets smaller, and things like .git or .env do not end up inside the image by accident.

📖 Answer

The docker build . command sends the whole folder given by the dot (the build context) to the daemon. If it contains the .git history, IDE files and old target/ folders, hundreds of megabytes get copied for nothing on every build.

# .dockerignore
.git
.idea
*.iml
target/
!target/*.jar
.env
*.log

There is a security side too. If the Dockerfile has COPY . ., the .env file goes into the image together with its passwords, and anyone who pulls the image from the registry can read it.

Another benefit: when an unneeded file changes (a README, say), the COPY . . layer does not get invalidated in the cache, so the build is faster.