Skip to content

Conversation

@flouthoc
Copy link
Collaborator

@flouthoc flouthoc commented May 10, 2022

As of now for complicated builds the ability to only access files from one location became quite limiting OTOH we already support multi-stage builds where you can copy files from other parts of the Containerfile by adding the --from flag and pointing it to the name of another Containerfile stage or a remote image but this is still limited.

The new named build context feature is an extension of this pattern. You can now define additional build contexts when running the build command, give them a name, and then access them inside a Containerfile the same way you previously did with build stages.

Additional build contexts can be defined with a new --build-context [name]=[value] flag. The key component defines the name for your build context and the value can be:

 * Local directory – e.g. --build-context project2=../path/to/project2/src
 * HTTP URL to a tarball – e.g. --build-context src=https://example.org/releases/src.tar
 * Container image – Define with a `docker-image://` prefix, e.g. --build-context alpine=docker-image://alpine:3.15, ( also supports `docker://`, `container-image://`)

On the Containerfile side, you can reference the build context on all commands that accept the from parameter. Here’s how that might look:

FROM [name]
COPY --from=[name] ...
RUN --mount=from=[name] …

The value of [name] is matched with the following priority order:

  • Named build context defined with --build-context [name]=..
  • Stage defined with AS [name] inside Dockerfile
  • Remote image [name] in a container registry

Added Features

  • Pinning images for FROM,COPY and ADD.
  • Specifying multiple buildcontexts from different projects
    and using them with --from in ADD and COPY directive
  • Override a Remote Dependency with a Local One.
  • Using additional context from external Tar

More Context: https://www.docker.com/blog/dockerfiles-now-support-multiple-build-contexts/

@openshift-ci
Copy link
Contributor

openshift-ci bot commented May 10, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: flouthoc

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@flouthoc flouthoc changed the title buildkit, build: add support for additionalBuildContext in builds via --build-context buildkit, build: add support for additionalBuildContext in builds via --build-context May 10, 2022
@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch from 2ba51cd to bf81f15 Compare May 10, 2022 10:27
@flouthoc
Copy link
Collaborator Author

@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch 4 times, most recently from 08dfee6 to b1bb97f Compare May 10, 2022 12:33
@flouthoc flouthoc requested review from nalind and rhatdan May 10, 2022 14:47
Copy link
Member

@nalind nalind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a stage named using "FROM base AS name" but referred to in a "COPY --from=" instruction that refers to it by its numeric position require additional handling?

Needs to test cases with a stage named using "FROM base AS name" syntax and a build context named "name" to check that "COPY --from" looks in the right place, and that "FROM name" does the expected thing.

Needs to test "RUN --mount from=...." pointing to the various kinds of additional build contexts.

The patch touches platformsForBaseImages(), so we should test how it affects the --all-platforms build flag.

@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch 2 times, most recently from 91fe328 to d6819c8 Compare May 11, 2022 09:36
@flouthoc
Copy link
Collaborator Author

Does a stage named using "FROM base AS name" but referred to in a "COPY --from=" instruction that refers to it by its numeric position require additional handling?

Needs to test cases with a stage named using "FROM base AS name" syntax and a build context named "name" to check that "COPY --from" looks in the right place, and that "FROM name" does the expected thing.

Needs to test "RUN --mount from=...." pointing to the various kinds of additional build contexts.

The patch touches platformsForBaseImages(), so we should test how it affects the --all-platforms build flag.

@nalind Added new tests as listed below which should cover all the requested scenarios, also added comments where we match with buildx parity.

ok 1 build-with-additional-build-context and COPY, test pinning image
ok 2 build-with-additional-build-context and COPY, stagename and additional-context conflict
ok 3 build-with-additional-build-context and COPY, additionalContext and numeric value of stage
ok 4 build-with-additional-build-context and FROM, stagename and additional-context conflict
ok 5 build-with-additional-build-context and COPY, additional context from host
ok 6 build-with-additional-build-context and COPY, additional context from external URL
ok 7 build-with-additional-build-context and FROM, pin busybox to alpine
ok 8 build-with-additional-build-context and RUN --mount=from=, additional-context and also test conflict with stagename
ok 9 build-with-additional-build-context and RUN --mount=from=, additional-context not image and also test conflict with stagename
ok 10 bud-multiple-platform for --all-platform with additional-build-context

