Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions cli/command/container/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command {
Short: "Kill one or more running containers",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
for _, name := range args {
Comment thread
lifubang marked this conversation as resolved.
Outdated
if !cli.CheckContainerName(name) {
return fmt.Errorf("container name %s is invalid", name)
}
}
opts.containers = args
return runKill(dockerCli, &opts)
},
Expand Down
5 changes: 5 additions & 0 deletions cli/command/container/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command {
Short: "Remove one or more containers",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
for _, name := range args {
if !cli.CheckContainerName(name) {
return fmt.Errorf("container name %s is invalid", name)
}
}
opts.containers = args
return runRm(dockerCli, &opts)
},
Expand Down
5 changes: 5 additions & 0 deletions cli/command/container/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
Short: "Stop one or more running containers",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
for _, name := range args {
if !cli.CheckContainerName(name) {
return fmt.Errorf("container name %s is invalid", name)
}
}
opts.containers = args
opts.timeChanged = cmd.Flags().Changed("time")
return runStop(dockerCli, &opts)
Expand Down
19 changes: 19 additions & 0 deletions cli/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cli

import (
"strings"

"github.com/docker/cli/cli/names"
)

var (
validContainerNamePattern = names.RestrictedNamePattern
Comment thread
lifubang marked this conversation as resolved.
Outdated
)

// CheckContainerName check container's name is valid or not
func CheckContainerName(name string) bool {
if len(name) == 0 {
Comment thread
lifubang marked this conversation as resolved.
Outdated
return false
}
return validContainerNamePattern.MatchString(strings.TrimPrefix(name, "/"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why trimming the prefix? Is a container "/aaaa" valid? @thaJeztah WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, docker daemon said it's valid.
see https://github.com/moby/moby/blob/master/daemon/names.go#L59

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the leading slash is automatically added, and for historic reasons; a container is named /containera, and a container linked to that container (using the legacy --link option on the default network) gets an internal name /containera/containerb (or something along those lines)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's allowed to manually create a container with a slash though; they should be added automatically by the daemon where needed

}
11 changes: 11 additions & 0 deletions cli/names/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package names

import (
"regexp"
)

// RestrictedNameChars collects the characters allowed to represent a name, normally used to validate container and volume names.
const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
Comment thread
lifubang marked this conversation as resolved.
Outdated

// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
var RestrictedNamePattern = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)
Comment thread
lifubang marked this conversation as resolved.
Outdated