Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple targets for docker image tag command #5605

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
35 changes: 29 additions & 6 deletions cli/command/image/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@ package image

import (
"context"
"fmt"
"strings"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type tagOptions struct {
image string
name string
names []string
}

// NewTagCommand creates a new `docker tag` command
func NewTagCommand(dockerCli command.Cli) *cobra.Command {
var opts tagOptions

cmd := &cobra.Command{
Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]",
Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE",
Args: cli.ExactArgs(2),
Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] [TARGET_IMAGE[:TAG]...]",
Short: "Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE",
Args: cli.RequiresMinArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
opts.image = args[0]
opts.name = args[1]
opts.names = args[1:]
return runTag(cmd.Context(), dockerCli, opts)
},
Annotations: map[string]string{
Expand All @@ -40,5 +44,24 @@ func NewTagCommand(dockerCli command.Cli) *cobra.Command {
}

func runTag(ctx context.Context, dockerCli command.Cli, opts tagOptions) error {
return dockerCli.Client().ImageTag(ctx, opts.image, opts.name)
var errs []string
fatalErr := false
for _, name := range opts.names {
err := dockerCli.Client().ImageTag(ctx, opts.image, name)
if err != nil {
if !errdefs.IsNotFound(err) {
fatalErr = true
}
errs = append(errs, err.Error())
}
}

if len(errs) > 0 {
msg := strings.Join(errs, "\n")
if fatalErr {
return errors.New(msg)
}
fmt.Fprintln(dockerCli.Err(), msg)
}
return nil
}
2 changes: 1 addition & 1 deletion docs/reference/commandline/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The base command for the Docker CLI.
| [`stop`](stop.md) | Stop one or more running containers |
| [`swarm`](swarm.md) | Manage Swarm |
| [`system`](system.md) | Manage Docker |
| [`tag`](tag.md) | Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE |
| [`tag`](tag.md) | Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE |
| [`top`](top.md) | Display the running processes of a container |
| [`trust`](trust.md) | Manage trust on Docker images |
| [`unpause`](unpause.md) | Unpause all processes within one or more containers |
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commandline/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Manage images
| [`push`](image_push.md) | Upload an image to a registry |
| [`rm`](image_rm.md) | Remove one or more images |
| [`save`](image_save.md) | Save one or more images to a tar archive (streamed to STDOUT by default) |
| [`tag`](image_tag.md) | Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE |
| [`tag`](image_tag.md) | Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE |



Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commandline/image_tag.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tag

<!---MARKER_GEN_START-->
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE

### Aliases

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commandline/tag.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# docker tag

<!---MARKER_GEN_START-->
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE

### Aliases

Expand Down