testcontainers / testcontainers/testcontainers-java
container.getContainerIpAddress() not working when run inside Docker with a host configured with TCP socket and not unix socket
Nessuno ha ancora preso questa issue.
- Lingua principale
- Java
- Stelle
- 8.7k
- Fork
- 1.9k
- Merge medio
- 2g 17h
- PR unite (30g)
- 9
Descrizione
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();
}
}
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia da DockerClientConfigUtils.getDockerHostIpAddress() e analizza come interagiscono IN_A_CONTAINER, lo schema dell’host Docker e getDefaultGateway(). Riproduci il caso del socket TCP dell’issue e verifica che l’indirizzo dell’host del container venga risolto correttamente, mentre il comportamento al di fuori del container rimanga invariato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- docker, java
- Ambito
- devops, networking, testing
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100