Skip to content

Commit

Permalink
Support returning all IP addresses of a container (#553)
Browse files Browse the repository at this point in the history
* return all container IP addresses

* review fixes

* add unit test

* renaming

Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
  • Loading branch information
gauravgahlot and mdelapenya committed Oct 14, 2022
1 parent c0d73a6 commit 5e65c25
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,23 @@ func (c *DockerContainer) ContainerIP(ctx context.Context) (string, error) {
return ip, nil
}

// 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 {
return nil, err
}

networks := inspect.NetworkSettings.Networks
for _, nw := range networks {
ips = append(ips, nw.IPAddress)
}

return ips, 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)
Expand Down
48 changes: 48 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,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()

Expand Down

0 comments on commit 5e65c25

Please sign in to comment.