dotnet / docker-tools

This is a repo to house some common tools for our various docker repos.
MIT License
124 stars 46 forks source link

Enable use of BuildKit in CI #1159

Closed lbussell closed 1 year ago

lbussell commented 1 year ago

We want to be able to build Dockerfiles and samples with newer Docker functionality (multi-platform builds, etc). See https://github.com/dotnet/dotnet-docker/pull/4742.

We previously tried to enable BuildKit in ImageBuilder, but encountered an issue where multi-stage Dockerfiles will lose their variant (e.g. arm64v8 becomes arm64), likely due to us building all arm32v7 and arm64v8 Dockerfiles on an arm64 host, and not specifying the build target for the final stage in the Dockerfile build. See #1132 and #1136.

Now we should investigate whether or not we can install and use buildx on our build agents to resolve this issue.

dotnet-issue-labeler[bot] commented 1 year ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

lbussell commented 1 year ago

Related:

This is the same issue, but what I have observed is the opposite behavior. @mthalman mentions in https://github.com/dotnet/dotnet-docker/issues/4438#issuecomment-1440681555 that the issue might have been resolved due to build agent updates, so that might have changed the behavior here.

lbussell commented 1 year ago

@richlander since you are the resident expert on multi-platform Docker builds, can you provide some insight here? If having no --platform= specified in the FROM instruction implies that $TARGETPLATFORM will be the default platform for the final stage, why would the output of the image be different from what's passed in the --platform argument to docker build? Could this be a consequence of not using buildx? We are not yet trying to build multiple platforms at once, just one at a time.

I am still having troubles getting buildx working on Azure build agents. Docker has a "setup buildx" GitHub Action, but GitHub Actions runners already have buildx installed.

richlander commented 1 year ago

Let's assume that $BUILDPLATFORM is not used in the Dockerfile to make this discussion simple.

The value of --platform (via the CLI) will be used for two things:

Here's a good example.

$ echo FROM arm32v7/alpine > Dockerfile
$ docker build -t test --platform linux/amd64 .
$ docker inspect test | grep Arch
        "Architecture": "amd64",
$ docker run --rm test uname -a
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
Linux 82793be52fae 5.15.49-linuxkit-pr #1 SMP PREEMPT Thu May 25 07:27:39 UTC 2023 armv7l Linux
lbussell commented 1 year ago

We will get this for free with Alpine 3.18, since it includes Docker version 23, which sets buildkit+buildx to the default

lbussell commented 1 year ago

This is completed with https://github.com/dotnet/docker-tools/pull/1169