Skip to content

Commit b9a3f19

Browse files
committed
build: add manuals section about named contexts
Signed-off-by: David Karlsson <[email protected]>
1 parent f6e5164 commit b9a3f19

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

content/manuals/build/concepts/context.md

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,193 @@ README-secret.md
567567

568568
All of the README files are included. The middle line has no effect because
569569
`!README*.md` matches `README-secret.md` and comes last.
570+
571+
## Named contexts
572+
573+
In addition to the default build context (the positional argument to the
574+
`docker build` command), you can also pass additional named contexts to builds.
575+
576+
Named contexts are specified using the `--build-context` flag, followed by a
577+
name-value pair. This lets you include files and directories from multiple
578+
sources during the build, while keeping them logically separated.
579+
580+
```console
581+
$ docker build --build-context docs=./docs .
582+
```
583+
584+
In this example:
585+
586+
- The named `docs` context points to the `./docs` directory.
587+
- The default context (`.`) points to the current working directory.
588+
589+
### Using named contexts in a Dockerfile
590+
591+
Dockerfile instructions can reference named contexts as if they are stages in a
592+
multi-stage build.
593+
594+
For example, the following Dockerfile:
595+
596+
1. Uses a `COPY` instruction to copy files from the default context into the
597+
current build stage.
598+
2. Bind mounts the files in a named context to process the files as part of the
599+
build.
600+
601+
```dockerfile
602+
# syntax=docker/dockerfile:1
603+
FROM buildbase
604+
WORKDIR /app
605+
606+
# Copy all files from the default context into /app/src in the build container
607+
COPY . /app/src
608+
RUN make bin
609+
610+
# Mount the files from the named "docs" context to build the documentation
611+
RUN --mount=from=docs,target=/app/docs \
612+
make manpages
613+
```
614+
615+
### Use cases for named contexts
616+
617+
Using named contexts allows for greater flexibility and efficiency when
618+
building Docker images. Here are some scenarios where using named contexts can
619+
be useful:
620+
621+
#### Example: combine local and remote sources
622+
623+
You can define separate named contexts for different types of sources. For
624+
example, consider a project where the application source code is local, but the
625+
deployment scripts are stored in a Git repository:
626+
627+
```console
628+
$ docker build --build-context scripts=https://github.com/user/deployment-scripts.git .
629+
```
630+
631+
In the Dockerfile, you can use these contexts independently:
632+
633+
```dockerfile
634+
# syntax=docker/dockerfile:1
635+
FROM alpine:latest
636+
637+
# Copy application code from the main context
638+
COPY . /opt/app
639+
640+
# Run deployment scripts using the remote "scripts" context
641+
RUN --mount=from=scripts,target=/scripts /scripts/main.sh
642+
```
643+
644+
#### Example: dynamic builds with custom dependencies
645+
646+
In some scenarios, you might need to dynamically inject configuration files or
647+
dependencies into the build from external sources. Named contexts make this
648+
straightforward by allowing you to mount different configurations without
649+
modifying the default build context.
650+
651+
```console
652+
$ docker build --build-context config=./configs/prod .
653+
```
654+
655+
Example Dockerfile:
656+
657+
```dockerfile
658+
# syntax=docker/dockerfile:1
659+
FROM nginx:alpine
660+
661+
# Use the "config" context for environment-specific configurations
662+
COPY --from=config nginx.conf /etc/nginx/nginx.conf
663+
```
664+
665+
#### Example: pin or override images
666+
667+
You can refer to named contexts in a Dockerfile the same way you can refer to
668+
an image. That means you can change an image reference in your Dockerfile by
669+
overriding it with a named context. For example, given the following
670+
Dockerfile:
671+
672+
```dockerfile
673+
FROM alpine:{{% param example_alpine_version %}}
674+
```
675+
676+
If you want to force image reference to resolve to a different version, without
677+
changing the Dockerfile, you can pass a context with the same name to the
678+
build. For example:
679+
680+
```console
681+
docker buildx build --build-context alpine:{{% param example_alpine_version %}}=docker-image://alpine:edge .
682+
```
683+
684+
The `docker-image://` prefix marks the context as an image reference. The
685+
reference can be a local image or an image in your registry.
686+
687+
### Named contexts with Bake
688+
689+
[Bake](/manuals/build/bake/_index.md) is a tool built into `docker build` that
690+
lets you manage your build configuration with a configuration file. Bake fully
691+
supports named contexts.
692+
693+
To define named contexts in a Bake file:
694+
695+
```hcl {title=docker-bake.hcl}
696+
target "app" {
697+
contexts = {
698+
docs = "./docs"
699+
}
700+
}
701+
```
702+
703+
This is equivalent to the following CLI invocation:
704+
705+
```console
706+
$ docker build --build-context docs=./docs .
707+
```
708+
709+
#### Linking targets with named contexts
710+
711+
In addition to making complex builds more manageable, Bake also provides
712+
additional features on top of what you can do with `docker build` on the CLI.
713+
You can use named contexts to create build pipelines, where one target depends
714+
on and builds on top of another. For example, consider a Docker build setup
715+
where you have two Dockerfiles:
716+
717+
- `base.Dockerfile`: for building a base image
718+
- `app.Dockerfile`: for building an application image
719+
720+
The `app.Dockerfile` uses the image produced by `base.Dockerfile` as it's base
721+
image:
722+
723+
```dockerfile {title=app.Dockerfile}
724+
FROM mybaseimage
725+
```
726+
727+
Normally, you would have to build the base image first, and then either load it
728+
to Docker Engine's local image store or push it to a registry. With Bake, you
729+
can reference other targets directly, creating a dependency between the `app`
730+
target and the `base` target.
731+
732+
```hcl {title=docker-bake.hcl}
733+
target "base" {
734+
dockerfile = "base.Dockerfile"
735+
}
736+
737+
target "app" {
738+
dockerfile = "app.Dockerfile"
739+
contexts = {
740+
# the target: prefix indicates that 'base' is a Bake target
741+
mybaseimage = "target:base"
742+
}
743+
}
744+
```
745+
746+
With this configuration, references to `mybaseimage` in `app.Dockerfile` use
747+
the results from building the `base` target. Building the `app` target will
748+
also trigger a rebuild of `mybaseimage`, if necessary:
749+
750+
```console
751+
$ docker buildx bake app
752+
```
753+
754+
### Further reading
755+
756+
For more information about working with named contexts, see:
757+
758+
- [`--build-context` CLI reference](/reference/cli/docker/buildx/build.md#build-context)
759+
- [Using Bake with additional contexts](/manuals/build/bake/contexts.md)

0 commit comments

Comments
 (0)