Explorar o código

Merge pull request #1354 from ulyssessouza/fix-pull-arch

Take `platform` in account on pulling and building images
Guillaume Tardif %!s(int64=4) %!d(string=hai) anos
pai
achega
3f600281e6
Modificáronse 3 ficheiros con 34 adicións e 5 borrados
  1. 22 4
      local/compose/build.go
  2. 11 1
      local/compose/convergence.go
  3. 1 0
      local/compose/pull.go

+ 22 - 4
local/compose/build.go

@@ -26,12 +26,14 @@ import (
 	"github.com/docker/compose-cli/api/compose"
 
 	"github.com/compose-spec/compose-go/types"
+	"github.com/containerd/containerd/platforms"
 	"github.com/docker/buildx/build"
 	"github.com/docker/buildx/driver"
 	_ "github.com/docker/buildx/driver/docker" // required to get default driver registered
 	"github.com/docker/buildx/util/progress"
 	"github.com/docker/docker/errdefs"
 	bclient "github.com/moby/buildkit/client"
+	specs "github.com/opencontainers/image-spec/specs-go/v1"
 )
 
 func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
@@ -41,7 +43,10 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
 		if service.Build != nil {
 			imageName := getImageName(service, project.Name)
 			imagesToBuild = append(imagesToBuild, imageName)
-			buildOptions := s.toBuildOptions(service, project.WorkingDir, imageName)
+			buildOptions, err := s.toBuildOptions(service, project.WorkingDir, imageName)
+			if err != nil {
+				return err
+			}
 			buildOptions.Pull = options.Pull
 			buildOptions.BuildArgs = options.Args
 			opts[imageName] = buildOptions
@@ -75,7 +80,10 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
 				continue
 			}
 			imagesToBuild = append(imagesToBuild, imageName)
-			opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName)
+			opts[imageName], err = s.toBuildOptions(service, project.WorkingDir, imageName)
+			if err != nil {
+				return err
+			}
 			continue
 		}
 		if service.Image != "" {
@@ -148,7 +156,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
 	return err
 }
 
-func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string, imageTag string) build.Options {
+func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string, imageTag string) (build.Options, error) {
 	var tags []string
 	tags = append(tags, imageTag)
 
@@ -157,6 +165,15 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath
 	}
 	var buildArgs map[string]string
 
+	var plats []specs.Platform
+	if service.Platform != "" {
+		p, err := platforms.Parse(service.Platform)
+		if err != nil {
+			return build.Options{}, err
+		}
+		plats = append(plats, p)
+	}
+
 	return build.Options{
 		Inputs: build.Inputs{
 			ContextPath:    path.Join(contextPath, service.Build.Context),
@@ -166,7 +183,8 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath
 		Tags:      tags,
 		Target:    service.Build.Target,
 		Exports:   []bclient.ExportEntry{{Type: "image", Attrs: map[string]string{}}},
-	}
+		Platforms: plats,
+	}, nil
 }
 
 func flatten(in types.MappingWithEquals) map[string]string {

+ 11 - 1
local/compose/convergence.go

@@ -23,9 +23,11 @@ import (
 	"time"
 
 	"github.com/compose-spec/compose-go/types"
+	"github.com/containerd/containerd/platforms"
 	moby "github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types/filters"
 	"github.com/docker/docker/api/types/network"
+	specs "github.com/opencontainers/image-spec/specs-go/v1"
 	"golang.org/x/sync/errgroup"
 
 	"github.com/docker/compose-cli/api/compose"
@@ -280,7 +282,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
 	if err != nil {
 		return err
 	}
-	created, err := s.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, networkingConfig, nil, name)
+	var plat *specs.Platform
+	if service.Platform != "" {
+		p, err := platforms.Parse(service.Platform)
+		if err != nil {
+			return err
+		}
+		plat = &p
+	}
+	created, err := s.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, networkingConfig, plat, name)
 	if err != nil {
 		return err
 	}

+ 1 - 0
local/compose/pull.go

@@ -96,6 +96,7 @@ func (s *composeService) Pull(ctx context.Context, project *types.Project) error
 
 			stream, err := s.apiClient.ImagePull(ctx, service.Image, moby.ImagePullOptions{
 				RegistryAuth: base64.URLEncoding.EncodeToString(buf),
+				Platform:     service.Platform,
 			})
 			if err != nil {
 				w.Event(progress.Event{