Docker .dockerignore basics

Topic: Containers core

Summary

Add a .dockerignore file next to Dockerfile to exclude files from build context. Speeds build and avoids leaking secrets. Use when build context is large or you want to exclude git or local files.

Intent: How-to

Quick answer

  • Create .dockerignore with lines like .git, node_modules, *.log, .env. Same syntax as .gitignore.
  • Excluded files are not sent to daemon. Speeds build and keeps secrets out of context.
  • COPY . . only copies files not ignored. Test with docker build and check image contents.

Prerequisites

Steps

  1. Create file

    Create .dockerignore in same dir as Dockerfile. Add .git, node_modules, .env, tmp, *.log.

  2. Verify

    docker build. Build context should be smaller. Check that sensitive files are not in image.

  3. Refine

    Add more patterns if needed. Do not ignore files required by COPY or RUN.

Summary

Use .dockerignore to exclude unneeded and sensitive files from build context. Speeds build and improves security.

Prerequisites

Steps

Step 1: Create file

Add .dockerignore with .git, node_modules, .env, and other exclusions.

Step 2: Verify

Build; confirm smaller context and no secrets in image.

Step 3: Refine

Add patterns; do not ignore files needed by COPY or RUN.

Verification

  • Context smaller; no .env or .git in image.

Troubleshooting

COPY failed — Do not ignore file that COPY needs. Still large — Add more patterns.

Next steps

Continue to