Skip to main content
Use contexts when a chart Dockerfile needs content from another generated target, a base image, or a local path. This lets stages depend on each other without merging their chart Dockerfiles.

Use additional contexts

A context maps a name to an input: Read more in the Docker Bake contexts documentation.

Use the default build context

For every generated target except build, BuildCharts adds a context named build that points to the generated build target:
Chart Dockerfiles can use build as an upstream stage:
This works because target:build refers to the generated Bake target named build. BuildCharts requires exactly one build target in build.yml, so the context is stable and unambiguous. Keep build for this automatic context. Do not define build under with.contexts or types.<type>.contexts.

Add a target dependency

Use with.contexts when one target needs a named dependency that other targets do not.
The generated docker target receives:
  • The automatic build = "target:build" context
  • The configured publish = "target:publish" context
Use the configured context in the chart Dockerfile like any named stage:

Share a dependency by type

Use types.<type>.contexts when every target of a type needs the same dependency.
This keeps repeated context wiring out of each target definition.

Use image and local contexts

Contexts can also point to images or local paths.
A docker chart can consume the base image context:
A nuget chart can copy from the git local context:
For commands that need the full .git directory, bind mount it for the duration of the RUN step:

Context naming rules

Use context names that are valid HCL identifiers:
  • Start with a letter or _
  • Contain only letters, numbers, and _
  • Use non-empty values
Keep each context name unique per generated target. Do not define the same context name in both with.contexts and types.<type>.contexts.
Last modified on June 2, 2026