Project 1 โ€บ 05 Create a .dockerignore fileGitHub โ†—
๐Ÿค– AI Prompt โ€” stuck? paste this

โ€œCreate a .dockerignore for a Node.js project that excludes node_modules, .env, .git, and log files. Explain how it differs from .gitignore.โ€

05 โ€” Create a .dockerignore file

Category: Dockerfile ยท Priority: Medium ยท Step: 6

Exclude: node_modules/, .env, .git, __pycache__, *.log. Keeps the build context lean.

๐ŸŽฏ What is this task?

When Docker builds your image, it copies everything in your project folder into the build context. This includes things you don't need inside the container โ€” like node_modules, .env files, the .git folder, and log files. A .dockerignore file tells Docker to skip those. It works exactly like a .gitignore file.

Without this, Docker might copy hundreds of MB of files unnecessarily, and could even leak secret .env values into your image.

๐Ÿค– AI Prompt

"Create a .dockerignore file for a Node.js project. Exclude: node_modules, .env files, .git folder, log files, editor config files like .vscode, test files, and any OS junk like .DS_Store. Add a comment above each group explaining why it's excluded."

๐Ÿ“‹ Step-by-step

Step 1 โ€” Create .dockerignore in your project root

This file goes in the same folder as your Dockerfile. Create a file named exactly .dockerignore:

# Dependencies โ€” Docker will install these fresh via npm install
node_modules
npm-debug.log

# Environment secrets โ€” NEVER bake these into an image
.env
.env.*
*.env

# Git history โ€” not needed inside container
.git
.gitignore

# Editor files
.vscode
.idea
*.swp

# OS junk
.DS_Store
Thumbs.db

# Logs
logs/
*.log

# Test files
__tests__/
*.test.js
*.spec.js

Step 2 โ€” Verify it works by checking build output

docker build -t my-docker-app:v4 .

Look at the first line of output โ€” it shows [+] Building X.Xs (Y steps). The "Sending build context" step should be small (a few KB, not MB).

Step 3 โ€” Confirm node_modules wasn't copied

docker run --rm my-docker-app:v4 ls /app

You'll see index.js, package.json, and node_modules โ€” but the node_modules inside was installed by npm install in the Dockerfile, not copied from your machine.

โœ… Done when

  • .dockerignore file exists in your project root
  • Build context size in docker output is small
  • .env files and node_modules from your machine are not baked into the image

๐Ÿ“ Quiz

Answer all 2 questions correctly to complete this lesson.

1. What does .dockerignore control?
2. Which of these is a good reason to ignore .env?
โ† PreviousPass the quiz to completeNext โ†’