diff --git a/docker.go b/docker.go index 1e01e61c26..91fcab6631 100644 --- a/docker.go +++ b/docker.go @@ -101,6 +101,13 @@ func (c *DockerContainer) Host(ctx context.Context) (string, error) { // MappedPort gets externally mapped port for a container port func (c *DockerContainer) MappedPort(ctx context.Context, port nat.Port) (nat.Port, error) { + inspect, err := c.inspectContainer(ctx) + if err != nil { + return "", err + } + if inspect.ContainerJSONBase.HostConfig.NetworkMode == "host" { + return port, nil + } ports, err := c.Ports(ctx) if err != nil { return "", err diff --git a/docker_test.go b/docker_test.go index 39b69044f6..d4d21a98a7 100644 --- a/docker_test.go +++ b/docker_test.go @@ -136,6 +136,35 @@ func TestContainerWithNetworkModeAndNetworkTogether(t *testing.T) { } } +func TestContainerWithHostNetworkOptionsAndWaitStrategy(t *testing.T) { + ctx := context.Background() + gcr := GenericContainerRequest{ContainerRequest: ContainerRequest{ + Image: "nginx", + SkipReaper: true, + NetworkMode: "host", + WaitingFor: wait.ForListeningPort("80/tcp"), + }, + Started: true, + } + + nginxC, err := GenericContainer(ctx, gcr) + if err != nil { + t.Fatal(err) + } + + defer nginxC.Terminate(ctx) + + host, err := nginxC.Host(ctx) + if err != nil { + t.Errorf("Expected host %s. Got '%d'.", host, err) + } + + _, err = http.Get("http://" + host + ":80") + if err != nil { + t.Errorf("Expected OK response. Got '%d'.", err) + } +} + func TestContainerReturnItsContainerID(t *testing.T) { ctx := context.Background() nginxA, err := GenericContainer(ctx, GenericContainerRequest{