Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/client"

"github.com/docker/compose/v5/internal/envresolver"
"github.com/docker/compose/v5/pkg/api"
Expand All @@ -33,9 +34,11 @@ import (

// Backend is what the build needs from the engine it acts on.
type Backend interface {
// CLI is the Docker CLI Compose runs in: the daemon client, the credentials,
// the terminal output and the buildx plugin all come from it.
// CLI is the Docker CLI Compose runs in: the credentials, the terminal output
// and the buildx plugin come from it.
CLI() command.Cli
// ImageBuildAPI builds the images through the Docker daemon.
ImageBuildAPI() client.ImageBuildAPIClient
// Events receives the progress of the builds.
Events() api.EventProcessor
// DryRun tells whether the build only reports what it would do.
Expand Down
2 changes: 1 addition & 1 deletion internal/build/classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (s *Service) doBuildImage(ctx context.Context, project *types.Project, serv
ctx, cancel := context.WithCancel(ctx)
defer cancel()
s.backend.Events().On(composeprogress.Building(imageName))
response, err := s.backend.CLI().Client().ImageBuild(ctx, body, buildOpts)
response, err := s.backend.ImageBuildAPI().ImageBuild(ctx, body, buildOpts)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/containerops/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *Service) Commit(ctx context.Context, projectName string, options api.Co
return nil
}

response, err := s.backend.APIClient().ContainerCommit(ctx, ctr.ID, client.ContainerCommitOptions{
response, err := s.backend.ContainerAPI().ContainerCommit(ctx, ctr.ID, client.ContainerCommitOptions{
Reference: options.Reference,
Comment: options.Comment,
Author: options.Author,
Expand Down
3 changes: 3 additions & 0 deletions internal/containerops/containerops.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package containerops

import (
"github.com/docker/cli/cli/command"
"github.com/moby/moby/client"

"github.com/docker/compose/v5/internal/engine"
"github.com/docker/compose/v5/pkg/api"
Expand All @@ -33,6 +34,8 @@ import (
// implementation).
type Backend interface {
engine.Backend
// ContainerAPI acts on the containers: copy, export, commit.
ContainerAPI() client.ContainerAPIClient
// Events receives the progress of the operations.
Events() api.EventProcessor
// DryRun tells whether the operations only report what they would do.
Expand Down
10 changes: 5 additions & 5 deletions internal/containerops/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *Service) copyToContainer(ctx context.Context, containerID string, srcPa
// Prepare destination copy info by stat-ing the container path.
dstInfo := archive.CopyInfo{Path: dstPath}
var dstStat container.PathStat
res, err := s.backend.APIClient().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{
res, err := s.backend.ContainerAPI().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{
Path: dstPath,
})
if err == nil {
Expand All @@ -172,7 +172,7 @@ func (s *Service) copyToContainer(ctx context.Context, containerID string, srcPa
}

dstInfo.Path = linkTarget
res, err = s.backend.APIClient().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{
res, err = s.backend.ContainerAPI().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{
Path: linkTarget,
})
if err == nil {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (s *Service) copyToContainer(ctx context.Context, containerID string, srcPa
}
}

_, err = s.backend.APIClient().CopyToContainer(ctx, containerID, client.CopyToContainerOptions{
_, err = s.backend.ContainerAPI().CopyToContainer(ctx, containerID, client.CopyToContainerOptions{
DestinationPath: resolvedDstPath,
Content: content,
AllowOverwriteDirWithFile: false,
Expand All @@ -271,7 +271,7 @@ func (s *Service) copyFromContainer(ctx context.Context, containerID, srcPath, d
var rebaseName string
if options.FollowLink {
var srcStat container.PathStat
res, err := s.backend.APIClient().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{
res, err := s.backend.ContainerAPI().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{
Path: srcPath,
})
if err == nil {
Expand All @@ -292,7 +292,7 @@ func (s *Service) copyFromContainer(ctx context.Context, containerID, srcPath, d
}
}

res, err := s.backend.APIClient().CopyFromContainer(ctx, containerID, client.CopyFromContainerOptions{
res, err := s.backend.ContainerAPI().CopyFromContainer(ctx, containerID, client.CopyFromContainerOptions{
SourcePath: srcPath,
})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/containerops/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type fakeBackend struct {
asked string
}

func (f *fakeBackend) APIClient() client.APIClient { return nil }
func (f *fakeBackend) MaxConcurrency() int { return 0 }
func (f *fakeBackend) Events() api.EventProcessor { return nil }
func (f *fakeBackend) DryRun() bool { return false }
func (f *fakeBackend) CLI() command.Cli { return nil }
func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient { return nil }
func (f *fakeBackend) MaxConcurrency() int { return 0 }
func (f *fakeBackend) Events() api.EventProcessor { return nil }
func (f *fakeBackend) DryRun() bool { return false }
func (f *fakeBackend) CLI() command.Cli { return nil }

func (f *fakeBackend) Containers(_ context.Context, _ string, oneOff engine.OneOff, all bool, services ...string) ([]container.Summary, error) {
f.asked = "Containers"
Expand Down
2 changes: 1 addition & 1 deletion internal/containerops/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *Service) Export(ctx context.Context, projectName string, options api.Ex
Status: api.Working,
})

responseBody, err := s.backend.APIClient().ContainerExport(ctx, container.ID, client.ContainerExportOptions{})
responseBody, err := s.backend.ContainerAPI().ContainerExport(ctx, container.ID, client.ContainerExportOptions{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/desktop/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *Client) IsFeatureEnabled(ctx context.Context, feature string) (bool, er
// given feature flag is enabled. Returns false silently on any failure — the
// engine being unreachable, Desktop not being the active engine, or the flag
// being off — so callers can use this as a single gating check.
func IsFeatureActive(ctx context.Context, apiClient client.APIClient, feature string) bool {
func IsFeatureActive(ctx context.Context, apiClient client.SystemAPIClient, feature string) bool {
endpoint, err := Endpoint(ctx, apiClient)
if err != nil || endpoint == "" {
return false
Expand Down
4 changes: 2 additions & 2 deletions internal/desktop/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const ddProxyHost = "docker-desktop-http-proxy.invalid"

// Endpoint returns the Docker Desktop API socket endpoint advertised via the
// engine info labels, or "" when the active engine is not Docker Desktop.
func Endpoint(ctx context.Context, apiClient client.APIClient) (string, error) {
func Endpoint(ctx context.Context, apiClient client.SystemAPIClient) (string, error) {
res, err := apiClient.Info(ctx, client.InfoOptions{})
if err != nil {
return "", err
Expand Down Expand Up @@ -177,7 +177,7 @@ func isLoopbackHost(host string) bool {
// ProxyTransportFor discovers the Docker Desktop endpoint via apiClient and
// returns the matching transport, or nil when DD is not active or discovery
// fails (so callers fall back to their own default transport).
func ProxyTransportFor(ctx context.Context, apiClient client.APIClient) http.RoundTripper {
func ProxyTransportFor(ctx context.Context, apiClient client.SystemAPIClient) http.RoundTripper {
endpoint, err := Endpoint(ctx, apiClient)
if err != nil {
logrus.Debugf("could not detect Docker Desktop endpoint, deferring to caller's default transport: %v", err)
Expand Down
14 changes: 9 additions & 5 deletions internal/devwatch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ import (
// Backend is what the development loop needs from the engine it drives.
type Backend interface {
engine.Backend
// ExecAPI runs the sync commands and hooks in the containers.
ExecAPI() client.ExecAPIClient
// ContainerAPI copies the synced files into the containers.
ContainerAPI() client.ContainerAPIClient
// CLI is the Docker CLI Compose runs in: it owns the output stream the
// sync commands write to, the config the exec hooks read, and runs them.
CLI() command.Cli
Expand Down Expand Up @@ -466,7 +470,7 @@ func (t tarDockerClient) ContainersForService(ctx context.Context, projectName s
}

func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []string, in io.Reader) error {
execCreateResp, err := t.backend.APIClient().ExecCreate(ctx, containerID, client.ExecCreateOptions{
execCreateResp, err := t.backend.ExecAPI().ExecCreate(ctx, containerID, client.ExecCreateOptions{
Cmd: cmd,
AttachStdout: false,
AttachStderr: true,
Expand All @@ -477,7 +481,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str
return err
}

conn, err := t.backend.APIClient().ExecAttach(ctx, execCreateResp.ID, client.ExecAttachOptions{
conn, err := t.backend.ExecAPI().ExecAttach(ctx, execCreateResp.ID, client.ExecAttachOptions{
TTY: false,
})
if err != nil {
Expand All @@ -500,7 +504,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str
return err
})

_, err = t.backend.APIClient().ExecStart(ctx, execCreateResp.ID, client.ExecStartOptions{
_, err = t.backend.ExecAPI().ExecStart(ctx, execCreateResp.ID, client.ExecStartOptions{
TTY: false,
Detach: false,
})
Expand All @@ -515,7 +519,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str
return err
}

execResult, err := t.backend.APIClient().ExecInspect(ctx, execCreateResp.ID, client.ExecInspectOptions{})
execResult, err := t.backend.ExecAPI().ExecInspect(ctx, execCreateResp.ID, client.ExecInspectOptions{})
if err != nil {
return err
}
Expand All @@ -529,7 +533,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str
}

func (t tarDockerClient) Untar(ctx context.Context, id string, archive io.ReadCloser) error {
_, err := t.backend.APIClient().CopyToContainer(ctx, id, client.CopyToContainerOptions{
_, err := t.backend.ContainerAPI().CopyToContainer(ctx, id, client.CopyToContainerOptions{
DestinationPath: "/",
Content: archive,
CopyUIDGID: true,
Expand Down
7 changes: 6 additions & 1 deletion internal/devwatch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ type fakeBackend struct {
removeDanglingImages func(ctx context.Context, projectName string, keep func(image.Summary) bool) error
}

func (f *fakeBackend) APIClient() client.APIClient {
func (f *fakeBackend) ExecAPI() client.ExecAPIClient {
f.t.Errorf("unexpected call to the Docker client")
return nil
}

func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient {
f.t.Errorf("unexpected call to the Docker client")
return nil
}
Expand Down
8 changes: 3 additions & 5 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"

"github.com/moby/moby/api/types/container"
"github.com/moby/moby/client"
)

// OneOff tells whether the containers of `compose run` are part of a listing.
Expand All @@ -35,11 +34,10 @@ const (
OneOffExclude
)

// Backend is what every operation needs from the engine: the Docker daemon, the
// bound on the calls made to it, and the containers of a project.
// Backend is what every operation needs from the engine: the bound on the calls
// made to it, and the containers of a project. The calls themselves come from
// the families of the engine API each operation asks for in its own Backend.
type Backend interface {
// APIClient talks to the Docker daemon.
APIClient() client.APIClient
// MaxConcurrency bounds the daemon calls made in parallel, 0 meaning no bound.
MaxConcurrency() int
// Containers lists the containers of a project, narrowed to services when given.
Expand Down
20 changes: 11 additions & 9 deletions internal/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ const (
)

// Backend is what the operations need from the engine they act on. It is
// narrow on purpose: it redeclares APIClient and MaxConcurrency of
// engine.Backend rather than embedding it, which would force every
// narrow on purpose: it redeclares MaxConcurrency of engine.Backend rather
// than embedding it, which would force every
// implementation (the fake of the tests included) to provide Containers and
// SpecifiedContainer, that these operations never call.
type Backend interface {
// APIClient talks to the Docker daemon.
APIClient() client.APIClient
// ImageAPI reads and removes the images of the Docker daemon.
ImageAPI() client.ImageAPIClient
// ContainerAPI lists the containers that use the images.
ContainerAPI() client.ContainerAPIClient
// MaxConcurrency bounds the daemon calls made in parallel, 0 meaning no bound.
MaxConcurrency() int
// RuntimeAPIVersion is the API version negotiated with the daemon.
Expand All @@ -85,7 +87,7 @@ func New(backend Backend) *Service {
// Images lists the images the containers of a project run.
func (s *Service) Images(ctx context.Context, projectName string, options api.ImagesOptions) (map[string]api.ImageSummary, error) {
projectName = strings.ToLower(projectName)
allContainers, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{
allContainers, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{
All: true,
Filters: naming.ProjectFilter(projectName),
})
Expand Down Expand Up @@ -147,7 +149,7 @@ func (s *Service) containerImageSummary(ctx context.Context, c container.Summary
}
}

img, err := s.backend.APIClient().ImageInspect(ctx, c.Image)
img, err := s.backend.ImageAPI().ImageInspect(ctx, c.Image)
if errdefs.IsNotFound(err) {
return api.ImageSummary{
ID: c.ImageID,
Expand All @@ -161,7 +163,7 @@ func (s *Service) containerImageSummary(ctx context.Context, c container.Summary
id := img.ID // platform-specific image ID can't be combined with image tag, see https://github.com/moby/moby/issues/49995

if withPlatform && c.ImageManifestDescriptor != nil && c.ImageManifestDescriptor.Platform != nil {
img, err = s.backend.APIClient().ImageInspect(ctx, c.Image, client.ImageInspectWithPlatform(c.ImageManifestDescriptor.Platform))
img, err = s.backend.ImageAPI().ImageInspect(ctx, c.Image, client.ImageInspectWithPlatform(c.ImageManifestDescriptor.Platform))
if err != nil {
return api.ImageSummary{}, err
}
Expand Down Expand Up @@ -205,7 +207,7 @@ func (s *Service) InspectLocalImages(ctx context.Context, repoTags []string) (ma
eg, ctx := concurrency.NewErrgroup(ctx, s.backend.MaxConcurrency())
for _, repoTag := range repoTags {
eg.Go(func() error {
inspect, err := s.backend.APIClient().ImageInspect(ctx, repoTag, opts...)
inspect, err := s.backend.ImageAPI().ImageInspect(ctx, repoTag, opts...)
if err != nil {
if errdefs.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -389,7 +391,7 @@ func (s *Service) InspectLocalContent(ctx context.Context, ref string, platform
if err != nil {
return "", false, err
}
inspected, err := s.backend.APIClient().ImageInspect(ctx, ref, opts...)
inspected, err := s.backend.ImageAPI().ImageInspect(ctx, ref, opts...)
if err != nil {
return "", false, err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/images/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ type fakeBackend struct {
version string
}

func (f *fakeBackend) APIClient() client.APIClient { return f.apiClient }
func (f *fakeBackend) MaxConcurrency() int { return 0 }
func (f *fakeBackend) ImageAPI() client.ImageAPIClient { return f.apiClient }
func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient { return f.apiClient }
func (f *fakeBackend) MaxConcurrency() int { return 0 }

func (f *fakeBackend) RuntimeAPIVersion(context.Context) (string, error) { return f.version, nil }

Expand Down
4 changes: 2 additions & 2 deletions internal/images/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (s *Service) RemoveImages(ctx context.Context, images []image.Summary, limi
// shared errgroup ctx, so fn always returns nil and collects into the
// buckets below instead.
_ = concurrency.ForEach(ctx, limiter, images, func(ctx context.Context, img image.Summary) error {
if _, err := s.backend.APIClient().ImageRemove(ctx, img.ID, client.ImageRemoveOptions{}); err != nil {
if _, err := s.backend.ImageAPI().ImageRemove(ctx, img.ID, client.ImageRemoveOptions{}); err != nil {
mu.Lock()
defer mu.Unlock()
imgStillInUse, alreadyGone := s.backend.ClassifyRemovalError(err)
Expand Down Expand Up @@ -206,7 +206,7 @@ func (s *Service) RemoveImages(ctx context.Context, images []image.Summary, limi
// itself, to decide whether there's anything to report) and watch --prune,
// so the two don't drift apart.
func (s *Service) EligibleDanglingImages(ctx context.Context, projectName string, keep func(image.Summary) bool) ([]image.Summary, error) {
res, err := s.backend.APIClient().ImageList(ctx, client.ImageListOptions{
res, err := s.backend.ImageAPI().ImageList(ctx, client.ImageListOptions{
Filters: naming.ProjectFilter(projectName).Add("dangling", "true"),
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

func (s *Service) Events(ctx context.Context, projectName string, options api.EventsOptions) error {
projectName = strings.ToLower(projectName)
res := s.backend.APIClient().Events(ctx, client.EventsListOptions{
res := s.backend.SystemAPI().Events(ctx, client.EventsListOptions{
Filters: naming.ProjectFilter(projectName),
Since: options.Since,
Until: options.Until,
Expand Down
8 changes: 8 additions & 0 deletions internal/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
package inspect

import (
"github.com/moby/moby/client"

"github.com/docker/compose/v5/internal/engine"
)

// Backend is what the operations need from the engine they report on.
type Backend interface {
engine.Backend
// ContainerAPI lists, inspects and reads the processes of the containers.
ContainerAPI() client.ContainerAPIClient
// VolumeAPI lists the volumes.
VolumeAPI() client.VolumeAPIClient
// SystemAPI streams the events of the daemon.
SystemAPI() client.SystemAPIClient
}

// Service runs the inspection operations on a Backend.
Expand Down
6 changes: 4 additions & 2 deletions internal/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ type fakeBackend struct {
index int
}

func (f *fakeBackend) APIClient() client.APIClient { return f.apiClient }
func (f *fakeBackend) MaxConcurrency() int { return 2 }
func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient { return f.apiClient }
func (f *fakeBackend) VolumeAPI() client.VolumeAPIClient { return f.apiClient }
func (f *fakeBackend) SystemAPI() client.SystemAPIClient { return f.apiClient }
func (f *fakeBackend) MaxConcurrency() int { return 2 }

func (f *fakeBackend) Containers(_ context.Context, _ string, oneOff engine.OneOff, all bool, services ...string) ([]container.Summary, error) {
f.oneOff, f.all, f.services = oneOff, all, services
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func (s *Service) List(ctx context.Context, options api.ListOptions) ([]api.Stack, error) {
list, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{
list, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{
Filters: make(client.Filters).Add("label", api.ProjectLabel).Add("label", api.ConfigHashLabel),
All: options.All,
})
Expand Down
Loading
Loading