Skip to content
BENIBIX Learn

.dockerignore

Infrastructure📦 ContainersImage Builds

Also known as .dockerignore File, Docker Ignore File, dockerignore

A file that lists what to leave out when an image is built.

Think of it like

Think of it like a list of things not to pack for a trip.

Example

Emma's project folder holds 900 MB of downloaded packages and a .env file with passwords. Two lines in .dockerignore keep both out of the build.

Why it matters

Everything not left out is sent to the builder and can end up in the image. That includes passwords.

Definition

A .dockerignore file lists the files and folders to leave out when building an image. It sits in the top folder of the build. Before a build starts, that folder is sent to the builder, minus whatever this file names. Each line is a pattern, such as *.log for all log files. Leaving out big or secret files makes builds faster and keeps secrets out of the image.

Example code

console
$ cat .dockerignore
node_modules
.env
*.log
.git
Four lines keep out the packages, the secrets, the logs and the history.
The filter on the left is .dockerignore. Whatever it names never reaches the builder.

Common mistake

Putting it in the wrong folder. It has to sit in the top folder of the build, or nothing is left out.

Key takeaways

  1. 1
    A leave-out list. One pattern on each line.
  2. 2
    The top folder. It sits at the root of the build.
  3. 3
    Faster and safer. Less to send and no secrets inside.