From 65121e5fce3851d7d1e8c8cd5d77dd3c9591d773 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Tue, 21 Jul 2015 21:18:56 +0200 Subject: [PATCH] Allow starting a container with an existing hostConfig which contains links Signed-off-by: Antonio Murdaca --- daemon/daemon_unix.go | 1 - integration-cli/docker_api_containers_test.go | 45 +++++++++++++++++++ pkg/parsers/parsers.go | 8 ++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/daemon/daemon_unix.go b/daemon/daemon_unix.go index 49ee833215..286006930b 100644 --- a/daemon/daemon_unix.go +++ b/daemon/daemon_unix.go @@ -481,7 +481,6 @@ func (daemon *Daemon) NetworkApiRouter() func(w http.ResponseWriter, req *http.R } func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.HostConfig) error { - if hostConfig == nil || hostConfig.Links == nil { return nil } diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 9236f88409..d503111734 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -1614,3 +1614,48 @@ func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *che c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusCreated) } + +// #14640 +func (s *DockerSuite) TestPostContainersStartWithoutLinksInHostConfig(c *check.C) { + name := "test-host-config-links" + dockerCmd(c, "create", "--name", name, "busybox", "top") + + hc, err := inspectFieldJSON(name, "HostConfig") + c.Assert(err, check.IsNil) + config := `{"HostConfig":` + hc + `}` + + res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json") + c.Assert(err, check.IsNil) + c.Assert(res.StatusCode, check.Equals, http.StatusNoContent) +} + +// #14640 +func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfig(c *check.C) { + name := "test-host-config-links" + dockerCmd(c, "run", "--name", "foo", "-d", "busybox", "top") + dockerCmd(c, "create", "--name", name, "--link", "foo:bar", "busybox", "top") + + hc, err := inspectFieldJSON(name, "HostConfig") + c.Assert(err, check.IsNil) + config := `{"HostConfig":` + hc + `}` + + res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json") + c.Assert(err, check.IsNil) + c.Assert(res.StatusCode, check.Equals, http.StatusNoContent) +} + +// #14640 +func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfigIdLinked(c *check.C) { + name := "test-host-config-links" + out, _ := dockerCmd(c, "run", "--name", "link0", "-d", "busybox", "top") + id := strings.TrimSpace(out) + dockerCmd(c, "create", "--name", name, "--link", id, "busybox", "top") + + hc, err := inspectFieldJSON(name, "HostConfig") + c.Assert(err, check.IsNil) + config := `{"HostConfig":` + hc + `}` + + res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json") + c.Assert(err, check.IsNil) + c.Assert(res.StatusCode, check.Equals, http.StatusNoContent) +} diff --git a/pkg/parsers/parsers.go b/pkg/parsers/parsers.go index e6c8774f50..ef7f942e52 100644 --- a/pkg/parsers/parsers.go +++ b/pkg/parsers/parsers.go @@ -3,6 +3,7 @@ package parsers import ( "fmt" "net/url" + "path" "runtime" "strconv" "strings" @@ -158,5 +159,12 @@ func ParseLink(val string) (string, string, error) { if len(arr) == 1 { return val, val, nil } + // This is kept because we can actually get an HostConfig with links + // from an already created container and the format is not `foo:bar` + // but `/foo:/c1/bar` + if strings.HasPrefix(arr[0], "/") { + _, alias := path.Split(arr[1]) + return arr[0][1:], alias, nil + } return arr[0], arr[1], nil }