Allow starting a container with an existing hostConfig which contains links

Signed-off-by: Antonio Murdaca <runcom@linux.com>
This commit is contained in:
Antonio Murdaca 2015-07-21 21:18:56 +02:00
parent 3ee15acaad
commit 65121e5fce
3 changed files with 53 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}