chore: fix docker client CreateContainerWithImagePull, add WaitPortPublished

This commit is contained in:
Pasha Sviderski
2025-09-26 21:24:56 +10:00
parent cd25b973ea
commit bc6f2fd49d
+37 -11
View File
@@ -2,26 +2,23 @@ package docker
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"time"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/image"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
) )
// CreateContainerWithImagePull creates a Docker container. If the image is missing, it pulls the image first. // CreateContainerWithImagePull creates a container. If the image is missing, it pulls the image first.
func (cli *Client) CreateContainerWithImagePull( func (cli *Client) CreateContainerWithImagePull(
ctx context.Context, name string, config *container.Config, hostConfig *container.HostConfig, ctx context.Context, name string, config *container.Config, hostConfig *container.HostConfig,
) (container.CreateResponse, error) { ) (container.CreateResponse, error) {
var resp container.CreateResponse resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, name)
if err == nil || !client.IsErrNotFound(err) {
_, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, name) return resp, err
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{}) pullCh, err := cli.PullImage(ctx, config.Image, image.PullOptions{})
@@ -29,6 +26,8 @@ func (cli *Client) CreateContainerWithImagePull(
return resp, fmt.Errorf("pull image: %w", err) return resp, fmt.Errorf("pull image: %w", err)
} }
// Drain the pull channel until it's closed (image is fully pulled) or an error occurs.
// If the context is canceled during the pull, the channel will receive a context cancellation error.
for msg := range pullCh { for msg := range pullCh {
if msg.Err != nil { if msg.Err != nil {
return resp, fmt.Errorf("pull image: %w", msg.Err) return resp, fmt.Errorf("pull image: %w", msg.Err)
@@ -37,8 +36,35 @@ func (cli *Client) CreateContainerWithImagePull(
// Create container again after image pull. // Create container again after image pull.
if resp, err = cli.ContainerCreate(ctx, config, hostConfig, nil, nil, name); err != nil { if resp, err = cli.ContainerCreate(ctx, config, hostConfig, nil, nil, name); err != nil {
return resp, fmt.Errorf("create container: %w", err) return resp, err
} }
return resp, nil return resp, nil
} }
// WaitPortPublished waits for a container port to be published on the host which happens asynchronously.
func (cli *Client) WaitPortPublished(ctx context.Context, containerID string, port nat.Port) ([]nat.PortBinding, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
for {
c, err := cli.ContainerInspect(ctx, containerID)
if err != nil {
return nil, fmt.Errorf("inspect container: %w", err)
}
binding, ok := c.NetworkSettings.Ports[port]
if ok && len(binding) > 0 {
return binding, nil
}
select {
case <-time.After(10 * time.Millisecond):
case <-ctx.Done():
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, errors.New("timeout")
}
return nil, ctx.Err()
}
}
}