Skip to main content
Scans of public Docker Hub images have revealed sensitive data (for example, private keys and credentials) in many images. Properly using a .dockerignore file prevents sensitive and unnecessary files from being sent to the Docker daemon during docker build, reducing image size, build time, and leakage risk. When you run docker build, Docker first collects everything in the build context (the current directory by default). Before the build starts, Docker reads .dockerignore and excludes any matching files and directories from the context. Files excluded by .dockerignore are not sent to the daemon and therefore cannot be included in any image layers. What to exclude from the build context falls into three broad buckets:
  • Heavy stuff
  • Secret stuff
  • Noisy stuff
Table: Why and what to ignore

Heavy stuff

Large host directories that are recreated during the image build should be excluded. For example, node_modules can be hundreds of MB and often contain native binaries built for the host OS. Copying them into the build context wastes time, bandwidth, and can break cross-platform builds. Ignore these directories and let the Dockerfile run package installs inside the container.

Secret stuff

Never send files containing credentials, keys, or tokens to the build context. Because each Dockerfile instruction creates a layer, copying a secret into the image—even if you delete it later in a subsequent instruction—leaves it recoverable in an earlier layer. Example illustrating why secrets must be excluded from the build context:
Never rely on deleting secrets in later build steps—do not send them to the daemon at all. Use secret managers or runtime injection instead (see Best practices and alternatives below).
Avoid copying secrets into images. If your build needs credentials (for private package registries, etc.), inject them at build or runtime using secure build-time secrets or your orchestrator’s secret management.

Noisy stuff

Files and folders that change constantly but don’t affect the build should be ignored. The common examples are .git (your commit history) and log files. Sending .git on every build can force unnecessary rebuilds because Docker sees the context as changed.
A stylized slide titled "NOISY STUFF" showing a .dockerignore box listing .git and *.log with a git commit timeline and the note "it changes on every single commit." To the right is an empty outlined "Docker image" box indicating those noisy files are excluded from the image.
Example .dockerignore snippets for noisy files:

Common mistake: copying .gitignore into .dockerignore

A frequent trap is copying .gitignore contents into .dockerignore and assuming the semantics are identical. They are not. .gitignore patterns are intended for Git and operate differently from .dockerignore patterns, which are matched against paths in the build context.
A dark-themed diagram titled "The Copy-Paste Trap" showing a project folder with top-level and nested node_modules directories. To the right are boxes for .gitignore and .dockerignore listing node_modules and an empty Docker image box, illustrating how node_modules can be accidentally copied into images.
Why the copy-paste trap happens
  • .gitignore is evaluated in the repository with semantics that typically match files anywhere. For Docker you must be explicit about path matching. A node_modules line in .gitignore ignores that folder wherever it appears for Git. In .dockerignore, to ensure you exclude node_modules anywhere in the context, use explicit glob patterns like **/node_modules or node_modules/**.
Practical .dockerignore patterns
  • Ignore top-level node_modules:
    • node_modules or node_modules/
  • Ignore node_modules anywhere in the context:
    • **/node_modules
    • or node_modules/**

Best practices and alternatives

  • Never send secrets to the daemon. Instead:
    • Use Docker build-time secrets or buildkit secrets to provide credentials during build without baking them into image layers.
    • Use your orchestrator’s secret manager at runtime (for example, Docker Swarm secrets or Kubernetes Secrets).
    • Inject environment variables at deploy time or mount secret files as volumes.
  • Keep .dockerignore in version control and review changes as part of PRs.
  • In monorepos or nested projects, explicitly use glob patterns so you exclude the intended paths.
  • Test builds in CI where the context is controlled to avoid accidental local files leaking into images.
Never include secrets (like .env, private keys, or cloud credentials) in the build context. Deleting them in a later Dockerfile instruction does not remove them from previous image layers.
Keeping your .dockerignore accurate and up to date makes builds faster, images smaller, and your projects more secure.

Watch Video