DevOps/Docker

.dockerignore

neal89 2025. 6. 7. 16:01

Using .dockerignore and Path Notation

When building a Docker image, the COPY instruction copies files or folders from the host machine's build context into the image. At times, it becomes necessary to exclude specific files or folders from this process. Additionally, understanding how to specify paths is crucial.

1. How to Use .dockerignore

A .dockerignore file functions similarly to a .gitignore file. It instructs Docker to exclude specific files or directories from the Docker build context, which helps reduce the build context size and prevents unnecessary files from being included in the image.

  • Location: The .dockerignore file must be located in the same directory as the Dockerfile.
  • How it Works: Before sending the build context to the Docker daemon, the Docker client reads the rules in the .dockerignore file and excludes any matching files or folders from the transmission list.

Common Rules for .dockerignore files:

  • Blank lines or lines starting with #: Are ignored.
  • * (wildcard): Matches zero or more characters.
    • *.log: Excludes all .log files.
    • temp*: Excludes all files/folders starting with temp.
  • ** (double wildcard): Matches zero or more directories. (Includes files/folders in any subdirectory.)
    • **/temp: Excludes all temp folders and temp files in any subdirectory.
    • src/**/*.log: Excludes all .log files in any subdirectory under the src folder.
  • ?: Matches exactly one character.
    • my_file_?.txt: Matches my_file_A.txt, my_file_B.txt, etc.
  • ! (negation): Includes items that would otherwise be excluded by a previous pattern. Useful for re-including specific files.
    • *: Exclude all files.
    • !README.md: Re-include only README.md.
  • Folder Name: Excludes the specified folder and all its contents.
    • node_modules/: Excludes the entire node_modules folder.
    • build/: Excludes the entire build folder.
  • file.txt: Excludes a specific file.
    • config.bak: Excludes the config.bak file.

Example .dockerignore file (for a backend project):

# Exclude build artifacts and temporary files
target/        # Maven project build output folder
build/         # Gradle project build output folder
.gradle/       # Gradle related cache and configuration folder
*.jar          # JAR files (usually explicitly copied via COPY, so excluded from build context)
*.war          # WAR files

# Exclude IDE-related files and settings
.idea/         # IntelliJ IDEA settings folder
.vscode/       # VS Code settings folder
*.iml          # IntelliJ IDEA module file

# Exclude version control related files
.git/          # Git repository information
.gitignore     # The .gitignore file itself (not needed, so exclude)

# Node.js project related (if frontend is co-located)
node_modules/  # Node.js dependency folder

# Exclude logs and environment configuration files
*.log          # All log files
.env           # Environment variable files (should not be included in the image for security reasons)

Reasons to use .dockerignore:

  • Improve Build Speed: Reduces the size of the build context sent to the Docker daemon, speeding up the build process.
  • Reduce Image Size: Prevents unnecessary files from being included in the image, reducing the image size. This helps with deployment and saves storage space.
  • Enhance Security: Prevents sensitive information (e.g., .env files, API keys) from accidentally being included in the image.