chore: internal docker package with handy PullImage and CreateContainerWithImagePull methods

This commit is contained in:
Pasha Sviderski
2025-09-26 21:24:56 +10:00
parent 20cf98d42e
commit 27bbbe7186
4 changed files with 152 additions and 35 deletions
+44
View File
@@ -0,0 +1,44 @@
package docker
import (
"context"
"fmt"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
)
// CreateContainerWithImagePull creates a Docker container. If the image is missing, it pulls the image first.
func (cli *Client) CreateContainerWithImagePull(
ctx context.Context, name string, config *container.Config, hostConfig *container.HostConfig,
) (container.CreateResponse, error) {
var resp container.CreateResponse
_, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, name)
if err == nil {
return resp, nil
}
if !client.IsErrNotFound(err) {
return resp, fmt.Errorf("create container: %w", err)
}
pullCh, err := cli.PullImage(ctx, config.Image, image.PullOptions{})
if err != nil {
return resp, fmt.Errorf("pull image: %w", err)
}
for msg := range pullCh {
if msg.Err != nil {
return resp, fmt.Errorf("pull image: %w", msg.Err)
}
}
// Create container again after image pull.
if resp, err = cli.ContainerCreate(ctx, config, hostConfig, nil, nil, name); err != nil {
return resp, fmt.Errorf("create container: %w", err)
}
return resp, nil
}