File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,6 +26,11 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command {
2626 Short : "Kill one or more running containers" ,
2727 Args : cli .RequiresMinArgs (1 ),
2828 RunE : func (cmd * cobra.Command , args []string ) error {
29+ for _ , name := range args {
30+ if ! cli .CheckContainerName (name ) {
31+ return fmt .Errorf ("container name %s is invalid" , name )
32+ }
33+ }
2934 opts .containers = args
3035 return runKill (dockerCli , & opts )
3136 },
Original file line number Diff line number Diff line change @@ -29,6 +29,11 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command {
2929 Short : "Remove one or more containers" ,
3030 Args : cli .RequiresMinArgs (1 ),
3131 RunE : func (cmd * cobra.Command , args []string ) error {
32+ for _ , name := range args {
33+ if ! cli .CheckContainerName (name ) {
34+ return fmt .Errorf ("container name %s is invalid" , name )
35+ }
36+ }
3237 opts .containers = args
3338 return runRm (dockerCli , & opts )
3439 },
Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
2828 Short : "Stop one or more running containers" ,
2929 Args : cli .RequiresMinArgs (1 ),
3030 RunE : func (cmd * cobra.Command , args []string ) error {
31+ for _ , name := range args {
32+ if ! cli .CheckContainerName (name ) {
33+ return fmt .Errorf ("container name %s is invalid" , name )
34+ }
35+ }
3136 opts .containers = args
3237 opts .timeChanged = cmd .Flags ().Changed ("time" )
3338 return runStop (dockerCli , & opts )
Original file line number Diff line number Diff line change 1+ package cli
2+
3+ import (
4+ "strings"
5+
6+ "github.com/docker/cli/cli/names"
7+ )
8+
9+ var (
10+ validContainerNamePattern = names .RestrictedNamePattern
11+ )
12+
13+ // CheckContainerName check container's name is valid or not
14+ func CheckContainerName (name string ) bool {
15+ if len (name ) == 0 {
16+ return false
17+ }
18+ return validContainerNamePattern .MatchString (strings .TrimPrefix (name , "/" ))
19+ }
Original file line number Diff line number Diff line change 1+ package names
2+
3+ import (
4+ "regexp"
5+ )
6+
7+ // RestrictedNameChars collects the characters allowed to represent a name, normally used to validate container and volume names.
8+ const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
9+
10+ // RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
11+ var RestrictedNamePattern = regexp .MustCompile (`^` + RestrictedNameChars + `+$` )
You can’t perform that action at this time.
0 commit comments