diff --git a/cli/cmd/run/run.go b/cli/cmd/run/run.go index 5b448f87..6c5e864e 100644 --- a/cli/cmd/run/run.go +++ b/cli/cmd/run/run.go @@ -30,9 +30,7 @@ package run import ( "context" "fmt" - "strings" - "github.com/docker/docker/pkg/namesgenerator" "github.com/spf13/cobra" "github.com/docker/api/cli/options/run" @@ -52,7 +50,7 @@ func Command() *cobra.Command { } cmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT") - cmd.Flags().StringVar(&opts.Name, "name", getRandomName(), "Assign a name to the container") + cmd.Flags().StringVar(&opts.Name, "name", "", "Assign a name to the container") cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container") cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: user:key@my_share:/absolute/path/to/target") @@ -77,8 +75,3 @@ func runRun(ctx context.Context, image string, opts run.Opts) error { return nil } - -func getRandomName() string { - // Azure supports hyphen but not underscore in names - return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1) -} diff --git a/cli/cmd/run/run_test.go b/cli/cmd/run/run_test.go deleted file mode 100644 index 2ef4b81f..00000000 --- a/cli/cmd/run/run_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package run - -import ( - "regexp" - "testing" - - "github.com/stretchr/testify/require" -) - -var ( - // AzureNameRegex is used to validate container names - // Regex was taken from server side error: - // The container name must contain no more than 63 characters and must match the regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?' (e.g. 'my-name'). - AzureNameRegex = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])") -) - -// TestAzureRandomName ensures compliance with Azure naming requirements -func TestAzureRandomName(t *testing.T) { - n := getRandomName() - require.Less(t, len(n), 64) - require.Greater(t, len(n), 1) - require.Regexp(t, AzureNameRegex, n) -} diff --git a/cli/options/run/opts.go b/cli/options/run/opts.go index af3bd991..2e55c953 100644 --- a/cli/options/run/opts.go +++ b/cli/options/run/opts.go @@ -5,6 +5,7 @@ import ( "strconv" "strings" + "github.com/docker/docker/pkg/namesgenerator" "github.com/docker/go-connections/nat" "github.com/docker/api/containers" @@ -20,6 +21,10 @@ type Opts struct { // ToContainerConfig convert run options to a container configuration func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) { + if r.Name == "" { + r.Name = getRandomName() + } + publish, err := r.toPorts() if err != nil { return containers.ContainerConfig{}, err @@ -83,3 +88,8 @@ func toLabels(labels []string) (map[string]string, error) { return result, nil } + +func getRandomName() string { + // Azure supports hyphen but not underscore in names + return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1) +} diff --git a/cli/options/run/opts_test.go b/cli/options/run/opts_test.go index b2e4f4ee..881ff253 100644 --- a/cli/options/run/opts_test.go +++ b/cli/options/run/opts_test.go @@ -2,6 +2,7 @@ package run import ( "errors" + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -15,6 +16,21 @@ type RunOptsSuite struct { suite.Suite } +var ( + // AzureNameRegex is used to validate container names + // Regex was taken from server side error: + // The container name must contain no more than 63 characters and must match the regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?' (e.g. 'my-name'). + AzureNameRegex = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])") +) + +// TestAzureRandomName ensures compliance with Azure naming requirements +func (s *RunOptsSuite) TestAzureRandomName() { + n := getRandomName() + require.Less(s.T(), len(n), 64) + require.Greater(s.T(), len(n), 1) + require.Regexp(s.T(), AzureNameRegex, n) +} + func (s *RunOptsSuite) TestPortParse() { testCases := []struct { in string