@@ -27,19 +27,21 @@ import (
2727 "fmt"
2828 "io"
2929
30+ "github.com/containerd/platforms"
3031 "github.com/docker/docker/api/types"
3132 "github.com/docker/docker/api/types/container"
3233 "github.com/docker/docker/api/types/filters"
3334 dockerimage "github.com/docker/docker/api/types/image"
3435 "github.com/docker/docker/client"
3536 l "github.com/k3d-io/k3d/v5/pkg/logger"
3637 k3d "github.com/k3d-io/k3d/v5/pkg/types"
38+ ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1"
3739 "github.com/sirupsen/logrus"
3840)
3941
4042// createContainer creates a new docker container from translated specs
4143func createContainer (ctx context.Context , dockerNode * NodeInDocker , name string ) (string , error ) {
42- l .Log ().Tracef ("Creating docker container with translated config\n %+v\n " , dockerNode )
44+ l .Log ().Tracef ("Creating docker container with translated config: %s \n %+v\n " , name , dockerNode )
4345
4446 // initialize docker client
4547 docker , err := GetDockerClient ()
@@ -51,10 +53,10 @@ func createContainer(ctx context.Context, dockerNode *NodeInDocker, name string)
5153 // create container
5254 var resp container.CreateResponse
5355 for {
54- resp , err = docker .ContainerCreate (ctx , & dockerNode .ContainerConfig , & dockerNode .HostConfig , & dockerNode .NetworkingConfig , nil , name )
56+ resp , err = docker .ContainerCreate (ctx , & dockerNode .ContainerConfig , & dockerNode .HostConfig , & dockerNode .NetworkingConfig , dockerNode . PlatformConfig , name )
5557 if err != nil {
5658 if client .IsErrNotFound (err ) {
57- if err := pullImage (ctx , docker , dockerNode .ContainerConfig .Image ); err != nil {
59+ if err := pullImage (ctx , docker , dockerNode .ContainerConfig .Image , dockerNode . PlatformConfig ); err != nil {
5860 return "" , fmt .Errorf ("docker failed to pull image '%s': %w" , dockerNode .ContainerConfig .Image , err )
5961 }
6062 continue
@@ -105,8 +107,15 @@ func removeContainer(ctx context.Context, ID string) error {
105107}
106108
107109// pullImage pulls a container image and outputs progress if --verbose flag is set
108- func pullImage (ctx context.Context , docker client.APIClient , image string ) error {
109- resp , err := docker .ImagePull (ctx , image , dockerimage.PullOptions {})
110+ func pullImage (ctx context.Context , docker client.APIClient , image string , platform * ocispecv1.Platform ) error {
111+ opts := dockerimage.PullOptions {}
112+
113+ if platform != nil {
114+ // if a platform is specified, use it to pull the image
115+ opts .Platform = platforms .Format (* platform )
116+ }
117+
118+ resp , err := docker .ImagePull (ctx , image , opts )
110119 if err != nil {
111120 return fmt .Errorf ("docker failed to pull the image '%s': %w" , image , err )
112121 }
@@ -168,7 +177,7 @@ func getNodeContainer(ctx context.Context, node *k3d.Node) (*types.Container, er
168177
169178// executes an arbitrary command in a container while returning its exit code.
170179// useful to check something in docker env
171- func executeCheckInContainer (ctx context.Context , image string , cmd []string ) (int64 , error ) {
180+ func executeCheckInContainer (ctx context.Context , image string , platform * ocispecv1. Platform , cmd []string ) (int64 , error ) {
172181 docker , err := GetDockerClient ()
173182 if err != nil {
174183 return - 1 , fmt .Errorf ("failed to create docker client: %w" , err )
@@ -186,7 +195,7 @@ func executeCheckInContainer(ctx context.Context, image string, cmd []string) (i
186195 }, nil , nil , nil , "" )
187196 if err != nil {
188197 if client .IsErrNotFound (err ) {
189- if err := pullImage (ctx , docker , image ); err != nil {
198+ if err := pullImage (ctx , docker , image , platform ); err != nil {
190199 return - 1 , fmt .Errorf ("docker failed to pull image '%s': %w" , image , err )
191200 }
192201 continue
@@ -219,11 +228,11 @@ func executeCheckInContainer(ctx context.Context, image string, cmd []string) (i
219228}
220229
221230// CheckIfDirectoryExists checks for the existence of a given path inside the docker environment
222- func CheckIfDirectoryExists (ctx context.Context , image string , dir string ) (bool , error ) {
231+ func CheckIfDirectoryExists (ctx context.Context , image string , platform * ocispecv1. Platform , dir string ) (bool , error ) {
223232 l .Log ().Tracef ("checking if dir %s exists in docker environment..." , dir )
224233 shellCmd := fmt .Sprintf ("[ -d \" %s\" ] && exit 0 || exit 1" , dir )
225234 cmd := []string {"sh" , "-c" , shellCmd }
226- exitCode , err := executeCheckInContainer (ctx , image , cmd )
235+ exitCode , err := executeCheckInContainer (ctx , image , platform , cmd )
227236 l .Log ().Tracef ("check dir container returned %d exit code" , exitCode )
228237 return exitCode == 0 , err
229238}
0 commit comments