diff --git a/pkg/compose/build_buildkit.go b/pkg/compose/build_buildkit.go index fb3981ab3..b11251ec2 100644 --- a/pkg/compose/build_buildkit.go +++ b/pkg/compose/build_buildkit.go @@ -56,10 +56,7 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]bu defer cancel() w := xprogress.NewPrinter(progressCtx, s.stdout(), os.Stdout, mode) - // Get the DockerAPI if a "docker" export is defined (ie: up and run command), otherwise get nil and let use the default buildx builder - API := getDockerAPI(s.dockerCli, opts) - - response, err := build.Build(ctx, dis, opts, API, filepath.Dir(s.configFile().Filename), w) + response, err := build.Build(ctx, dis, opts, &internalAPI{dockerCli: s.dockerCli}, filepath.Dir(s.configFile().Filename), w) errW := w.Wait() if err == nil { err = errW @@ -265,18 +262,3 @@ func (a *internalAPI) DockerAPI(name string) (dockerclient.APIClient, error) { } return clientForEndpoint(a.dockerCli, name) } - -func dockerAPI(dockerCli command.Cli) *internalAPI { - return &internalAPI{dockerCli: dockerCli} -} - -func getDockerAPI(cli command.Cli, opts map[string]build.Options) *internalAPI { - for _, opt := range opts { - for _, export := range opt.Exports { - if export.Type == "docker" { - return dockerAPI(cli) - } - } - } - return nil -} diff --git a/pkg/e2e/build_test.go b/pkg/e2e/build_test.go index 7aacf2b5c..b91d42b61 100644 --- a/pkg/e2e/build_test.go +++ b/pkg/e2e/build_test.go @@ -281,6 +281,22 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) { }) + t.Run("multi-arch multi service builds ok", func(t *testing.T) { + res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms", + "-f", "fixtures/build-test/platforms/compose-multiple-platform-builds.yaml", "build") + assert.NilError(t, res.Error, res.Stderr()) + res = c.RunDockerCmd(t, "manifest", "inspect", "--insecure", "localhost:5001/build-test-platform-a:test") + res.Assert(t, icmd.Expected{Out: `"architecture": "amd64",`}) + res.Assert(t, icmd.Expected{Out: `"architecture": "arm64",`}) + res = c.RunDockerCmd(t, "manifest", "inspect", "--insecure", "localhost:5001/build-test-platform-b:test") + res.Assert(t, icmd.Expected{Out: `"architecture": "amd64",`}) + res.Assert(t, icmd.Expected{Out: `"architecture": "arm64",`}) + res = c.RunDockerCmd(t, "manifest", "inspect", "--insecure", "localhost:5001/build-test-platform-c:test") + res.Assert(t, icmd.Expected{Out: `"architecture": "amd64",`}) + res.Assert(t, icmd.Expected{Out: `"architecture": "arm64",`}) + + }) + t.Run("multi-arch up --build", func(t *testing.T) { res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms", "up", "--build") assert.NilError(t, res.Error, res.Stderr()) diff --git a/pkg/e2e/fixtures/build-test/platforms/compose-multiple-platform-builds.yaml b/pkg/e2e/fixtures/build-test/platforms/compose-multiple-platform-builds.yaml new file mode 100644 index 000000000..0f8ce9936 --- /dev/null +++ b/pkg/e2e/fixtures/build-test/platforms/compose-multiple-platform-builds.yaml @@ -0,0 +1,23 @@ +services: + serviceA: + image: localhost:5001/build-test-platform-a:test + build: + context: ./contextServiceA + platforms: + - linux/amd64 + - linux/arm64 + serviceB: + image: localhost:5001/build-test-platform-b:test + build: + context: ./contextServiceB + platforms: + - linux/amd64 + - linux/arm64 + serviceC: + image: localhost:5001/build-test-platform-c:test + build: + context: ./contextServiceC + platforms: + - linux/amd64 + - linux/arm64 + diff --git a/pkg/e2e/fixtures/build-test/platforms/contextServiceA/Dockerfile b/pkg/e2e/fixtures/build-test/platforms/contextServiceA/Dockerfile new file mode 100644 index 000000000..057ed864c --- /dev/null +++ b/pkg/e2e/fixtures/build-test/platforms/contextServiceA/Dockerfile @@ -0,0 +1,22 @@ +# Copyright 2020 Docker Compose CLI authors + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM --platform=$BUILDPLATFORM golang:alpine AS build + +ARG TARGETPLATFORM +ARG BUILDPLATFORM +RUN echo "I'm Service A and I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log + +FROM alpine +COPY --from=build /log /log diff --git a/pkg/e2e/fixtures/build-test/platforms/contextServiceB/Dockerfile b/pkg/e2e/fixtures/build-test/platforms/contextServiceB/Dockerfile new file mode 100644 index 000000000..88eecb902 --- /dev/null +++ b/pkg/e2e/fixtures/build-test/platforms/contextServiceB/Dockerfile @@ -0,0 +1,22 @@ +# Copyright 2020 Docker Compose CLI authors + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM --platform=$BUILDPLATFORM golang:alpine AS build + +ARG TARGETPLATFORM +ARG BUILDPLATFORM +RUN echo "I'm Service B and I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log + +FROM alpine +COPY --from=build /log /log diff --git a/pkg/e2e/fixtures/build-test/platforms/contextServiceC/Dockerfile b/pkg/e2e/fixtures/build-test/platforms/contextServiceC/Dockerfile new file mode 100644 index 000000000..1b9172994 --- /dev/null +++ b/pkg/e2e/fixtures/build-test/platforms/contextServiceC/Dockerfile @@ -0,0 +1,22 @@ +# Copyright 2020 Docker Compose CLI authors + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM --platform=$BUILDPLATFORM golang:alpine AS build + +ARG TARGETPLATFORM +ARG BUILDPLATFORM +RUN echo "I'm Service C and I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log + +FROM alpine +COPY --from=build /log /log