From f879f8acb358981475a1227ab5374b0b9b11e461 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 6 Oct 2022 10:20:51 +0530 Subject: [PATCH 1/4] return all container IP addresses --- docker.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docker.go b/docker.go index b710ea08bb..125971ce4f 100644 --- a/docker.go +++ b/docker.go @@ -393,6 +393,23 @@ func (c *DockerContainer) ContainerIP(ctx context.Context) (string, error) { return ip, nil } +// ContainerIPList gets the IP addresses of all the networks within the container. +func (c *DockerContainer) ContainerIPList(ctx context.Context) ([]string, error) { + ipList := make([]string, 0) + + inspect, err := c.inspectContainer(ctx) + if err != nil { + return nil, err + } + + networks := inspect.NetworkSettings.Networks + for _, endpoints := range networks { + ipList = append(ipList, endpoints.IPAddress) + } + + return ipList, nil +} + // NetworkAliases gets the aliases of the container for the networks it is attached to. func (c *DockerContainer) NetworkAliases(ctx context.Context) (map[string][]string, error) { inspect, err := c.inspectContainer(ctx) From 93b1c37ca8df99c0f8e94d443343ee03f32d20ba Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 6 Oct 2022 11:29:06 +0530 Subject: [PATCH 2/4] review fixes --- docker.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docker.go b/docker.go index 125971ce4f..f89039996f 100644 --- a/docker.go +++ b/docker.go @@ -393,9 +393,9 @@ func (c *DockerContainer) ContainerIP(ctx context.Context) (string, error) { return ip, nil } -// ContainerIPList gets the IP addresses of all the networks within the container. -func (c *DockerContainer) ContainerIPList(ctx context.Context) ([]string, error) { - ipList := make([]string, 0) +// ContainerIPs gets the IP addresses of all the networks within the container. +func (c *DockerContainer) ContainerIPs(ctx context.Context) ([]string, error) { + ips := make([]string, 0) inspect, err := c.inspectContainer(ctx) if err != nil { @@ -404,10 +404,10 @@ func (c *DockerContainer) ContainerIPList(ctx context.Context) ([]string, error) networks := inspect.NetworkSettings.Networks for _, endpoints := range networks { - ipList = append(ipList, endpoints.IPAddress) + ips = append(ips, endpoints.IPAddress) } - return ipList, nil + return ips, nil } // NetworkAliases gets the aliases of the container for the networks it is attached to. From 91c75a3dfda7a4feb550e7fe933e1d339d72073c Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 6 Oct 2022 12:05:12 +0530 Subject: [PATCH 3/4] add unit test --- container.go | 3 ++- docker_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/container.go b/container.go index ddb78da94a..fec922fd7d 100644 --- a/container.go +++ b/container.go @@ -55,7 +55,8 @@ type Container interface { Networks(context.Context) ([]string, error) // get container networks NetworkAliases(context.Context) (map[string][]string, error) // get container network aliases for a network Exec(ctx context.Context, cmd []string) (int, io.Reader, error) - ContainerIP(context.Context) (string, error) // get container ip + ContainerIP(context.Context) (string, error) // get container ip + ContainerIPs(context.Context) ([]string, error) // get all container IPs CopyToContainer(ctx context.Context, fileContent []byte, containerFilePath string, fileMode int64) error CopyDirToContainer(ctx context.Context, hostDirPath string, containerParentPath string, fileMode int64) error CopyFileToContainer(ctx context.Context, hostFilePath string, containerFilePath string, fileMode int64) error diff --git a/docker_test.go b/docker_test.go index fe77f7a8ad..75df7e5b89 100644 --- a/docker_test.go +++ b/docker_test.go @@ -751,6 +751,54 @@ func TestContainerCreation(t *testing.T) { } } +func TestContainerIPs(t *testing.T) { + ctx := context.Background() + + networkName := "new-network" + newNetwork, err := GenericNetwork(ctx, GenericNetworkRequest{ + ProviderType: providerType, + NetworkRequest: NetworkRequest{ + Name: networkName, + CheckDuplicate: true, + }, + }) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(func() { + require.NoError(t, newNetwork.Remove(ctx)) + }) + + nginxC, err := GenericContainer(ctx, GenericContainerRequest{ + ProviderType: providerType, + ContainerRequest: ContainerRequest{ + Image: nginxAlpineImage, + ExposedPorts: []string{ + nginxDefaultPort, + }, + Networks: []string{ + "bridge", + networkName, + }, + WaitingFor: wait.ForListeningPort(nginxDefaultPort), + }, + Started: true, + }) + + require.NoError(t, err) + terminateContainerOnEnd(t, ctx, nginxC) + + ips, err := nginxC.ContainerIPs(ctx) + if err != nil { + t.Fatal(err) + } + + if len(ips) != 2 { + t.Errorf("Expected two IP addresses, got %v", len(ips)) + } +} + func TestContainerCreationWithName(t *testing.T) { ctx := context.Background() From 8311b8baab92b85d3831647057935c81df3eaf0f Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 6 Oct 2022 13:50:10 +0530 Subject: [PATCH 4/4] renaming --- docker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker.go b/docker.go index f89039996f..120c03c330 100644 --- a/docker.go +++ b/docker.go @@ -403,8 +403,8 @@ func (c *DockerContainer) ContainerIPs(ctx context.Context) ([]string, error) { } networks := inspect.NetworkSettings.Networks - for _, endpoints := range networks { - ips = append(ips, endpoints.IPAddress) + for _, nw := range networks { + ips = append(ips, nw.IPAddress) } return ips, nil