📖 Scenario: You are working on a Docker project that contains many files, including some large files and folders that are not needed inside the Docker image. To make your Docker builds faster and smaller, you want to exclude these unnecessary files using a .dockerignore file.
🎯 Goal: Create a .dockerignore file that excludes specific files and folders from the Docker build context to optimize the build process.
📋 What You'll Learn
Create a .dockerignore file
Add rules to exclude the node_modules folder
Add rules to exclude all .log files
Add rules to exclude the temp folder
Verify the .dockerignore file content
💡 Why This Matters
🌍 Real World
In real projects, many files like dependencies, logs, or temp files are not needed inside Docker images. Using <code>.dockerignore</code> helps keep images clean and build times short.
💼 Career
Knowing how to optimize Docker builds with <code>.dockerignore</code> is a key skill for DevOps engineers and developers working with containerized applications.
Progress0 / 4 steps
1
Create a .dockerignore file
Create an empty file named .dockerignore in your project root directory.
Docker
Hint
Use your text editor or command line to create a file named .dockerignore in the project folder.
2
Add rules to exclude node_modules and .log files
Add these two lines to the .dockerignore file exactly: node_modules *.log
Docker
Hint
Each line in .dockerignore tells Docker what to ignore. Use node_modules to ignore the folder and *.log to ignore all log files.
3
Add rule to exclude the temp folder
Add a line to the .dockerignore file to exclude the temp folder exactly: temp
Docker
Hint
Just add the folder name temp on a new line to exclude it.
4
Verify the .dockerignore file content
Print the content of the .dockerignore file using the command cat .dockerignore to verify it contains exactly these lines: node_modules *.log temp
Docker
Hint
Use the cat .dockerignore command in your terminal to see the file content.
Practice
(1/5)
1. What is the main purpose of a .dockerignore file in a Docker project?
easy
A. To specify environment variables for the Docker container
B. To tell Docker which files and folders to exclude when building an image
C. To configure network settings for Docker containers
D. To list all files that must be included in the Docker image
Solution
Step 1: Understand the role of .dockerignore
The .dockerignore file lists files and folders Docker should skip during image build.
Step 2: Compare options with this role
Only To tell Docker which files and folders to exclude when building an image correctly states that .dockerignore excludes files/folders from the build context.
Final Answer:
To tell Docker which files and folders to exclude when building an image -> Option B
Quick Check:
.dockerignore excludes files from build context [OK]
Hint: Think: .dockerignore tells Docker what NOT to include [OK]
Common Mistakes:
Confusing .dockerignore with Dockerfile
Thinking it lists files to include instead of exclude
Assuming it configures container runtime settings
2. Which of the following is the correct syntax to ignore the node_modules folder in a .dockerignore file?
easy
A. node_modules
B. node_modules/*
C. /node_modules/*
D. !node_modules
Solution
Step 1: Review .dockerignore syntax for ignoring folders
Simply writing the folder name like node_modules ignores it and all its contents.
Step 2: Analyze options
A (node_modules) is correct because it ignores the folder and its contents. B (node_modules/*) and C (/node_modules/*) ignore only contents, leaving an empty folder in the build context. D (!node_modules) negates ignoring, so it includes the folder.
Final Answer:
node_modules -> Option A
Quick Check:
Folder name alone ignores folder and contents [OK]
Hint: Write folder name alone to ignore it fully [OK]
Common Mistakes:
Using ! to ignore instead of include
Adding slashes inconsistently
Confusing .dockerignore with .gitignore syntax
3. Given this .dockerignore content:
node_modules
secret.txt
!secret.txt.bak
Which files will be included in the Docker build context?
medium
A. secret.txt and secret.txt.bak only
B. node_modules folder, secret.txt, secret.txt.bak
C. node_modules folder and secret.txt.bak only
D. secret.txt.bak only
Solution
Step 1: Interpret the .dockerignore rules
Lines exclude node_modules and secret.txt. The !secret.txt.bak line re-includes secret.txt.bak.
Step 2: Determine included files
Excluded: node_modules and secret.txt. Included: secret.txt.bak because of the ! rule.
Final Answer:
secret.txt.bak only -> Option D
Quick Check:
! negates ignore = include secret.txt.bak [OK]
Hint: ! before filename re-includes it despite ignore [OK]
Common Mistakes:
Assuming ! negates all ignores
Including ignored folders by mistake
Ignoring the order of rules
4. You wrote this .dockerignore file:
node_modules
!node_modules/package.json
But package.json is still excluded from the build. What is the likely problem?
medium
A. Docker does not support negation (!) in .dockerignore
B. You must restart Docker daemon to apply .dockerignore changes
C. Negation does not work on files inside ignored folders
D. The syntax for negation is incorrect; use double exclamation marks (!!)
Solution
Step 1: Understand negation behavior in .dockerignore
Negation (!) cannot re-include files inside a folder that is ignored entirely.
Step 2: Analyze the problem
Since node_modules is ignored, negating node_modules/package.json does not include it back.
Final Answer:
Negation does not work on files inside ignored folders -> Option C
5. You want to speed up your Docker build by excluding all .log files except important.log in the root folder. Which .dockerignore content achieves this?
hard
A. *.log
!important.log
B. *.log
!important.log
!important.log
C. *.log
!important.log
!important/*.log
D. *.log
!important.log
!important/
Solution
Step 1: Write rule to ignore all .log files
Using *.log ignores all files ending with .log anywhere.
Step 2: Use negation to include only important.log in root
!important.log re-includes that specific file in the root folder.
Final Answer:
*.log\n!important.log -> Option A
Quick Check:
Ignore all .log, then include important.log [OK]
Hint: Ignore *.log then negate important.log to include it [OK]
Common Mistakes:
Repeating negation lines unnecessarily
Negating folders instead of files
Using wrong paths for negation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.