mirror of
https://github.com/docker/compose.git
synced 2025-07-24 06:04:57 +02:00
fix panic when using 'compose up --build'
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
parent
8b1b70833e
commit
537f023a3b
@ -20,8 +20,8 @@ import (
|
|||||||
"github.com/moby/buildkit/util/tracing/detect"
|
"github.com/moby/buildkit/util/tracing/detect"
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
|
|
||||||
_ "github.com/moby/buildkit/util/tracing/detect/delegated" //nolint:revive
|
_ "github.com/moby/buildkit/util/tracing/detect/delegated" //nolint:blank-imports
|
||||||
_ "github.com/moby/buildkit/util/tracing/env" //nolint:revive
|
_ "github.com/moby/buildkit/util/tracing/env" //nolint:blank-imports
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -173,7 +173,11 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
|
|||||||
if len(opt.Platforms) > 1 {
|
if len(opt.Platforms) > 1 {
|
||||||
opt.Exports = []bclient.ExportEntry{{
|
opt.Exports = []bclient.ExportEntry{{
|
||||||
Type: "docker",
|
Type: "docker",
|
||||||
|
Attrs: map[string]string{
|
||||||
|
"load": "true",
|
||||||
|
},
|
||||||
}}
|
}}
|
||||||
|
opt.Platforms = []specs.Platform{}
|
||||||
}
|
}
|
||||||
opts[imageName] = opt
|
opts[imageName] = opt
|
||||||
continue
|
continue
|
||||||
|
@ -37,9 +37,9 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/buildx/build"
|
"github.com/docker/buildx/build"
|
||||||
"github.com/docker/buildx/driver"
|
"github.com/docker/buildx/driver"
|
||||||
_ "github.com/docker/buildx/driver/docker" //nolint:revive
|
_ "github.com/docker/buildx/driver/docker" //nolint:blank-imports
|
||||||
_ "github.com/docker/buildx/driver/docker-container" //nolint:revive
|
_ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
|
||||||
_ "github.com/docker/buildx/driver/kubernetes" //nolint:revive
|
_ "github.com/docker/buildx/driver/kubernetes" //nolint:blank-imports
|
||||||
xprogress "github.com/docker/buildx/util/progress"
|
xprogress "github.com/docker/buildx/util/progress"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,8 +56,10 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]bu
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
w := xprogress.NewPrinter(progressCtx, s.stdout(), os.Stdout, mode)
|
w := xprogress.NewPrinter(progressCtx, s.stdout(), os.Stdout, mode)
|
||||||
|
|
||||||
// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
|
// Get the DockerAPI if a "docker" export is defined (ie: up and run command), otherwise get nil and let use the default buildx builder
|
||||||
response, err := build.Build(ctx, dis, opts, nil, filepath.Dir(s.configFile().Filename), w)
|
API := getDockerAPI(s.dockerCli, opts)
|
||||||
|
|
||||||
|
response, err := build.Build(ctx, dis, opts, API, filepath.Dir(s.configFile().Filename), w)
|
||||||
errW := w.Wait()
|
errW := w.Wait()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = errW
|
err = errW
|
||||||
@ -252,3 +254,29 @@ func configFromContext(endpointName string, s ctxstore.Reader) (clientcmd.Client
|
|||||||
}
|
}
|
||||||
return ctxkube.ConfigFromContext(endpointName, s)
|
return ctxkube.ConfigFromContext(endpointName, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type internalAPI struct {
|
||||||
|
dockerCli command.Cli
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *internalAPI) DockerAPI(name string) (dockerclient.APIClient, error) {
|
||||||
|
if name == "" {
|
||||||
|
name = a.dockerCli.CurrentContext()
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@ -258,6 +258,7 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
|
|||||||
assert.NilError(t, result.Error)
|
assert.NilError(t, result.Error)
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/platforms", "down")
|
||||||
_ = c.RunDockerCmd(t, "buildx", "rm", "-f", "build-platform")
|
_ = c.RunDockerCmd(t, "buildx", "rm", "-f", "build-platform")
|
||||||
_ = c.RunDockerCmd(t, "rm", "-f", "registry")
|
_ = c.RunDockerCmd(t, "rm", "-f", "registry")
|
||||||
})
|
})
|
||||||
@ -279,6 +280,12 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
|
|||||||
res.Assert(t, icmd.Expected{Out: `"architecture": "arm64",`})
|
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())
|
||||||
|
res.Assert(t, icmd.Expected{Out: "platforms-platforms-1 exited with code 0"})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildPlatformsStandardErrors(t *testing.T) {
|
func TestBuildPlatformsStandardErrors(t *testing.T) {
|
||||||
|
@ -12,6 +12,11 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
FROM alpine
|
FROM --platform=$BUILDPLATFORM golang:alpine AS build
|
||||||
|
|
||||||
RUN echo "SUCCESS"
|
ARG TARGETPLATFORM
|
||||||
|
ARG BUILDPLATFORM
|
||||||
|
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
|
||||||
|
|
||||||
|
FROM alpine
|
||||||
|
COPY --from=build /log /log
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
services:
|
services:
|
||||||
platforms:
|
platforms:
|
||||||
image: localhost:5001/build-test-platform:test
|
image: localhost:5001/build-test-platform:test
|
||||||
platform: linux/amd64
|
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
platforms:
|
platforms:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user