Ignore files

3 snippets across 2 stacks - Docker, Git

DKDocker

.dockerignore - Exclude Build Context

DK · Dockerfile
Syntax
# .dockerignore file
pattern
dir/
Example
node_modules
.git
*.md
.env
dist
.DS_Store
coverage

Note Place this file next to your Dockerfile. Without it, Docker sends everything in the build context to the daemon, including node_modules (hundreds of MB), .git history, and possibly secrets in .env files. This is one of the highest-impact optimizations you can make.

Always Use .dockerignore

DK · Best Practices
Syntax
# .dockerignore
Example
node_modules
.git
*.md
.env*
coverage
dist
.DS_Store
__pycache__
*.pyc
.venv

Note Without .dockerignore, a COPY . . sends everything to the daemon including .git (often 100MB+), node_modules, environment files with secrets, and test coverage reports. Builds become slow and images become bloated.

GitGit

Ignoring Files with .gitignore

Git · Setup & Config
Syntax
# .gitignore file patterns:
pattern
dir/
*.ext
!exception
Example
# .gitignore
node_modules/
*.log
.env
.DS_Store
build/

# Track this specific log despite the wildcard:
!important.log

Note If a file is already tracked, adding it to .gitignore will NOT stop tracking it. You must first run: git rm --cached <file> to untrack it.

Frequently asked questions

How do you ignore files?
This task is covered in 2 stacks on this page: Docker, Git. The ".dockerignore - Exclude Build Context" snippet in Docker uses `# .dockerignore file`.
Which command does the Docker example use?
The ".dockerignore - Exclude Build Context" snippet uses `# .dockerignore file`, from the Dockerfile section of the Docker cheat sheet.
Which stacks cover "ignore files" on this page?
Docker, Git. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For ".dockerignore - Exclude Build Context": Place this file next to your Dockerfile. Without it, Docker sends everything in the build context to the daemon, including node_modules (hundreds of MB), .git history, and possibly secrets in .env files. This is one of the highest-impact optimizations you can make.