Skip to content

.dockerignore

A .dockerignore file tells Docker which files and folders must not be included in the build context.

It is essentially identical in spirit to .gitignore, but for the Docker build daemon instead of Git.

Without a .dockerignore, the build context will zip up and send everything in our project directory over to the Docker Engine. That often includes things we really do not want inside an image, such as:

  • node_modules
  • .git histories
  • local error logs
  • editor config files
  • temporary build files
  • coverage output
  • local .env files with secret keys

That kind of sprawl makes our builds:

  • slower
  • larger
  • noisier
  • much less secure
  • far less predictable
Host Contamination

This step is especially critical for Node development.

We do not want to copy your host machine’s node_modules into the final image. Doing so completely defeats the purpose of building in a clean, isolated environment, and can cause strange architecture mismatch errors.

Sound familiar? The above is why I insist that you remove node_modules from your assignments before submitting them.

Leaky Secrets

We definitely do not want to copy our host machine’s .env file into the final image.

To do so would be to invite disaster.

We will discuss how to handle environment variables in a more secure manner later.

.dockerignore
node_modules
.git
.env
npm-debug.log
.DS_Store
coverage

Docker Docs: .dockerignore files