No markdown files are included in the context except README files other than
`README-secret.md`.
Now consider this example:
```text
*.md
README-secret.md
!README*.md
```
All of the README files are included. The middle line has no effect because
`!README*.md` matches `README-secret.md` and comes last.
## Named contexts
In addition to the default build context (the positional argument to the
`docker build` command), you can also pass additional named contexts to builds.
Named contexts are specified using the `--build-context` flag, followed by a
name-value pair. This lets you include files and directories from multiple
sources during the build, while keeping them logically separated.
```console
$ docker build --build-context docs=./docs .
```
In this example:
- The named `docs` context points to the `./docs` directory.
- The default context (`.`) points to the current working directory.
### Using named contexts in a Dockerfile
Dockerfile instructions can reference named contexts as if they are stages in a
multi-stage build.
For example, the following Dockerfile:
1. Uses a `COPY` instruction to copy files from the default context into the
current build stage.
2. Bind mounts the files in a named context to process the files as part of the
build.
```dockerfile
# syntax=docker/dockerfile:1
FROM buildbase
WORKDIR /app
# Copy all files from the default context into /app/src in the build container
COPY . /app/src
RUN make bin
# Mount the files from the named "docs" context to build the documentation
RUN --mount=from=docs,target=/app/docs \
make manpages
```
### Use cases for named contexts
Using named contexts allows for greater flexibility and efficiency when
building Docker images. Here are some scenarios where using named contexts can
be useful:
#### Example: combine local and remote sources
You can define separate named contexts for different types of sources. For
example, consider a project where the application source code is local, but the
deployment scripts are stored in a Git repository:
```console
$ docker build --build-context scripts=https://github.com/user/deployment-scripts.git .
```
In the Dockerfile, you can use these contexts independently:
```dockerfile
# syntax=docker/dockerfile:1
FROM alpine:latest
# Copy application code from the main context
COPY . /opt/app
# Run deployment scripts using the remote "scripts" context
RUN --mount=from=scripts,target=/scripts /scripts/main.sh
```
#### Example: dynamic builds with custom dependencies
In some scenarios, you might need to dynamically inject configuration files or
dependencies into the build from external sources. Named contexts make this
straightforward by allowing you to mount different configurations without
modifying the default build context.
```console
$ docker build --build-context config=./configs/prod .
```
Example Dockerfile:
```dockerfile
# syntax=docker/dockerfile:1
FROM nginx:alpine
# Use the "config" context for environment-specific configurations