@@ -15,6 +15,7 @@ import (
1515 "github.com/docker/docker/api/types"
1616 "github.com/docker/docker/api/types/container"
1717 "github.com/docker/docker/api/types/filters"
18+ "github.com/docker/docker/api/types/image"
1819 "github.com/docker/docker/api/types/mount"
1920 "github.com/docker/docker/api/types/network"
2021 "github.com/docker/docker/api/types/swarm"
@@ -84,6 +85,14 @@ type ContainerRunOptions struct {
8485}
8586
8687func (d * Docker ) ContainerRun (ctx context.Context , opts ContainerRunOptions ) (string , error ) {
88+ if opts .Config != nil {
89+ if opts .Config .Image == "" {
90+ return "" , errors .New ("image must be specified in container config" )
91+ }
92+ if err := d .ensureDockerImage (ctx , opts .Config .Image ); err != nil {
93+ return "" , fmt .Errorf ("failed to ensure docker image %q: %w" , opts .Config .Image , err )
94+ }
95+ }
8796 resp , err := d .client .ContainerCreate (ctx , opts .Config , opts .Host , opts .Net , opts .Platform , opts .Name )
8897 if err != nil {
8998 return "" , fmt .Errorf ("failed to create container: %w" , err )
@@ -453,6 +462,22 @@ func (d *Docker) Shutdown() error {
453462 return nil
454463}
455464
465+ func (d * Docker ) ensureDockerImage (ctx context.Context , img string ) error {
466+ // Pull the image
467+ reader , err := d .client .ImagePull (ctx , img , image.PullOptions {})
468+ if err != nil {
469+ return fmt .Errorf ("failed to pull image %q: %w" , img , err )
470+ }
471+ defer reader .Close ()
472+
473+ // Read the output from the pull operation
474+ if _ , err := io .Copy (io .Discard , reader ); err != nil {
475+ return fmt .Errorf ("failed to read image pull output: %w" , err )
476+ }
477+
478+ return nil
479+ }
480+
456481type NetworkInfo struct {
457482 Name string
458483 ID string
0 commit comments