Skip to content

Commit

Permalink
Add compose images command
Browse files Browse the repository at this point in the history
The command prints images used by created containers in services.

Signed-off-by: Jin Dong <jindon@amazon.com>
  • Loading branch information
djdongjin committed Nov 26, 2022
1 parent 281a8a6 commit d5985db
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ It does not necessarily mean that the corresponding features are missing in cont
- [:whale: nerdctl compose logs](#whale-nerdctl-compose-logs)
- [:whale: nerdctl compose build](#whale-nerdctl-compose-build)
- [:whale: nerdctl compose down](#whale-nerdctl-compose-down)
- [:whale: nerdctl compose images](#whale-nerdctl-compose-images)
- [:whale: nerdctl compose stop](#whale-nerdctl-compose-stop)
- [:whale: nerdctl compose ps](#whale-nerdctl-compose-ps)
- [:whale: nerdctl compose pull](#whale-nerdctl-compose-pull)
Expand Down Expand Up @@ -1445,6 +1446,16 @@ Flags:

Unimplemented `docker-compose down` (V1) flags: `--rmi`, `--remove-orphans`, `--timeout`

### :whale: nerdctl compose images

List images used by created containers in services

Usage: `nerdctl compose images [OPTIONS] [SERVICE...]`

Flags:

- :whale: `-q, --quiet`: Only show numeric image IDs

### :whale: nerdctl compose stop

Stop containers in services without removing them.
Expand Down Expand Up @@ -1601,7 +1612,7 @@ Registry:
- `docker search`

Compose:
- `docker-compose create|events|exec|images|pause|port|rm|scale|start|top|unpause`
- `docker-compose create|events|exec|pause|port|rm|scale|start|top|unpause`

Others:
- `docker system df`
Expand Down
1 change: 1 addition & 0 deletions cmd/nerdctl/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func newComposeCommand() *cobra.Command {
newComposeLogsCommand(),
newComposeConfigCommand(),
newComposeBuildCommand(),
newComposeImagesCommand(),
newComposePushCommand(),
newComposePullCommand(),
newComposeDownCommand(),
Expand Down
190 changes: 190 additions & 0 deletions cmd/nerdctl/compose_images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
Copyright The containerd 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 main

import (
"context"
"fmt"
"strings"
"sync"
"text/tabwriter"

"github.com/containerd/containerd"
"github.com/containerd/containerd/pkg/progress"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/nerdctl/pkg/imgutil"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

func newComposeImagesCommand() *cobra.Command {
var composeImagesCommand = &cobra.Command{
Use: "images [flags] [SERVICE...]",
Short: "List images used by created containers in services",
RunE: composeImagesAction,
SilenceUsage: true,
SilenceErrors: true,
}
composeImagesCommand.Flags().BoolP("quiet", "q", false, "Only show numeric image IDs")

return composeImagesCommand
}

func composeImagesAction(cmd *cobra.Command, args []string) error {
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
}
defer cancel()

c, err := getComposer(cmd, client)
if err != nil {
return err
}

serviceNames, err := c.ServiceNames(args...)
if err != nil {
return err
}

containers, err := c.Containers(ctx, serviceNames...)
if err != nil {
return err
}

if quiet {
return printComposeImageIDs(ctx, containers)
}

snapshotter, err := cmd.Flags().GetString("snapshotter")
if err != nil {
return err
}
sn := client.SnapshotService(snapshotter)

return printComposeImages(ctx, cmd, containers, sn)
}

func printComposeImageIDs(ctx context.Context, containers []containerd.Container) error {
ids := []string{}
for _, c := range containers {
image, err := c.Image(ctx)
if err != nil {
return err
}
metaImage := image.Metadata()
id := metaImage.Target.Digest.String()
if !strutil.InStringSlice(ids, id) {
ids = append(ids, id)
}
}

for _, id := range ids {
// always truncate image ids.
fmt.Println(strings.Split(id, ":")[1][:12])
}
return nil
}

func printComposeImages(ctx context.Context, cmd *cobra.Command, containers []containerd.Container, sn snapshots.Snapshotter) error {
type composeImagePrintable struct {
ContainerName string
Repository string
Tag string
ImageID string
Size string
}

var (
imagePrintables = []composeImagePrintable{}
mu sync.Mutex
)

eg, ctx := errgroup.WithContext(ctx)
for _, c := range containers {
c := c
eg.Go(func() error {
info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata)
if err != nil {
return err
}
containerName := info.Labels[labels.Name]

image, err := c.Image(ctx)
if err != nil {
return err
}

size, err := unpackedImageSize(ctx, sn, image)
if err != nil {
return err
}

metaImage := image.Metadata()
repository, tag := imgutil.ParseRepoTag(metaImage.Name)
imageID := metaImage.Target.Digest.String()
if repository == "" {
repository = "<none>"
}
if tag == "" {
tag = "<none>"
}
imageID = strings.Split(imageID, ":")[1][:12]

printable := composeImagePrintable{
ContainerName: containerName,
Repository: repository,
Tag: tag,
ImageID: imageID,
Size: progress.Bytes(size).String(),
}

mu.Lock()
defer mu.Unlock()
imagePrintables = append(imagePrintables, printable)

return nil
})
}

if err := eg.Wait(); err != nil {
return err
}

w := tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "Container\tRepository\tTag\tImage Id\tSize")
for _, p := range imagePrintables {
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
p.ContainerName,
p.Repository,
p.Tag,
p.ImageID,
p.Size,
); err != nil {
return err
}
}

return w.Flush()
}
86 changes: 86 additions & 0 deletions cmd/nerdctl/compose_images_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright The containerd 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 main

import (
"fmt"
"strings"
"testing"

"github.com/containerd/nerdctl/pkg/testutil"
)

func TestComposeImages(t *testing.T) {
base := testutil.NewBase(t)
var dockerComposeYAML = fmt.Sprintf(`
version: '3.1'
services:
wordpress:
image: %s
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- wordpress:/var/www/html
db:
image: %s
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
volumes:
wordpress:
db:
`, testutil.WordpressImage, testutil.MariaDBImage)

comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()
projectName := comp.ProjectName()
t.Logf("projectName=%q", projectName)

base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()

ImageAssertHandler := func(svc string, image string, exist bool) func(stdout string) error {
return func(stdout string) error {
if strings.Contains(stdout, image) != exist {
return fmt.Errorf("image %s from service %s: expect in output (%t), actual (%t)", image, svc, exist, !exist)
}
return nil
}
}

wordpressImageName := strings.Split(testutil.WordpressImage, ":")[0]
dbImageName := strings.Split(testutil.MariaDBImage, ":")[0]

// check one service image
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutWithFunc(ImageAssertHandler("db", dbImageName, true))
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutWithFunc(ImageAssertHandler("wordpress", wordpressImageName, false))

// check all service images
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutWithFunc(ImageAssertHandler("db", dbImageName, true))
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutWithFunc(ImageAssertHandler("wordpress", wordpressImageName, true))
}

0 comments on commit d5985db

Please sign in to comment.