// Package dkr wraps the Docker Engine API with the operations the migration // tool needs: reading a full container inventory, streaming data out of a // container's mounts, and streaming image layers. package dkr import ( "context" "fmt" "github.com/docker/docker/api/types/system" "github.com/docker/docker/client" ) // Client is a connection to one Docker daemon. type Client struct { api *client.Client // Endpoint is the daemon address, shown in the UI. Endpoint string } // New connects to the daemon described by the standard DOCKER_* environment // variables, or to host when it is non-empty (e.g. unix:///var/run/docker.sock // or tcp://10.0.0.5:2375). func New(host string) (*Client, error) { opts := []client.Opt{client.FromEnv, client.WithAPIVersionNegotiation()} if host != "" { opts = append(opts, client.WithHost(host)) } api, err := client.NewClientWithOpts(opts...) if err != nil { return nil, fmt.Errorf("create docker client: %w", err) } return &Client{api: api, Endpoint: api.DaemonHost()}, nil } // API exposes the underlying SDK client for callers that need an operation // this package does not wrap. func (c *Client) API() *client.Client { return c.api } // Close releases the daemon connection. func (c *Client) Close() error { return c.api.Close() } // Info returns daemon information, and doubles as a connectivity check. func (c *Client) Info(ctx context.Context) (system.Info, error) { return c.api.Info(ctx) } // Ping verifies the daemon is reachable and returns its version string. func (c *Client) Ping(ctx context.Context) (string, error) { v, err := c.api.ServerVersion(ctx) if err != nil { return "", err } return v.Version, nil }