Browse code

Allow starting a container with an existing hostConfig which contains links

Signed-off-by: Antonio Murdaca <runcom@linux.com>

Antonio Murdaca authored on 2015/07/22 04:18:56
Showing 3 changed files
... ...
@@ -481,7 +481,6 @@ func (daemon *Daemon) NetworkApiRouter() func(w http.ResponseWriter, req *http.R
481 481
 }
482 482
 
483 483
 func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.HostConfig) error {
484
-
485 484
 	if hostConfig == nil || hostConfig.Links == nil {
486 485
 		return nil
487 486
 	}
... ...
@@ -1614,3 +1614,48 @@ func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *che
1614 1614
 	c.Assert(err, check.IsNil)
1615 1615
 	c.Assert(status, check.Equals, http.StatusCreated)
1616 1616
 }
1617
+
1618
+// #14640
1619
+func (s *DockerSuite) TestPostContainersStartWithoutLinksInHostConfig(c *check.C) {
1620
+	name := "test-host-config-links"
1621
+	dockerCmd(c, "create", "--name", name, "busybox", "top")
1622
+
1623
+	hc, err := inspectFieldJSON(name, "HostConfig")
1624
+	c.Assert(err, check.IsNil)
1625
+	config := `{"HostConfig":` + hc + `}`
1626
+
1627
+	res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
1628
+	c.Assert(err, check.IsNil)
1629
+	c.Assert(res.StatusCode, check.Equals, http.StatusNoContent)
1630
+}
1631
+
1632
+// #14640
1633
+func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfig(c *check.C) {
1634
+	name := "test-host-config-links"
1635
+	dockerCmd(c, "run", "--name", "foo", "-d", "busybox", "top")
1636
+	dockerCmd(c, "create", "--name", name, "--link", "foo:bar", "busybox", "top")
1637
+
1638
+	hc, err := inspectFieldJSON(name, "HostConfig")
1639
+	c.Assert(err, check.IsNil)
1640
+	config := `{"HostConfig":` + hc + `}`
1641
+
1642
+	res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
1643
+	c.Assert(err, check.IsNil)
1644
+	c.Assert(res.StatusCode, check.Equals, http.StatusNoContent)
1645
+}
1646
+
1647
+// #14640
1648
+func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfigIdLinked(c *check.C) {
1649
+	name := "test-host-config-links"
1650
+	out, _ := dockerCmd(c, "run", "--name", "link0", "-d", "busybox", "top")
1651
+	id := strings.TrimSpace(out)
1652
+	dockerCmd(c, "create", "--name", name, "--link", id, "busybox", "top")
1653
+
1654
+	hc, err := inspectFieldJSON(name, "HostConfig")
1655
+	c.Assert(err, check.IsNil)
1656
+	config := `{"HostConfig":` + hc + `}`
1657
+
1658
+	res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
1659
+	c.Assert(err, check.IsNil)
1660
+	c.Assert(res.StatusCode, check.Equals, http.StatusNoContent)
1661
+}
... ...
@@ -3,6 +3,7 @@ package parsers
3 3
 import (
4 4
 	"fmt"
5 5
 	"net/url"
6
+	"path"
6 7
 	"runtime"
7 8
 	"strconv"
8 9
 	"strings"
... ...
@@ -158,5 +159,12 @@ func ParseLink(val string) (string, string, error) {
158 158
 	if len(arr) == 1 {
159 159
 		return val, val, nil
160 160
 	}
161
+	// This is kept because we can actually get an HostConfig with links
162
+	// from an already created container and the format is not `foo:bar`
163
+	// but `/foo:/c1/bar`
164
+	if strings.HasPrefix(arr[0], "/") {
165
+		_, alias := path.Split(arr[1])
166
+		return arr[0][1:], alias, nil
167
+	}
161 168
 	return arr[0], arr[1], nil
162 169
 }