Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerfile: add support for named contexts #2521

Merged
merged 3 commits into from
Dec 27, 2021

Conversation

tonistiigi
Copy link
Member

Related moby/moby#37129
Related docker/buildx#447 (comment)

Stages and implicit stages from image names can be
redefined with build options.

This enables using more that one source directory
and reusing results from other builds. This can also
be used to use a local image from other build without
including a registry.

Contexts need to be defined as context:name= frontend
options. The value can be image, git repository,
URL, local directory, or a frontend input.

E.g.

FROM alpine AS base
..
..

buildctl build --frontend=dockerfile.v0 --local context=. --local dockerfile=. --opt context:alpine=docker-image://alpine:3.15

buildctl build --frontend=dockerfile.v0 --local context=. --local dockerfile=. --local base=path/to/base --opt context:base=local:base

Signed-off-by: Tonis Tiigi [email protected]

@tonistiigi tonistiigi force-pushed the dockerfile-named-contexts branch from eedec9c to d2f85d4 Compare December 14, 2021 07:26
@tonistiigi
Copy link
Member Author

Added additional test for series of multi-platform builds passing result from first to the next one.

Stages and implicit stages from image names can be
redefined with build options.

This enables using more that one source directory
and reusing results from other builds. This can also
be used to use a local image from other build without
including a registry.

Contexts need to be defined as `context:name=` frontend
options. The value can be image, git repository,
URL, local directory or a frontend input.

Signed-off-by: Tonis Tiigi <[email protected]>
@tonistiigi tonistiigi force-pushed the dockerfile-named-contexts branch from bd0d7d3 to 23709ef Compare December 16, 2021 04:57
@crazy-max
Copy link
Member

crazy-max commented Dec 18, 2021

Looks great! I have this use case where loaded a local image would be helpful instead of using a local registry.

@AkihiroSuda AkihiroSuda merged commit a8278dd into moby:master Dec 27, 2021
@crazy-max crazy-max added this to the v0.10.0 milestone Feb 4, 2022
@rittneje
Copy link
Contributor

@tonistiigi This feature seems really useful. It would be nice to add it to the documentation. Based upon your changeset it seems your options are:

  1. docker-image://<image name> to reference an image in a registry
  2. git://<git repo url>, http://<git repo url>, https://<git repo url> to reference a Git repository
  3. local:<path> to reference a local directory
  4. input:<key> to reference some previously built image; requires --opt input-metadata:<key>=<json>, where <json> is, I assume, obtained by reading the output from --metadata-file

@rittneje
Copy link
Contributor

Is the example you linked correct? It just has project=path/to/project/source without the local: prefix. Is that only needed for buildctl?

Why can't input be used with buildctl? It would be nice for when we are building a bunch of images off the same base image, and don't want to waste time with the cache or passing build args that the sub-stages don't need.

In any case, I would not generally look to the buildx documentation for buildctl, so it would be nice if it were documented here too.

@tonistiigi
Copy link
Member Author

tonistiigi commented Feb 13, 2022

Is the example you linked correct? It just has project=path/to/project/source without the local: prefix. Is that only needed for buildctl?

Yes. Buildx has a new flag for this. Buildctl has generic --opt that requires prefix and another flag to load the local.

Why can't input be used with buildctl?

Because there is nothing that keeps the result allocated when your first build completes. Pipelines of multiple builds are out of scope for buildctl which is a test client.

In any case, I would not generally look to the buildx documentation for buildctl,

This is a Dockerfile frontend feature. Other frontends can of course use the same keys to also connect to --build-context flag directly. If a frontend looked for the existence of these keys, a feature like this worked on any old version of buildctl.

@rittneje
Copy link
Contributor

I'm confused. To be honest, right now the various --opt flags for buildctl are poorly documented. The readme has a few (unexplained) examples, but there is no authoritative list that I can find. Even if I did think to reference the buildx docs, I certainly wouldn't guess that --build-context becomes --opt context, with an extra quirk for local. I'm going to file a separate issue to improve the buildctl docs on this point.

I'm also not sure what you mean by buildctl being a "test client". The docs never refer to it as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants