Creating a .dockerignore file
To create a .dockerignore file:
- Create a new file named
.dockerignorein the root directory of your project (the same directory that contains your Dockerfile) - Add patterns for files and directories you want to exclude, one per line
Here’s a simple example of a .dockerignore file:
# Exclude node modules
node_modules
# Exclude any git files
.git
.gitignore
# Exclude log files
*.log
# Exclude the README
README.md
.dockerignore syntax
The .dockerignore file uses pattern matching similar to .gitignore. Here’s what you need to know:
Basic patterns
# Comment line (ignored)
file.txt # Ignores file.txt in any directory
path/to/file.txt # Ignores specific file
*.log # Ignores all files with .log extension
**/*.log # Ignores .log files in any subdirectory
temp? # ? matches a single character (temp1, tempA, etc.)
Directory patterns
node_modules/ # Ignores the node_modules directory and all its contents
build/ # Ignores the build directory and all its contents
Negation patterns
You can use ! to negate a pattern, meaning Docker will include a file that would otherwise be excluded:
# Ignore all .md files
*.md
# But include important-notes.md
!important-notes.md
Path patterns
# Matches directories and files named temp at any level
**/temp
# Matches files or directories in the root directory only
/temp
# Matches any file or directory named temp or starting with temp
temp*
Practical examples
Let’s look at some real-world examples for different types of projects:
Node.js project
# Development dependencies
node_modules
npm-debug.log
yarn-debug.log
yarn-error.log
# Environment and configuration
.env
.env.local
.env.development
.env.test
.env.production
# Testing
coverage
.nyc_output
# Build outputs
dist
build
# Version control
.git
.gitignore
# IDE specific files
.idea
.vscode
*.swp
*.swo
# OS specific files
.DS_Store
Thumbs.db
Python project
# Python bytecode
__pycache__/
*.py[cod]
*$py.class
# Virtual environments
venv/
env/
ENV/
.env
# Testing
.pytest_cache/
.coverage
htmlcov/
# Distribution / packaging
dist/
build/
*.egg-info/
# Documentation
docs/_build/
# Version control
.git
.gitignore
# IDE specific files
.idea/
.vscode/
Java project
# Compiled class files
*.class
target/
build/
# Package files
*.jar
*.war
*.ear
# Maven
.mvn/
mvnw
mvnw.cmd
# Gradle
.gradle/
gradlew
gradlew.bat
# Testing
test-output/
# Logs
*.log
# Version control
.git/
.gitignore
# IDE specific files
.idea/
.vscode/
*.iml
.classpath
.project
.settings/
Example: Complete workflow
Let’s walk through a complete example of setting up a .dockerignore file for a Node.js web application:
- Project structure:
my-node-app/
├── .git/
├── node_modules/
├── src/
│ ├── index.js
│ └── components/
├── public/
│ ├── images/
│ └── styles/
├── tests/
├── .env
├── .env.example
├── package.json
├── package-lock.json
└── Dockerfile
- Create a
.dockerignorefile:
# Version control
.git
.gitignore
# Dependencies
node_modules
# Environment variables (except examples)
.env
.env.*
!.env.example
# Testing
tests
coverage
# Build artifacts
build
dist
# Logs
logs
*.log
# OS files
.DS_Store
Thumbs.db
# Editor directories
.idea
.vscode
*.swp
- Dockerfile:
FROM node:14-alpine
WORKDIR /app
# Copy package files first (for better caching)
COPY package*.json ./
RUN npm install --production
# Copy the rest of the application
COPY . .
EXPOSE 3000
CMD ["node", "src/index.js"]
- Build the Docker image:
docker build -t my-node-app .