@flouthoc flouthoc requested a review from nalind May 11, 2022 09:38
@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch 2 times, most recently from 585e4dd to f7d0cc3 Compare May 11, 2022 09:52
if isStage, err := s.executor.waitForStage(s.ctx, from, s.stages[:s.index]); isStage && err != nil {
return err
if additionalBuildContext, ok := s.executor.additionalBuildContexts[from]; ok {
if additionalBuildContext != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we never put nil values in the map, then we don't need to check for them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added a check so in future if someone edits top level code we don't endup SEGSEV. But I can drop it if needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and drop it. I can't conceive of a use case for setting nil values in this map.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropped this nil and other similar nil check for additionalBuildContext.

@nalind
Copy link
Member

nalind commented May 12, 2022

Should have a test with "RUN --mount=type=bind,from=..." where the additional build context specified as the "from" value was given as a URL, and at least one of the "RUN --mount" tests should be using the "src" option to look in a subdirectory of the additional build context, to make sure we're not forgetting that in the logic.

@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch from f7d0cc3 to 28dfc09 Compare May 13, 2022 10:34
@flouthoc
Copy link
Collaborator Author

@nalind Added a new test build-with-additional-build-context and RUN --mount=from=, additional-context is URL and mounted from subdir that should cover both.

@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch 2 times, most recently from 7c43831 to fb82bf9 Compare May 13, 2022 11:35
@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch from fb82bf9 to 1815612 Compare May 16, 2022 08:17
@flouthoc
Copy link
Collaborator Author

@nalind Could you PTAL again.

Copy link
Member

@nalind nalind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits in error messages, a couple of redundant variables, and would like an answer about https://github.com/containers/buildah/pull/3978/files#r871374285.

@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch 2 times, most recently from 2007bd0 to c020f98 Compare May 17, 2022 10:40
@flouthoc flouthoc requested a review from nalind May 17, 2022 10:41
@nalind
Copy link
Member

nalind commented May 17, 2022

There's still a redundant --jobs=1 in the bud-multiple-platform for --all-platform with additional-build-context test, otherwise LGTM.

As builds got more complicated, the ability to only access files from one location became quite limiting. With `multi-stage` builds where you can `copy` files from other parts of the Containerfile by adding the `--from` flag and pointing it to the name of another Containerfile stage or a remote image.

The new named build context feature is an extension of this pattern. You can now define additional build contexts when running the build command, give them a name, and then access them inside a Dockerfile the same way you previously did with build stages.

Additional build contexts can be defined with a new `--build-context [name]=[value]` flag. The key component defines the name for your build context and the value can be:

```console

    Local directory – e.g. --build-context project2=../path/to/project2/src
    HTTP URL to a tarball – e.g. --build-context src=https://example.org/releases/src.tar
    Container image – Define with a docker-image:// prefix, e.g. --build-context alpine=docker-image://alpine:3.15, ( also supports docker://, container-image:// )
```

On the Containerfile side, you can reference the build context on all commands that accept the “from” parameter. Here’s how that might look:
```Dockerfile
FROM [name]
COPY --from=[name] ...
RUN --mount=from=[name] …
```

The value of [name] is matched with the following priority order:

* Named build context defined with `--build-context [name]=..`
* Stage defined with `AS [name]` inside Dockerfile
* Remote image `[name]` in a container registry

Added Features

* Pinning images for `FROM` and `COPY`
* Specifying multiple buildcontexts from different projects
  and using them with `--from` in `ADD` and `COPY` directive
* Override a Remote Dependency with a Local One.
* Using additional context from external `Tar`

Signed-off-by: Aditya R <[email protected]>
@flouthoc flouthoc force-pushed the buildkit-add-support-for-additional-context branch from c020f98 to c2adbad Compare May 17, 2022 18:14
@flouthoc
Copy link
Collaborator Author

There's still a redundant --jobs=1 in the bud-multiple-platform for --all-platform with additional-build-context test, otherwise LGTM.

Fixed.

@flouthoc
Copy link
Collaborator Author

@rhatdan @TomSweeneyRedHat PTAL

@rhatdan
Copy link
Member

rhatdan commented May 18, 2022

Nice work @flouthoc
/lgtm

@openshift-ci openshift-ci bot added the lgtm label May 18, 2022
@openshift-merge-robot openshift-merge-robot merged commit ed9e71d into containers:main May 18, 2022
flouthoc added a commit to flouthoc/podman that referenced this pull request Jun 2, 2022
…remote

Feature of additional build context added here containers/buildah#3978
already exists on `podman` following PR just enables this feature of
`podman-remote` and `podman on macOS` setups.

Signed-off-by: Aditya R <[email protected]>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants