mirror of
https://github.com/docker/compose.git
synced 2026-08-28 12:23:49 +00:00
Set random container name if not set
Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
parent
578376aabe
commit
0efa67f7d2
4 changed files with 27 additions and 31 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue