diff --git a/api/server/router/container/backend.go b/api/server/router/container/backend.go index 444260af9f..0f6555f134 100644 --- a/api/server/router/container/backend.go +++ b/api/server/router/container/backend.go @@ -32,17 +32,17 @@ type copyBackend interface { // stateBackend includes functions to implement to provide container state lifecycle functionality. type stateBackend interface { - ContainerCreate(config types.ContainerCreateConfig, validateHostname bool) (types.ContainerCreateResponse, error) + ContainerCreate(config types.ContainerCreateConfig) (types.ContainerCreateResponse, error) ContainerKill(name string, sig uint64) error ContainerPause(name string) error ContainerRename(oldName, newName string) error ContainerResize(name string, height, width int) error ContainerRestart(name string, seconds int) error ContainerRm(name string, config *types.ContainerRmConfig) error - ContainerStart(name string, hostConfig *container.HostConfig, validateHostname bool) error + ContainerStart(name string, hostConfig *container.HostConfig) error ContainerStop(name string, seconds int) error ContainerUnpause(name string) error - ContainerUpdate(name string, hostConfig *container.HostConfig, validateHostname bool) ([]string, error) + ContainerUpdate(name string, hostConfig *container.HostConfig) ([]string, error) ContainerWait(name string, timeout time.Duration) (int, error) } diff --git a/api/server/router/container/container_routes.go b/api/server/router/container/container_routes.go index 977ce2522d..dba0d2e381 100644 --- a/api/server/router/container/container_routes.go +++ b/api/server/router/container/container_routes.go @@ -151,8 +151,7 @@ func (s *containerRouter) postContainersStart(ctx context.Context, w http.Respon hostConfig = c } - validateHostname := versions.GreaterThanOrEqualTo(version, "1.24") - if err := s.backend.ContainerStart(vars["name"], hostConfig, validateHostname); err != nil { + if err := s.backend.ContainerStart(vars["name"], hostConfig); err != nil { return err } w.WriteHeader(http.StatusNoContent) @@ -312,7 +311,6 @@ func (s *containerRouter) postContainerUpdate(ctx context.Context, w http.Respon return err } - version := httputils.VersionFromContext(ctx) var updateConfig container.UpdateConfig decoder := json.NewDecoder(r.Body) @@ -326,8 +324,7 @@ func (s *containerRouter) postContainerUpdate(ctx context.Context, w http.Respon } name := vars["name"] - validateHostname := versions.GreaterThanOrEqualTo(version, "1.24") - warnings, err := s.backend.ContainerUpdate(name, hostConfig, validateHostname) + warnings, err := s.backend.ContainerUpdate(name, hostConfig) if err != nil { return err } @@ -354,14 +351,13 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo version := httputils.VersionFromContext(ctx) adjustCPUShares := versions.LessThan(version, "1.19") - validateHostname := versions.GreaterThanOrEqualTo(version, "1.24") ccr, err := s.backend.ContainerCreate(types.ContainerCreateConfig{ Name: name, Config: config, HostConfig: hostConfig, NetworkingConfig: networkingConfig, AdjustCPUShares: adjustCPUShares, - }, validateHostname) + }) if err != nil { return err } diff --git a/builder/builder.go b/builder/builder.go index 125e56ab22..dc68be86cb 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -116,7 +116,7 @@ type Backend interface { // ContainerAttachRaw attaches to container. ContainerAttachRaw(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error // ContainerCreate creates a new Docker container and returns potential warnings - ContainerCreate(config types.ContainerCreateConfig, validateHostname bool) (types.ContainerCreateResponse, error) + ContainerCreate(config types.ContainerCreateConfig) (types.ContainerCreateResponse, error) // ContainerRm removes a container specified by `id`. ContainerRm(name string, config *types.ContainerRmConfig) error // Commit creates a new Docker image from an existing Docker container. @@ -124,7 +124,7 @@ type Backend interface { // ContainerKill stops the container execution abruptly. ContainerKill(containerID string, sig uint64) error // ContainerStart starts a new container - ContainerStart(containerID string, hostConfig *container.HostConfig, validateHostname bool) error + ContainerStart(containerID string, hostConfig *container.HostConfig) error // ContainerWait stops processing until the given container is stopped. ContainerWait(containerID string, timeout time.Duration) (int, error) // ContainerUpdateCmdOnBuild updates container.Path and container.Args diff --git a/builder/dockerfile/internals.go b/builder/dockerfile/internals.go index 2dc3fe6bd1..27e290d40f 100644 --- a/builder/dockerfile/internals.go +++ b/builder/dockerfile/internals.go @@ -181,7 +181,7 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalD return nil } - container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: b.runConfig}, true) + container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: b.runConfig}) if err != nil { return err } @@ -508,7 +508,7 @@ func (b *Builder) create() (string, error) { c, err := b.docker.ContainerCreate(types.ContainerCreateConfig{ Config: b.runConfig, HostConfig: hostConfig, - }, true) + }) if err != nil { return "", err } @@ -552,7 +552,7 @@ func (b *Builder) run(cID string) (err error) { } }() - if err := b.docker.ContainerStart(cID, nil, true); err != nil { + if err := b.docker.ContainerStart(cID, nil); err != nil { return err } diff --git a/daemon/cluster/executor/backend.go b/daemon/cluster/executor/backend.go index 9fa8ef70b1..0dbab7d9e1 100644 --- a/daemon/cluster/executor/backend.go +++ b/daemon/cluster/executor/backend.go @@ -23,8 +23,8 @@ type Backend interface { FindNetwork(idName string) (libnetwork.Network, error) SetupIngress(req clustertypes.NetworkCreateRequest, nodeIP string) error PullImage(ctx context.Context, image, tag string, metaHeaders map[string][]string, authConfig *types.AuthConfig, outStream io.Writer) error - CreateManagedContainer(config types.ContainerCreateConfig, validateHostname bool) (types.ContainerCreateResponse, error) - ContainerStart(name string, hostConfig *container.HostConfig, validateHostname bool) error + CreateManagedContainer(config types.ContainerCreateConfig) (types.ContainerCreateResponse, error) + ContainerStart(name string, hostConfig *container.HostConfig) error ContainerStop(name string, seconds int) error ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error diff --git a/daemon/cluster/executor/container/adapter.go b/daemon/cluster/executor/container/adapter.go index 38ff63afc2..f0c509e226 100644 --- a/daemon/cluster/executor/container/adapter.go +++ b/daemon/cluster/executor/container/adapter.go @@ -10,11 +10,9 @@ import ( "time" "github.com/Sirupsen/logrus" - "github.com/docker/docker/api/server/httputils" executorpkg "github.com/docker/docker/daemon/cluster/executor" "github.com/docker/engine-api/types" "github.com/docker/engine-api/types/events" - "github.com/docker/engine-api/types/versions" "github.com/docker/libnetwork" "github.com/docker/swarmkit/api" "github.com/docker/swarmkit/log" @@ -119,8 +117,6 @@ func (c *containerAdapter) removeNetworks(ctx context.Context) error { func (c *containerAdapter) create(ctx context.Context, backend executorpkg.Backend) error { var cr types.ContainerCreateResponse var err error - version := httputils.VersionFromContext(ctx) - validateHostname := versions.GreaterThanOrEqualTo(version, "1.24") if cr, err = backend.CreateManagedContainer(types.ContainerCreateConfig{ Name: c.container.name(), @@ -128,7 +124,7 @@ func (c *containerAdapter) create(ctx context.Context, backend executorpkg.Backe HostConfig: c.container.hostConfig(), // Use the first network in container create NetworkingConfig: c.container.createNetworkingConfig(), - }, validateHostname); err != nil { + }); err != nil { return err } @@ -152,9 +148,7 @@ func (c *containerAdapter) create(ctx context.Context, backend executorpkg.Backe } func (c *containerAdapter) start(ctx context.Context) error { - version := httputils.VersionFromContext(ctx) - validateHostname := versions.GreaterThanOrEqualTo(version, "1.24") - return c.backend.ContainerStart(c.container.name(), nil, validateHostname) + return c.backend.ContainerStart(c.container.name(), nil) } func (c *containerAdapter) inspect(ctx context.Context) (types.ContainerJSON, error) { diff --git a/daemon/container.go b/daemon/container.go index b9f63dedde..21355ba817 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -3,7 +3,6 @@ package daemon import ( "fmt" "path/filepath" - "regexp" "time" "github.com/docker/docker/container" @@ -203,7 +202,7 @@ func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig * // verifyContainerSettings performs validation of the hostconfig and config // structures. -func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool, validateHostname bool) ([]string, error) { +func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) { // First perform verification of settings common across all platforms. if config != nil { @@ -220,18 +219,6 @@ func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostCon return nil, err } } - - // Validate if the given hostname is RFC 1123 (https://tools.ietf.org/html/rfc1123) compliant. - if validateHostname && len(config.Hostname) > 0 { - // RFC1123 specifies that 63 bytes is the maximium length - // Windows has the limitation of 63 bytes in length - // Linux hostname is limited to HOST_NAME_MAX=64, not including the terminating null byte. - // We limit the length to 63 bytes here to match RFC1035 and RFC1123. - matched, _ := regexp.MatchString("^(([[:alnum:]]|[[:alnum:]][[:alnum:]\\-]*[[:alnum:]])\\.)*([[:alnum:]]|[[:alnum:]][[:alnum:]\\-]*[[:alnum:]])$", config.Hostname) - if len(config.Hostname) > 63 || !matched { - return nil, fmt.Errorf("invalid hostname format: %s", config.Hostname) - } - } } if hostConfig == nil { diff --git a/daemon/create.go b/daemon/create.go index 13424f4755..48e7245916 100644 --- a/daemon/create.go +++ b/daemon/create.go @@ -20,21 +20,21 @@ import ( ) // CreateManagedContainer creates a container that is managed by a Service -func (daemon *Daemon) CreateManagedContainer(params types.ContainerCreateConfig, validateHostname bool) (types.ContainerCreateResponse, error) { - return daemon.containerCreate(params, true, validateHostname) +func (daemon *Daemon) CreateManagedContainer(params types.ContainerCreateConfig) (types.ContainerCreateResponse, error) { + return daemon.containerCreate(params, true) } // ContainerCreate creates a regular container -func (daemon *Daemon) ContainerCreate(params types.ContainerCreateConfig, validateHostname bool) (types.ContainerCreateResponse, error) { - return daemon.containerCreate(params, false, validateHostname) +func (daemon *Daemon) ContainerCreate(params types.ContainerCreateConfig) (types.ContainerCreateResponse, error) { + return daemon.containerCreate(params, false) } -func (daemon *Daemon) containerCreate(params types.ContainerCreateConfig, managed bool, validateHostname bool) (types.ContainerCreateResponse, error) { +func (daemon *Daemon) containerCreate(params types.ContainerCreateConfig, managed bool) (types.ContainerCreateResponse, error) { if params.Config == nil { return types.ContainerCreateResponse{}, fmt.Errorf("Config cannot be empty in order to create a container") } - warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config, false, validateHostname) + warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config, false) if err != nil { return types.ContainerCreateResponse{Warnings: warnings}, err } diff --git a/daemon/start.go b/daemon/start.go index fcf24c59f6..9af41f8915 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -18,7 +18,7 @@ import ( ) // ContainerStart starts a container. -func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig, validateHostname bool) error { +func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig) error { container, err := daemon.GetContainer(name) if err != nil { return err @@ -68,7 +68,7 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.Hos // check if hostConfig is in line with the current system settings. // It may happen cgroups are umounted or the like. - if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false, validateHostname); err != nil { + if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false); err != nil { return err } // Adapt for old containers in case we have updates in this function and diff --git a/daemon/update.go b/daemon/update.go index 0a5e76d1cd..05a41b2e95 100644 --- a/daemon/update.go +++ b/daemon/update.go @@ -7,10 +7,10 @@ import ( ) // ContainerUpdate updates configuration of the container -func (daemon *Daemon) ContainerUpdate(name string, hostConfig *container.HostConfig, validateHostname bool) ([]string, error) { +func (daemon *Daemon) ContainerUpdate(name string, hostConfig *container.HostConfig) ([]string, error) { var warnings []string - warnings, err := daemon.verifyContainerSettings(hostConfig, nil, true, validateHostname) + warnings, err := daemon.verifyContainerSettings(hostConfig, nil, true) if err != nil { return warnings, err } diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index ef2c98c6a8..21f1faddbb 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -4416,42 +4416,6 @@ func (s *DockerSuite) TestRunVolumeCopyFlag(c *check.C) { c.Assert(err, checker.NotNil, check.Commentf(out)) } -func (s *DockerSuite) TestRunTooLongHostname(c *check.C) { - // Test case in #21445 - hostname1 := "this-is-a-way-too-long-hostname-but-it-should-give-a-nice-error.local" - out, _, err := dockerCmdWithError("run", "--hostname", hostname1, "busybox", "echo", "test") - c.Assert(err, checker.NotNil, check.Commentf("Expected docker run to fail!")) - c.Assert(out, checker.Contains, "invalid hostname format:", check.Commentf("Expected to have 'invalid hostname format:' in the output, get: %s!", out)) - - // Additional test cases - validHostnames := map[string]string{ - "hostname": "hostname", - "host-name": "host-name", - "hostname123": "hostname123", - "123hostname": "123hostname", - "hostname-of-63-bytes-long-should-be-valid-and-without-any-error": "hostname-of-63-bytes-long-should-be-valid-and-without-any-error", - } - for hostname := range validHostnames { - dockerCmd(c, "run", "--hostname", hostname, "busybox", "echo", "test") - } - - invalidHostnames := map[string]string{ - "^hostname": "invalid hostname format: ^hostname", - "hostname%": "invalid hostname format: hostname%", - "host&name": "invalid hostname format: host&name", - "-hostname": "invalid hostname format: -hostname", - "host_name": "invalid hostname format: host_name", - "hostname-of-64-bytes-long-should-be-invalid-and-be-with-an-error": "invalid hostname format: hostname-of-64-bytes-long-should-be-invalid-and-be-with-an-error", - } - - for hostname, expectedError := range invalidHostnames { - out, _, err = dockerCmdWithError("run", "--hostname", hostname, "busybox", "echo", "test") - c.Assert(err, checker.NotNil, check.Commentf("Expected docker run to fail!")) - c.Assert(out, checker.Contains, expectedError, check.Commentf("Expected to have '%s' in the output, get: %s!", expectedError, out)) - - } -} - // Test case for #21976 func (s *DockerSuite) TestRunDnsInHostMode(c *check.C) { testRequires(c, DaemonIsLinux, NotUserNamespace)