> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using dockerignore Properly

> How to use .dockerignore to exclude large, secret, and noisy files from Docker build contexts to reduce image size, build time, and prevent leaking sensitive data

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

| Bucket | Why ignore | Common examples |
| - | - | - |
| Heavy | Large directories that are rebuilt inside the image and waste bandwidth | `node_modules`, `dist`, `build` |
| Secret | Any credentials or keys should never be sent to the daemon | `.env`, `*.pem`, `id_rsa`, cloud credentials |
| Noisy | Files that change frequently but don't affect the build | `.git`, `*.log`, temporary databases |

## 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:

```dockerfile theme={null}
FROM node:20
COPY . .
RUN npm install
# Deleting after copying does not remove it from previous layers
RUN rm .env
CMD ["node", "app.js"]
```

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.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Docker/Using-dockerignore-Properly/dockerignore-noisy-files-git-commits-image.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=0156437bec6ede650dff56fef18840c1)

Example `.dockerignore` snippets for noisy files:

```gitignore theme={null}
.git
*.log
*.sql
```

## 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.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Docker/Using-dockerignore-Properly/copy-paste-trap-node-modules-dockerignore.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=c589a9facef0bc5b36a0a2d1e7eac5e8)

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/**`

## A recommended `.dockerignore`

```gitignore theme={null}
# Heavy
node_modules
**/node_modules
dist
build

# Secrets
.env
*.env
*.pem
*.key
id_rsa
credentials.json

# Noisy
.git
*.log
*.sql
tmp
.vscode
```

## 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.

## Quick reference links

* [Docker build context and .dockerignore behavior](https://docs.docker.com/engine/reference/builder/#dockerignore-file)
* [Docker secrets (build-time and Swarm)](https://docs.docker.com/engine/swarm/secrets/)
* [Kubernetes Secrets](https://kubernetes.io/docs/concepts/configuration/secret/)

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.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/1d3d5877-dbf7-4105-8bc2-2c619ac62421/lesson/d54e85fc-8473-4386-8479-a77111f7fd2b)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.