testcontainers / testcontainers/testcontainers-java

container.getContainerIpAddress() not working when run inside Docker with a host configured with TCP socket and not unix socket

Open
#1,914 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

resolution/acknowledged
Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

In our Jenkins configuration, the Docker host is configured with tcp: protocol and not unix: protocol.
TestContainers is run from Maven tests inside a Jenkins agent that is a Docker container itself.
The socket is correctly shared between the host and the container.

The problem is that, in this configuration, someContainer.getContainerIpAddress() returns localhost instead of the default gateway.

This seems to be because, in the DockerClientConfigUtils.getDockerHostIpAddress() method, the IN_CONTAINER property is only used for the unix or npipe schemes:

    public static String getDockerHostIpAddress(DockerClientConfig config) {
        switch (config.getDockerHost().getScheme()) {
        case "http": 
        case "https": 
        case "tcp": 
            return config.getDockerHost().getHost();
        case "unix": 
        case "npipe": 
            if (IN_A_CONTAINER) {
                return getDefaultGateway().orElse("localhost");
            }
            return "localhost";
        default: 
            return null;
        }
    }

The current workaround I found for my environment was to create a wrapper method of my own:

    /**
     * Workaround method to get the right Docker host IP address depending on whether
     * the tests are run inside Docker or not.
     * <p>
     * The problem with the method
     * {@link DockerClientConfigUtils#getDockerHostIpAddress(com.github.dockerjava.core.DockerClientConfig)},
     * is that when running inside a container, and the Docker host is using the {@code tcp:} protocol like
     * in {@code tcp://localhost}, the Docker host address is resolved to {@code localhost} instead of the
     * default gateway.
     */
    static String getDockerHostIp(ContainerState container) {
        if (DockerClientConfigUtils.IN_A_CONTAINER) {
            return DockerClientConfigUtils
                    .getDefaultGateway()
                    .orElse("172.18.0.1");
        } else {
            return container.getContainerIpAddress();
        }
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DockerClientConfigUtils.getDockerHostIpAddress() and inspect how IN_A_CONTAINER, the Docker host scheme, and getDefaultGateway() interact. Reproduce the TCP-socket case from the issue and verify that the container host address resolves correctly while non-container behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java
Domain
devops, networking, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.