testcontainers / testcontainers/testcontainers-java

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

Offen
#1,914 3 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

resolution/acknowledged
Vorherrschende Sprache
Java
Sterne
8.7k
Forks
1.9k
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
9

Beschreibung

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();
        }
    }

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit DockerClientConfigUtils.getDockerHostIpAddress() und untersuche, wie IN_A_CONTAINER, das Docker-Host-Schema und getDefaultGateway() zusammenspielen. Reproduziere den TCP-Socket-Fall aus dem Issue und überprüfe, dass die Container-Host-Adresse korrekt aufgelöst wird, während das Verhalten außerhalb von Containern unverändert bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
docker, java
Bereich
devops, networking, testing
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.