Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning all IP addresses of a container #553

Merged
merged 6 commits into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion container.go
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
Expand Up @@ -393,6 +393,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 _, endpoints := range networks {
ips = append(ips, endpoints.IPAddress)
gauravgahlot marked this conversation as resolved.
Show resolved Hide resolved
}

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
Expand Up @@ -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()

Expand Down