@@ -47,20 +47,63 @@ func (b *ColimaBackend) SetDockerHost(dockerHost string) {
4747 b .dockerBackend = NewDockerBackend ()
4848}
4949
50+ // GetVMArch returns the architecture of the Colima VM
51+ // Returns "amd64", "arm64", or empty string if detection fails
52+ func (b * ColimaBackend ) GetVMArch (ctx context.Context ) string {
53+ // Use colima status to get VM info
54+ cmd := exec .CommandContext (ctx , "colima" , "status" , "-p" , b .profile , "--json" )
55+ output , err := cmd .Output ()
56+ if err != nil {
57+ // Fallback: try running uname -m inside docker
58+ unameCmd := exec .CommandContext (ctx , "docker" , "run" , "--rm" , "alpine" , "uname" , "-m" )
59+ unameCmd .Env = append (os .Environ (), fmt .Sprintf ("DOCKER_HOST=%s" , b .dockerHost ))
60+ unameOutput , unameErr := unameCmd .Output ()
61+ if unameErr == nil {
62+ arch := strings .TrimSpace (string (unameOutput ))
63+ return NormalizeArch (arch )
64+ }
65+ return ""
66+ }
67+
68+ var status struct {
69+ Arch string `json:"arch"`
70+ }
71+
72+ if err := json .Unmarshal (output , & status ); err != nil {
73+ return ""
74+ }
75+
76+ return NormalizeArch (status .Arch )
77+ }
78+
5079// pullImageWithProgress pulls a Docker image with progress output to stderr
51- func (b * ColimaBackend ) pullImageWithProgress (ctx context.Context , image string ) error {
52- // Check if image already exists locally
80+ // If platform is specified, it will use --platform flag
81+ func (b * ColimaBackend ) pullImageWithProgress (ctx context.Context , image , platform string ) error {
82+ // Check if image already exists locally with correct platform
5383 checkCmd := exec .CommandContext (ctx , "docker" , "image" , "inspect" , image )
5484 checkCmd .Env = append (os .Environ (), fmt .Sprintf ("DOCKER_HOST=%s" , b .dockerHost ))
5585 if err := checkCmd .Run (); err == nil {
56- return nil // Image exists
86+ // Image exists, but we should re-pull if platform is specified
87+ // to ensure we get the right architecture
88+ if platform == "" {
89+ return nil // Image exists and no specific platform requested
90+ }
5791 }
5892
59- // Image doesn't exist, pull it with progress
93+ // Image doesn't exist or we need to ensure correct platform , pull it with progress
6094 fmt .Fprintf (os .Stderr , "\n Pulling image: %s\n " , image )
95+ if platform != "" {
96+ fmt .Fprintf (os .Stderr , " Platform: %s\n " , platform )
97+ }
6198 fmt .Fprintf (os .Stderr , " This may take a few minutes for large images...\n \n " )
6299
63- pullCmd := exec .CommandContext (ctx , "docker" , "pull" , image )
100+ pullArgs := []string {"pull" }
101+ if platform != "" {
102+ pullArgs = append (pullArgs , "--platform" , platform )
103+ }
104+ pullArgs = append (pullArgs , image )
105+
106+ pullCmd := exec .CommandContext (ctx , "docker" , pullArgs ... )
64107 pullCmd .Env = append (os .Environ (), fmt .Sprintf ("DOCKER_HOST=%s" , b .dockerHost ))
65108 // Stream output to stderr so user can see progress
66109 pullCmd .Stdout = os .Stderr
@@ -315,12 +358,29 @@ func (b *ColimaBackend) Create(ctx context.Context, opts *CreateOptions) (*Envir
315358 return nil , err
316359 }
317360
361+ // Determine the platform to use
362+ // If user specified platform, use it; otherwise detect from VM
363+ platform := opts .Platform
364+ if platform == "" {
365+ // Auto-detect VM architecture
366+ vmArch := b .GetVMArch (ctx )
367+ if vmArch != "" {
368+ platform = "linux/" + vmArch
369+ fmt .Fprintf (os .Stderr , " Auto-detected VM architecture: %s\n " , platform )
370+ }
371+ }
372+
318373 // Generate container name with random suffix to avoid conflicts
319374 containerName := GenerateContainerName (opts .Name )
320375
321376 // Build docker run command
322377 args := []string {"run" , "-d" , "--name" , containerName }
323378
379+ // Add platform flag if specified
380+ if platform != "" {
381+ args = append (args , "--platform" , platform )
382+ }
383+
324384 // Add labels
325385 args = append (args , "--label" , "ggo.managed=true" )
326386 args = append (args , "--label" , fmt .Sprintf ("ggo.name=%s" , opts .Name ))
@@ -417,7 +477,7 @@ func (b *ColimaBackend) Create(ctx context.Context, opts *CreateOptions) (*Envir
417477 }
418478
419479 // Pull image first with progress visible to user
420- if err := b .pullImageWithProgress (ctx , image ); err != nil {
480+ if err := b .pullImageWithProgress (ctx , image , platform ); err != nil {
421481 return nil , fmt .Errorf ("failed to pull image: %w" , err )
422482 }
423483
0 commit comments