2014-06-24 02:03:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-06-05 21:50:11 +00:00
|
|
|
"fmt"
|
2015-08-31 21:40:36 +00:00
|
|
|
"net"
|
2015-06-05 21:50:11 +00:00
|
|
|
"regexp"
|
2014-06-24 02:03:08 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-06-24 02:03:08 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestPortList(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-06-24 02:03:08 +00:00
|
|
|
// one port
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "top")
|
2015-04-06 13:21:18 +00:00
|
|
|
firstID := strings.TrimSpace(out)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err := assertPortList(c, out, []string{"0.0.0.0:9876"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "port", firstID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "rm", "-f", firstID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
|
|
|
// three port
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "-d",
|
2014-06-24 02:03:08 +00:00
|
|
|
"-p", "9876:80",
|
|
|
|
"-p", "9877:81",
|
|
|
|
"-p", "9878:82",
|
|
|
|
"busybox", "top")
|
2015-04-06 13:21:18 +00:00
|
|
|
ID := strings.TrimSpace(out)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "port", ID, "80")
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{"0.0.0.0:9876"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "port", ID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{
|
2014-06-24 02:03:08 +00:00
|
|
|
"80/tcp -> 0.0.0.0:9876",
|
|
|
|
"81/tcp -> 0.0.0.0:9877",
|
2015-10-16 06:30:44 +00:00
|
|
|
"82/tcp -> 0.0.0.0:9878"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "rm", "-f", ID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
|
|
|
// more and one port mapped to the same container port
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "-d",
|
2014-06-24 02:03:08 +00:00
|
|
|
"-p", "9876:80",
|
|
|
|
"-p", "9999:80",
|
|
|
|
"-p", "9877:81",
|
|
|
|
"-p", "9878:82",
|
|
|
|
"busybox", "top")
|
2015-04-06 13:21:18 +00:00
|
|
|
ID = strings.TrimSpace(out)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "port", ID, "80")
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "port", ID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{
|
2014-06-24 02:03:08 +00:00
|
|
|
"80/tcp -> 0.0.0.0:9876",
|
|
|
|
"80/tcp -> 0.0.0.0:9999",
|
|
|
|
"81/tcp -> 0.0.0.0:9877",
|
2015-10-16 06:30:44 +00:00
|
|
|
"82/tcp -> 0.0.0.0:9878"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "rm", "-f", ID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2015-05-01 19:35:26 +00:00
|
|
|
testRange := func() {
|
|
|
|
// host port ranges used
|
|
|
|
IDs := make([]string, 3)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
out, _ = dockerCmd(c, "run", "-d",
|
|
|
|
"-p", "9090-9092:80",
|
|
|
|
"busybox", "top")
|
|
|
|
IDs[i] = strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "port", IDs[i])
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i)})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-05-01 19:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test port range exhaustion
|
2015-10-16 06:30:44 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "-d",
|
2015-05-01 19:35:26 +00:00
|
|
|
"-p", "9090-9092:80",
|
|
|
|
"busybox", "top")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Exhausted port range did not return an error
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
2015-05-01 19:35:26 +00:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
dockerCmd(c, "rm", "-f", IDs[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testRange()
|
|
|
|
// Verify we ran re-use port ranges after they are no longer in use.
|
|
|
|
testRange()
|
|
|
|
|
|
|
|
// test invalid port ranges
|
|
|
|
for _, invalidRange := range []string{"9090-9089:80", "9090-:80", "-9090:80"} {
|
2015-10-16 06:30:44 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "-d",
|
2015-05-01 19:35:26 +00:00
|
|
|
"-p", invalidRange,
|
|
|
|
"busybox", "top")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Port range should have returned an error
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
2015-05-01 19:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test host range:container range spec.
|
|
|
|
out, _ = dockerCmd(c, "run", "-d",
|
|
|
|
"-p", "9800-9803:80-83",
|
|
|
|
"busybox", "top")
|
|
|
|
ID = strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "port", ID)
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{
|
2015-05-01 19:35:26 +00:00
|
|
|
"80/tcp -> 0.0.0.0:9800",
|
|
|
|
"81/tcp -> 0.0.0.0:9801",
|
|
|
|
"82/tcp -> 0.0.0.0:9802",
|
2015-10-16 06:30:44 +00:00
|
|
|
"83/tcp -> 0.0.0.0:9803"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-05-01 19:35:26 +00:00
|
|
|
dockerCmd(c, "rm", "-f", ID)
|
|
|
|
|
|
|
|
// test mixing protocols in same port range
|
|
|
|
out, _ = dockerCmd(c, "run", "-d",
|
|
|
|
"-p", "8000-8080:80",
|
|
|
|
"-p", "8000-8080:80/udp",
|
|
|
|
"busybox", "top")
|
|
|
|
ID = strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "port", ID)
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err = assertPortList(c, out, []string{
|
2015-05-01 19:35:26 +00:00
|
|
|
"80/tcp -> 0.0.0.0:8000",
|
2015-10-16 06:30:44 +00:00
|
|
|
"80/udp -> 0.0.0.0:8000"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-05-01 19:35:26 +00:00
|
|
|
dockerCmd(c, "rm", "-f", ID)
|
2014-06-24 02:03:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
func assertPortList(c *check.C, out string, expected []string) error {
|
2014-06-24 02:03:08 +00:00
|
|
|
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
|
|
|
if len(lines) != len(expected) {
|
2015-10-16 06:30:44 +00:00
|
|
|
return fmt.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
|
2014-06-24 02:03:08 +00:00
|
|
|
}
|
|
|
|
sort.Strings(lines)
|
|
|
|
sort.Strings(expected)
|
|
|
|
|
|
|
|
for i := 0; i < len(expected); i++ {
|
|
|
|
if lines[i] != expected[i] {
|
2015-10-16 06:30:44 +00:00
|
|
|
return fmt.Errorf("|" + lines[i] + "!=" + expected[i] + "|")
|
2014-06-24 02:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
return nil
|
2014-06-24 02:03:08 +00:00
|
|
|
}
|
2015-04-19 00:55:40 +00:00
|
|
|
|
2015-06-05 21:50:11 +00:00
|
|
|
func stopRemoveContainer(id string, c *check.C) {
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "rm", "-f", id)
|
2015-06-05 21:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-06-05 21:50:11 +00:00
|
|
|
// Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
|
|
|
|
port1 := 80
|
|
|
|
port2 := 443
|
|
|
|
expose1 := fmt.Sprintf("--expose=%d", port1)
|
|
|
|
expose2 := fmt.Sprintf("--expose=%d", port2)
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5")
|
2015-06-05 21:50:11 +00:00
|
|
|
|
|
|
|
// Check docker ps o/p for last created container reports the unpublished ports
|
|
|
|
unpPort1 := fmt.Sprintf("%d/tcp", port1)
|
|
|
|
unpPort2 := fmt.Sprintf("%d/tcp", port2)
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ := dockerCmd(c, "ps", "-n=1")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Missing unpublished ports in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, unpPort1)
|
|
|
|
// Missing unpublished ports in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, unpPort2)
|
2015-06-05 21:50:11 +00:00
|
|
|
|
|
|
|
// Run the container forcing to publish the exposed ports
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
|
2015-06-05 21:50:11 +00:00
|
|
|
|
|
|
|
// Check docker ps o/p for last created container reports the exposed ports in the port bindings
|
|
|
|
expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
|
|
|
|
expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort1) in docker ps output
|
|
|
|
c.Assert(expBndRegx1.MatchString(out), checker.Equals, true, check.Commentf("out: %s; unpPort1: %s", out, unpPort1))
|
|
|
|
// Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort2) in docker ps output
|
|
|
|
c.Assert(expBndRegx2.MatchString(out), checker.Equals, true, check.Commentf("out: %s; unpPort2: %s", out, unpPort2))
|
2015-06-05 21:50:11 +00:00
|
|
|
|
|
|
|
// Run the container specifying explicit port bindings for the exposed ports
|
|
|
|
offset := 10000
|
|
|
|
pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1)
|
|
|
|
pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2)
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5")
|
2015-06-05 21:50:11 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
// Check docker ps o/p for last created container reports the specified port mappings
|
|
|
|
expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
|
|
|
|
expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding (expBnd1) in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, expBnd1)
|
|
|
|
// Cannot find expected port binding (expBnd2) in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, expBnd2)
|
|
|
|
|
2015-12-13 16:00:39 +00:00
|
|
|
// Remove container now otherwise it will interfere with next test
|
2015-06-05 21:50:11 +00:00
|
|
|
stopRemoveContainer(id, c)
|
|
|
|
|
|
|
|
// Run the container with explicit port bindings and no exposed ports
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5")
|
2015-06-05 21:50:11 +00:00
|
|
|
id = strings.TrimSpace(out)
|
|
|
|
|
|
|
|
// Check docker ps o/p for last created container reports the specified port mappings
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding (expBnd1) in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, expBnd1)
|
|
|
|
// Cannot find expected port binding (expBnd2) in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, expBnd2)
|
2015-12-13 16:00:39 +00:00
|
|
|
// Remove container now otherwise it will interfere with next test
|
2015-06-05 21:50:11 +00:00
|
|
|
stopRemoveContainer(id, c)
|
|
|
|
|
|
|
|
// Run the container with one unpublished exposed port and one explicit port binding
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5")
|
2015-06-05 21:50:11 +00:00
|
|
|
|
|
|
|
// Check docker ps o/p for last created container reports the specified unpublished port and port mapping
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Missing unpublished exposed ports (unpPort1) in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, unpPort1)
|
|
|
|
// Missing port binding (expBnd2) in docker ps output
|
|
|
|
c.Assert(out, checker.Contains, expBnd2)
|
2015-06-05 21:50:11 +00:00
|
|
|
}
|
2015-08-31 21:40:36 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestPortHostBinding(c *check.C) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2015-08-31 21:40:36 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox",
|
|
|
|
"nc", "-l", "-p", "80")
|
|
|
|
firstID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
err := assertPortList(c, out, []string{"0.0.0.0:9876"})
|
|
|
|
// Port list is not correct
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-31 21:40:36 +00:00
|
|
|
|
|
|
|
dockerCmd(c, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", "9876")
|
|
|
|
|
|
|
|
dockerCmd(c, "rm", "-f", firstID)
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "9876")
|
|
|
|
// Port is still bound after the Container is removed
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
2015-08-31 21:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2015-08-31 21:40:36 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox",
|
|
|
|
"nc", "-l", "-p", "80")
|
|
|
|
firstID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
|
|
|
|
|
|
|
_, exposedPort, err := net.SplitHostPort(out)
|
2015-10-16 06:30:44 +00:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("out: %s", out))
|
2015-08-31 21:40:36 +00:00
|
|
|
|
|
|
|
dockerCmd(c, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", strings.TrimSpace(exposedPort))
|
|
|
|
|
|
|
|
dockerCmd(c, "rm", "-f", firstID)
|
|
|
|
|
2015-10-16 06:30:44 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", strings.TrimSpace(exposedPort))
|
|
|
|
// Port is still bound after the Container is removed
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
2015-08-31 21:40:36 +00:00
|
|
|
}
|
2016-01-26 03:55:18 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestPortBindingOnSandbox(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
|
|
|
dockerCmd(c, "network", "create", "--internal", "-d", "bridge", "internal-net")
|
2015-12-10 14:02:50 +00:00
|
|
|
nr := getNetworkResource(c, "internal-net")
|
|
|
|
c.Assert(nr.Internal, checker.Equals, true)
|
|
|
|
|
2016-01-26 03:55:18 +00:00
|
|
|
dockerCmd(c, "run", "--net", "internal-net", "-d", "--name", "c1",
|
|
|
|
"-p", "8080:8080", "busybox", "nc", "-l", "-p", "8080")
|
|
|
|
c.Assert(waitRun("c1"), check.IsNil)
|
|
|
|
|
|
|
|
_, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080")
|
|
|
|
c.Assert(err, check.NotNil,
|
|
|
|
check.Commentf("Port mapping on internal network is expected to fail"))
|
|
|
|
|
|
|
|
// Connect container to another normal bridge network
|
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", "foo-net")
|
|
|
|
dockerCmd(c, "network", "connect", "foo-net", "c1")
|
|
|
|
|
|
|
|
_, _, err = dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080")
|
|
|
|
c.Assert(err, check.IsNil,
|
|
|
|
check.Commentf("Port mapping on the new network is expected to succeed"))
|
|
|
|
|
|
|
|
}
|