testcontainers / testcontainers/testcontainers-java

[Bug]: Containers created with DockerCompose can't resolve host.testcontainers.internal

Abierto
#6,932 1 comentario 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

type/bug
Lenguaje dominante
Java
Estrellas
8.7k
Forks
1.9k
Merge medio
2 d 17 h
PR fusionados (30 d)
9

Descripción

Module

Core

Testcontainers version

1.17.2

Using the latest Testcontainers version?

No

Host OS

MacOS

Host Arch

x86

Docker version
Client:
 Cloud integration: v1.0.31
 Version:           20.10.24
 API version:       1.41
 Go version:        go1.19.7
 Git commit:        297e128
 Built:             Tue Apr  4 18:21:21 2023
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.18.0 (104112)
 Engine:
  Version:          20.10.24
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.19.7
  Git commit:       5d6db84
  Built:            Tue Apr  4 18:18:42 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.18
  GitCommit:        2456e983eb9e37e47538f59ea18f2043c9a73640
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
What happened?

If started an HttpServer, then exposed its port using

Testcontainers.exposeHostPorts(webHookPort);

Then I start my service using DockerComposeContainer

My service then can't resolve hostname "host.testcontainers.internal"

This is because all the magic to resolve this is done in GenericContainer

here to attach to network https://github.com/testcontainers/testcontainers-java/blob/b448c9a25763da3d99e6f15aeba4941b85cc0215/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L435

here to add the hostname resolution

https://github.com/testcontainers/testcontainers-java/blob/b448c9a25763da3d99e6f15aeba4941b85cc0215/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L869

I was able to workaround it :

Add this to docker compose on service where you need the host to be resovled

    extra_hosts:
      - "host.testcontainers.internal:${HOST_TESTCONTAINERS_INTERNAL_IP}"

Then before starting the docker compose get an instance of the sshd container and its network

  var dockerCLient = DockerClientFactory.lazyClient();
        final Container sshdContainer = dockerCLient.listContainersCmd()
                .exec()
                .stream()
                .filter(c -> c.getImage().contains("sshd"))
                .findFirst()
                .orElseThrow(() -> new Exception("PortForwardingContainer not found"));

        final ContainerNetwork sshContainerNetwork = sshdContainer.getNetworkSettings()
                        .getNetworks()
                        .values()
                        .stream()
                        .findFirst()
                        .orElseThrow(() -> new Exception("PortForwardingContainer network not found"));

Then you can push the IP to the DockerCompose env

                .withEnv("HOST_TESTCONTAINERS_INTERNAL_IP", sshContainerNetwork.getIpAddress())

Then join your containers to the network with command

 final ContainerState mycontainer = dockerCompose.getContainerByServiceName("service_1")
                .orElseThrow();
  mycontainer.getDockerClient().connectToNetworkCmd()
          .withContainerId(mycontainer.getContainerId())
          .withNetworkId(sshContainerNetwork.getNetworkID())
          .exec();

Relevant log output

No response

Additional Information

No response

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza rastreando exposeHostPorts en core/src/main/java/org/testcontainers/containers/GenericContainer.java en el archivo adjunto mencionado y el código de resolución de nombres de host; después, compara ese comportamiento con el inicio de DockerComposeContainer. Reproduce el caso de MacOS Docker Compose descrito en el issue y verifica que un servicio de Compose pueda resolver host.testcontainers.internal y alcanzar el puerto de host expuesto sin el workaround.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
docker, docker-compose, java
Área
networking, testing-qa
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.