Sub topic 3
The Build Context and .dockerignore
This chapter assumes you can write a basic Dockerfile and run docker run. It explains what the build context is, how it affects your build, and how .dockerignore keeps unneeded files out of it.
What the build context is
The build context is the set of files that docker build sends to the builder. You choose it with the last argument of the command. In docker build -t myapp:1.0 ., the final . is the context: the current directory and everything under it.
Two rules follow from this:
COPYandADDcan only read files inside the context. A path such asCOPY ../secrets.txt .fails, because it points outside the context.- The Dockerfile is not copied into the image unless you copy it. It is read from the context, or from a path you give with
-f.
Why the size of the context matters
Every build starts by transferring the context. A large context slows each build, even when the cache would otherwise skip every step. It also changes the cache: COPY . . creates a layer whose cache key depends on the content of the files it copies. If a log file in the context changes, that COPY step is rebuilt, and so is every step after it.
A broad COPY . . can also place files in the image that should never be there, such as .git, local caches, and .env files with credentials.
Excluding files with .dockerignore
Place a file named .dockerignore in the root of the context. Each line is a pattern for paths to leave out. Patterns are matched relative to the context root.
.git
__pycache__/
*.log
.envThis keeps version-control data, Python bytecode caches, log files, and local secrets out of the context. They are not sent to the builder, so COPY . . cannot add them to the image.
Some pattern details are worth knowing:
*.logmatches log files in the root of the context. To match them in any subdirectory, write**/*.log.- A line that starts with
!makes an exception to an earlier pattern. For example,*.mdfollowed by!README.mdexcludes all Markdown files exceptREADME.md. - Lines that start with
#are comments.
Common errors
- "file not found" during COPY. The file is outside the context, or
.dockerignoreexcludes it. Check both. - A slow build that says it is sending a large context. Look for directories such as
node_modules,.git, or build output, and add them to.dockerignore. - A cache miss on
COPY . .after an unrelated change. A file that changes often, such as a log, is inside the context. Exclude it. - Using
/as the context. This sends your whole filesystem. Always pass the project directory.
Exercise
Your project has a .env file and a logs/ directory. A build is slow, and COPY . . reruns every time you edit only README.md... wait, that edit changes a copied file. Which two lines would you add to .dockerignore to stop .env and logs/ from affecting the build, and why does that help the cache?
Show answer
Add .env and logs/. Excluded files are not sent in the context, so they are not copied into the image and do not change the cache key of the COPY . . layer. The layer is then rebuilt only when files you actually copy change. An edit to README.md still invalidates that layer, because README.md is part of the copied content.