CodePark/ questions / Layers & build cache
START PRACTICINGPRACTICE
Backend question · Docker › Layers & build cache

What is .dockerignore for, and what would you put in it for a Node project?

.dockerignore lists files and folders to leave out of the build context. The build context is the set of files the Docker client sends to the builder. COPY . . cannot copy anything that is excluded.

It matters for three reasons:

  • It keeps the context small, so builds start faster.
  • It stops unrelated changes, like .git activity, from invalidating the COPY cache.
  • It keeps secrets and junk files out of the image.

For a Node project, typical entries are:

  • node_modules. Modules built on the host, maybe for a different OS, must not overwrite the ones installed in the image.
  • dist
  • .git
  • .env and other env files
  • logs
  • coverage output
  • local Docker or editor files
  • It excludes files from the build context.
  • A smaller context makes builds faster.
  • It avoids needless cache invalidation.
  • It keeps secrets like .env out of the image.
  • Exclude node_modules, .git, dist and .env.