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
39 changes: 31 additions & 8 deletions cmd/compose/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

"github.com/containerd/platforms"
"github.com/docker/cli/cli/command"
cliformatter "github.com/docker/cli/cli/command/formatter"
cliflags "github.com/docker/cli/cli/flags"
"github.com/docker/go-units"
"github.com/moby/moby/client/pkg/stringid"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -54,7 +56,7 @@ func imagesCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Bac
}),
ValidArgsFunction: completeServiceNames(dockerCli, p),
}
imgCmd.Flags().StringVar(&opts.Format, "format", "table", "Format the output. Values: [table | json]")
imgCmd.Flags().StringVar(&opts.Format, "format", "table", cliflags.FormatHelp)
imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
return imgCmd
}
Expand Down Expand Up @@ -92,6 +94,7 @@ func runImages(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
}
return nil
}
imageList := imageListFromImages(images)
if opts.Format == "json" {

type img struct {
Expand All @@ -105,11 +108,12 @@ func runImages(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
LastTagTime time.Time `json:"LastTagTime,omitzero"`
}
// Convert map to slice
var imageList []img
for ctr, i := range images {
var jsonImages []img
for _, image := range imageList {
i := image.Summary
lastTagTime := i.LastTagTime
imageList = append(imageList, img{
ContainerName: ctr,
jsonImages = append(jsonImages, img{
ContainerName: image.ContainerName,
ID: i.ID,
Repository: i.Repository,
Tag: i.Tag,
Expand All @@ -119,18 +123,26 @@ func runImages(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
LastTagTime: lastTagTime,
})
}
json, err := formatter.ToJSON(imageList, "", "")
json, err := formatter.ToJSON(jsonImages, "", "")
if err != nil {
return err
}
_, err = fmt.Fprintln(dockerCli.Out(), json)
return err
}
if !formatter.IsStandardFormat(opts.Format) {
imageCtx := cliformatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewImageFormat(opts.Format),
}
return formatter.ImageWrite(imageCtx, imageList)
}

return formatter.Print(images, opts.Format, dockerCli.Out(),
func(w io.Writer) {
for _, container := range slices.Sorted(maps.Keys(images)) {
img := images[container]
for _, image := range imageList {
container := image.ContainerName
img := image.Summary
id := stringid.TruncateID(img.ID)
size := units.HumanSizeWithPrecision(float64(img.Size), 3)
repo := img.Repository
Expand All @@ -151,3 +163,14 @@ func runImages(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
},
"CONTAINER", "REPOSITORY", "TAG", "PLATFORM", "IMAGE ID", "SIZE", "CREATED")
}

func imageListFromImages(images map[string]api.ImageSummary) []formatter.Image {
imageList := make([]formatter.Image, 0, len(images))
for _, container := range slices.Sorted(maps.Keys(images)) {
imageList = append(imageList, formatter.Image{
ContainerName: container,
Summary: images[container],
})
}
return imageList
}
17 changes: 11 additions & 6 deletions cmd/compose/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"

"github.com/docker/cli/cli/command"
cliformatter "github.com/docker/cli/cli/command/formatter"
cliflags "github.com/docker/cli/cli/flags"
"github.com/docker/cli/opts"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,7 +54,7 @@ func listCommand(dockerCli command.Cli, backendOptions *BackendOptions) *cobra.C
Args: cobra.NoArgs,
ValidArgsFunction: noCompletion(),
}
lsCmd.Flags().StringVar(&lsOpts.Format, "format", "table", "Format the output. Values: [table | json]")
lsCmd.Flags().StringVar(&lsOpts.Format, "format", "table", cliflags.FormatHelp)
lsCmd.Flags().BoolVarP(&lsOpts.Quiet, "quiet", "q", false, "Only display project names")
lsCmd.Flags().Var(&lsOpts.Filter, "filter", "Filter output based on conditions provided")
lsCmd.Flags().BoolVarP(&lsOpts.All, "all", "a", false, "Show all stopped Compose projects")
Expand Down Expand Up @@ -119,18 +121,21 @@ func runList(ctx context.Context, dockerCli command.Cli, backendOptions *Backend
}

view := viewFromStackList(stackList)
if !formatter.IsStandardFormat(lsOpts.Format) {
projectCtx := cliformatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewProjectFormat(lsOpts.Format),
}
return formatter.ProjectWrite(projectCtx, view)
}
return formatter.Print(view, lsOpts.Format, dockerCli.Out(), func(w io.Writer) {
for _, stack := range view {
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", stack.Name, stack.Status, stack.ConfigFiles)
}
}, "NAME", "STATUS", "CONFIG FILES")
}

type stackView struct {
Name string
Status string
ConfigFiles string
}
type stackView = formatter.Project

func viewFromStackList(stackList []api.Stack) []stackView {
retList := make([]stackView, len(stackList))
Expand Down
119 changes: 119 additions & 0 deletions cmd/formatter/compose_format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2026 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package formatter

import (
"bytes"
"encoding/json"
"strings"
"testing"
"time"

"github.com/containerd/platforms"
cliformatter "github.com/docker/cli/cli/command/formatter"
"gotest.tools/v3/assert"

"github.com/docker/compose/v5/pkg/api"
)

func TestProjectWriteCustomTemplate(t *testing.T) {
projects := []Project{
{Name: "alpha", Status: "running(1)", ConfigFiles: "compose.yaml"},
{Name: "beta", Status: "exited(1)", ConfigFiles: "compose.yaml"},
}

var out bytes.Buffer
err := ProjectWrite(cliformatter.Context{
Output: &out,
Format: NewProjectFormat("{{.Name}}"),
}, projects)

assert.NilError(t, err)
assert.Equal(t, out.String(), "alpha\nbeta\n")
}

func TestProjectWriteJSONTemplate(t *testing.T) {
projects := []Project{{Name: "alpha", Status: "running(1)", ConfigFiles: "compose.yaml"}}

var out bytes.Buffer
err := ProjectWrite(cliformatter.Context{
Output: &out,
Format: NewProjectFormat("{{json .}}"),
}, projects)

assert.NilError(t, err)
var rendered map[string]any
assert.NilError(t, json.Unmarshal([]byte(strings.TrimSpace(out.String())), &rendered))
assert.Equal(t, rendered["Name"], "alpha")
assert.Equal(t, rendered["Status"], "running(1)")
assert.Equal(t, rendered["ConfigFiles"], "compose.yaml")
}

func TestImageWriteCustomTemplate(t *testing.T) {
created := time.Date(2026, 7, 19, 12, 0, 0, 0, time.UTC)
images := []Image{
{
ContainerName: "web-1",
Summary: api.ImageSummary{
ID: "sha256:1234567890abcdef",
Repository: "example/web",
Tag: "latest",
Platform: platforms.MustParse("linux/amd64"),
Size: 239840,
Created: &created,
},
},
}

var out bytes.Buffer
err := ImageWrite(cliformatter.Context{
Output: &out,
Format: NewImageFormat("{{.ContainerName}} {{.Repository}}:{{.Tag}} {{.Platform}}"),
}, images)

assert.NilError(t, err)
assert.Equal(t, out.String(), "web-1 example/web:latest linux/amd64\n")
}

func TestImageWriteJSONTemplate(t *testing.T) {
images := []Image{
{
ContainerName: "web-1",
Summary: api.ImageSummary{
ID: "sha256:1234567890abcdef",
Repository: "example/web",
Tag: "latest",
Platform: platforms.MustParse("linux/amd64"),
Size: 239840,
},
},
}

var out bytes.Buffer
err := ImageWrite(cliformatter.Context{
Output: &out,
Format: NewImageFormat("{{json .}}"),
}, images)

assert.NilError(t, err)
var rendered map[string]any
assert.NilError(t, json.Unmarshal([]byte(strings.TrimSpace(out.String())), &rendered))
assert.Equal(t, rendered["ContainerName"], "web-1")
assert.Equal(t, rendered["Repository"], "example/web")
assert.Equal(t, rendered["Tag"], "latest")
assert.Equal(t, rendered["Platform"], "linux/amd64")
}
29 changes: 29 additions & 0 deletions cmd/formatter/formats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2026 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package formatter

import "strings"

// IsStandardFormat returns true for Compose's existing non-template formats.
func IsStandardFormat(format string) bool {
switch strings.ToLower(format) {
case "", TABLE, PRETTY, JSON, TemplateLegacyJSON:
return true
default:
return false
}
}
Loading