mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 03:53:33 +00:00
chore: internal docker package with handy PullImage and CreateContainerWithImagePull methods
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user