2014-06-24 02:03:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-11 20:45:16 +00:00
|
|
|
"context"
|
2015-06-05 21:50:11 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2014-06-24 02:03:08 +00:00
|
|
|
"sort"
|
2017-09-08 15:16:15 +00:00
|
|
|
"strconv"
|
2014-06-24 02:03:08 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2023-07-14 18:02:38 +00:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2014-06-24 02:03:08 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLIPortSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLIPortSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIPortSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIPortSuite) TestPortList(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
|
2014-06-24 02:03:08 +00:00
|
|
|
// one port
|
2023-07-27 20:11:27 +00:00
|
|
|
firstID := cli.DockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "top").Stdout()
|
|
|
|
firstID = strings.TrimSpace(firstID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out := cli.DockerCmd(c, "port", firstID, "80").Stdout()
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876"})
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", firstID).Stdout()
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876", "80/tcp -> [::]:9876"})
|
2015-10-16 06:30:44 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", firstID)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
|
|
|
// three port
|
2023-07-27 20:11:27 +00:00
|
|
|
id := cli.DockerCmd(c, "run", "-d",
|
2014-06-24 02:03:08 +00:00
|
|
|
"-p", "9876:80",
|
|
|
|
"-p", "9877:81",
|
|
|
|
"-p", "9878:82",
|
2023-07-27 20:11:27 +00:00
|
|
|
"busybox", "top",
|
|
|
|
).Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", id, "80").Stdout()
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876"})
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", id).Stdout()
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{
|
2014-06-24 02:03:08 +00:00
|
|
|
"80/tcp -> 0.0.0.0:9876",
|
2021-01-20 16:48:05 +00:00
|
|
|
"80/tcp -> [::]:9876",
|
2014-06-24 02:03:08 +00:00
|
|
|
"81/tcp -> 0.0.0.0:9877",
|
2021-01-20 16:48:05 +00:00
|
|
|
"81/tcp -> [::]:9877",
|
2021-02-11 20:45:16 +00:00
|
|
|
"82/tcp -> 0.0.0.0:9878",
|
2021-01-20 16:48:05 +00:00
|
|
|
"82/tcp -> [::]:9878",
|
2021-02-11 20:45:16 +00:00
|
|
|
})
|
2015-10-16 06:30:44 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", id)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
|
|
|
// more and one port mapped to the same container port
|
2023-07-27 20:11:27 +00:00
|
|
|
id = cli.DockerCmd(c, "run", "-d",
|
2014-06-24 02:03:08 +00:00
|
|
|
"-p", "9876:80",
|
|
|
|
"-p", "9999:80",
|
|
|
|
"-p", "9877:81",
|
|
|
|
"-p", "9878:82",
|
2023-07-27 20:11:27 +00:00
|
|
|
"busybox", "top",
|
|
|
|
).Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", id, "80").Stdout()
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876", "0.0.0.0:9999", "[::]:9999"})
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", id).Stdout()
|
2014-06-24 02:03:08 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
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",
|
2021-01-20 16:48:05 +00:00
|
|
|
"80/tcp -> [::]:9876",
|
|
|
|
"80/tcp -> [::]:9999",
|
2014-06-24 02:03:08 +00:00
|
|
|
"81/tcp -> 0.0.0.0:9877",
|
2021-01-20 16:48:05 +00:00
|
|
|
"81/tcp -> [::]:9877",
|
2021-02-11 20:45:16 +00:00
|
|
|
"82/tcp -> 0.0.0.0:9878",
|
2021-01-20 16:48:05 +00:00
|
|
|
"82/tcp -> [::]:9878",
|
2021-02-11 20:45:16 +00:00
|
|
|
})
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.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++ {
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "run", "-d", "-p", "9090-9092:80", "busybox", "top").Stdout()
|
2015-05-01 19:35:26 +00:00
|
|
|
IDs[i] = strings.TrimSpace(out)
|
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", IDs[i]).Stdout()
|
2015-05-01 19:35:26 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{
|
2021-01-20 16:48:05 +00:00
|
|
|
fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i),
|
|
|
|
fmt.Sprintf("80/tcp -> [::]:%d", 9090+i),
|
|
|
|
})
|
2015-05-01 19:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test port range exhaustion
|
2023-07-14 18:02:38 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "-p", "9090-9092:80", "busybox", "top")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Exhausted port range did not return an error
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-05-01 19:35:26 +00:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", IDs[i])
|
2015-05-01 19:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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"} {
|
2023-07-14 18:02:38 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "-p", invalidRange, "busybox", "top")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Port range should have returned an error
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-05-01 19:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test host range:container range spec.
|
2023-07-27 20:11:27 +00:00
|
|
|
id = cli.DockerCmd(c, "run", "-d", "-p", "9800-9803:80-83", "busybox", "top").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2015-05-01 19:35:26 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", id).Stdout()
|
2015-05-01 19:35:26 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{
|
2015-05-01 19:35:26 +00:00
|
|
|
"80/tcp -> 0.0.0.0:9800",
|
2021-01-20 16:48:05 +00:00
|
|
|
"80/tcp -> [::]:9800",
|
2015-05-01 19:35:26 +00:00
|
|
|
"81/tcp -> 0.0.0.0:9801",
|
2021-01-20 16:48:05 +00:00
|
|
|
"81/tcp -> [::]:9801",
|
2015-05-01 19:35:26 +00:00
|
|
|
"82/tcp -> 0.0.0.0:9802",
|
2021-01-20 16:48:05 +00:00
|
|
|
"82/tcp -> [::]:9802",
|
2021-02-11 20:45:16 +00:00
|
|
|
"83/tcp -> 0.0.0.0:9803",
|
2021-01-20 16:48:05 +00:00
|
|
|
"83/tcp -> [::]:9803",
|
2021-02-11 20:45:16 +00:00
|
|
|
})
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", id)
|
2015-05-01 19:35:26 +00:00
|
|
|
|
|
|
|
// test mixing protocols in same port range
|
2023-07-27 20:11:27 +00:00
|
|
|
id = cli.DockerCmd(c, "run", "-d", "-p", "8000-8080:80", "-p", "8000-8080:80/udp", "busybox", "top").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2015-05-01 19:35:26 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "port", id).Stdout()
|
2017-09-12 12:53:20 +00:00
|
|
|
// Running this test multiple times causes the TCP port to increment.
|
2023-07-27 20:11:27 +00:00
|
|
|
err := assertPortRange(ctx, id, []int{8000, 8080}, []int{8000, 8080})
|
|
|
|
assert.Check(c, err)
|
|
|
|
cli.DockerCmd(c, "rm", "-f", id)
|
2014-06-24 02:03:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func assertPortList(c *testing.T, out string, expected []string) {
|
2021-02-11 20:45:16 +00:00
|
|
|
c.Helper()
|
2014-06-24 02:03:08 +00:00
|
|
|
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
2023-07-14 18:02:38 +00:00
|
|
|
assert.Assert(c, is.Len(lines, len(expected)), "exepcted: %s", strings.Join(expected, ", "))
|
|
|
|
|
2014-06-24 02:03:08 +00:00
|
|
|
sort.Strings(lines)
|
|
|
|
sort.Strings(expected)
|
|
|
|
|
2021-02-11 20:45:16 +00:00
|
|
|
// "docker port" does not yet have a "--format" flag, and older versions
|
|
|
|
// of the CLI used an incorrect output format for mappings on IPv6 addresses
|
|
|
|
// for example, "80/tcp -> :::80" instead of "80/tcp -> [::]:80".
|
|
|
|
oldFormat := func(mapping string) string {
|
2021-03-24 23:16:23 +00:00
|
|
|
old := strings.Replace(mapping, "[", "", 1)
|
2021-02-11 20:45:16 +00:00
|
|
|
old = strings.Replace(old, "]:", ":", 1)
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
2014-06-24 02:03:08 +00:00
|
|
|
for i := 0; i < len(expected); i++ {
|
2021-02-11 20:45:16 +00:00
|
|
|
if lines[i] == expected[i] {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-14 18:02:38 +00:00
|
|
|
assert.Equal(c, lines[i], oldFormat(expected[i]))
|
2014-06-24 02:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-19 00:55:40 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func assertPortRange(ctx context.Context, id string, expectedTCP, expectedUDP []int) error {
|
2021-02-11 20:45:16 +00:00
|
|
|
client := testEnv.APIClient()
|
2023-07-14 18:02:38 +00:00
|
|
|
inspect, err := client.ContainerInspect(ctx, id)
|
2021-02-11 20:45:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-08 15:16:15 +00:00
|
|
|
|
2019-08-05 13:17:42 +00:00
|
|
|
var validTCP, validUDP bool
|
2021-02-11 20:45:16 +00:00
|
|
|
for portAndProto, binding := range inspect.NetworkSettings.Ports {
|
|
|
|
if portAndProto.Proto() == "tcp" && len(expectedTCP) == 0 {
|
|
|
|
continue
|
2017-09-08 15:16:15 +00:00
|
|
|
}
|
2021-02-11 20:45:16 +00:00
|
|
|
if portAndProto.Proto() == "udp" && len(expectedTCP) == 0 {
|
|
|
|
continue
|
2017-09-08 15:16:15 +00:00
|
|
|
}
|
2021-02-11 20:45:16 +00:00
|
|
|
|
|
|
|
for _, b := range binding {
|
|
|
|
port, err := strconv.Atoi(b.HostPort)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(expectedTCP) > 0 {
|
|
|
|
if port < expectedTCP[0] || port > expectedTCP[1] {
|
|
|
|
return fmt.Errorf("tcp port (%d) not in range expected range %d-%d", port, expectedTCP[0], expectedTCP[1])
|
|
|
|
}
|
|
|
|
validTCP = true
|
|
|
|
}
|
|
|
|
if len(expectedUDP) > 0 {
|
|
|
|
if port < expectedUDP[0] || port > expectedUDP[1] {
|
|
|
|
return fmt.Errorf("udp port (%d) not in range expected range %d-%d", port, expectedUDP[0], expectedUDP[1])
|
|
|
|
}
|
|
|
|
validUDP = true
|
2017-09-08 15:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-05 13:17:42 +00:00
|
|
|
if !validTCP {
|
2017-09-08 15:16:15 +00:00
|
|
|
return fmt.Errorf("tcp port not found")
|
|
|
|
}
|
2019-08-05 13:17:42 +00:00
|
|
|
if !validUDP {
|
2017-09-08 15:16:15 +00:00
|
|
|
return fmt.Errorf("udp port not found")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func stopRemoveContainer(id string, c *testing.T) {
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", id)
|
2015-06-05 21:50:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPortSuite) TestUnpublishedPortsInPsOutput(c *testing.T) {
|
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)
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.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)
|
2023-07-27 20:11:27 +00:00
|
|
|
out := cli.DockerCmd(c, "ps", "-n=1").Stdout()
|
2015-10-16 06:30:44 +00:00
|
|
|
// Missing unpublished ports in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, unpPort1))
|
2015-10-16 06:30:44 +00:00
|
|
|
// Missing unpublished ports in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, unpPort2))
|
2015-06-05 21:50:11 +00:00
|
|
|
// Run the container forcing to publish the exposed ports
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.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)
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-n=1").Stdout()
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort1) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, expBndRegx1.MatchString(out), true, fmt.Sprintf("out: %s; unpPort1: %s", out, unpPort1))
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort2) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, expBndRegx2.MatchString(out), true, fmt.Sprintf("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)
|
2023-07-27 20:11:27 +00:00
|
|
|
|
|
|
|
id := cli.DockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2015-06-05 21:50:11 +00:00
|
|
|
|
|
|
|
// 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)
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-n=1").Stdout()
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding (expBnd1) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, expBnd1))
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding (expBnd2) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, 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
|
2023-07-27 20:11:27 +00:00
|
|
|
id = cli.DockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2015-06-05 21:50:11 +00:00
|
|
|
|
|
|
|
// Check docker ps o/p for last created container reports the specified port mappings
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-n=1").Stdout()
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding (expBnd1) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, expBnd1))
|
2015-10-16 06:30:44 +00:00
|
|
|
// Cannot find expected port binding (expBnd2) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, 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
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.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
|
2023-07-27 20:11:27 +00:00
|
|
|
out = cli.DockerCmd(c, "ps", "-n=1").Stdout()
|
2015-10-16 06:30:44 +00:00
|
|
|
// Missing unpublished exposed ports (unpPort1) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, unpPort1))
|
2015-10-16 06:30:44 +00:00
|
|
|
// Missing port binding (expBnd2) in docker ps output
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, expBnd2))
|
2015-06-05 21:50:11 +00:00
|
|
|
}
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPortSuite) TestPortHostBinding(c *testing.T) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2023-07-27 20:11:27 +00:00
|
|
|
firstID := cli.DockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "nc", "-l", "-p", "80").Stdout()
|
|
|
|
firstID = strings.TrimSpace(firstID)
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
out := cli.DockerCmd(c, "port", firstID, "80").Stdout()
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876"})
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "run", "--net=host", "busybox", "nc", "localhost", "9876")
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", firstID)
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "9876")
|
2015-10-16 06:30:44 +00:00
|
|
|
// Port is still bound after the Container is removed
|
2023-07-14 18:02:38 +00:00
|
|
|
assert.Assert(c, err != nil, out)
|
2015-08-31 21:40:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPortSuite) TestPortExposeHostBinding(c *testing.T) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2023-07-27 20:11:27 +00:00
|
|
|
firstID := cli.DockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox", "nc", "-l", "-p", "80").Stdout()
|
|
|
|
firstID = strings.TrimSpace(firstID)
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
exposedPort := cli.DockerCmd(c, "inspect", "--format", `{{index .NetworkSettings.Ports "80/tcp" 0 "HostPort" }}`, firstID).Stdout()
|
|
|
|
exposedPort = strings.TrimSpace(exposedPort)
|
|
|
|
cli.DockerCmd(c, "run", "--net=host", "busybox", "nc", "127.0.0.1", exposedPort)
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", firstID)
|
2015-08-31 21:40:36 +00:00
|
|
|
|
2021-02-11 20:45:16 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "127.0.0.1", exposedPort)
|
2015-10-16 06:30:44 +00:00
|
|
|
// Port is still bound after the Container is removed
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-08-31 21:40:36 +00:00
|
|
|
}
|
2016-01-26 03:55:18 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIPortSuite) TestPortBindingOnSandbox(c *testing.T) {
|
2016-01-26 03:55:18 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "network", "create", "--internal", "-d", "bridge", "internal-net")
|
2015-12-10 14:02:50 +00:00
|
|
|
nr := getNetworkResource(c, "internal-net")
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, nr.Internal, true)
|
2015-12-10 14:02:50 +00:00
|
|
|
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "run", "--net", "internal-net", "-d", "--name", "c1", "-p", "8080:8080", "busybox", "nc", "-l", "-p", "8080")
|
|
|
|
cli.WaitRun(c, "c1")
|
2016-01-26 03:55:18 +00:00
|
|
|
|
|
|
|
_, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err != nil, "Port mapping on internal network is expected to fail")
|
2016-01-26 03:55:18 +00:00
|
|
|
// Connect container to another normal bridge network
|
2023-07-27 20:11:27 +00:00
|
|
|
cli.DockerCmd(c, "network", "create", "-d", "bridge", "foo-net")
|
|
|
|
cli.DockerCmd(c, "network", "connect", "foo-net", "c1")
|
2016-01-26 03:55:18 +00:00
|
|
|
|
|
|
|
_, _, err = dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, err == nil, "Port mapping on the new network is expected to succeed")
|
2016-01-26 03:55:18 +00:00
|
|
|
}
|