testcontainers / testcontainers/testcontainers-java

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

Aperta
#6,932 1 commento 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

type/bug
Lingua principale
Java
Stelle
8.7k
Fork
1.9k
Merge medio
2g 17h
PR unite (30g)
9

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando exposeHostPorts in core/src/main/java/org/testcontainers/containers/GenericContainer.java nell’allegato indicato e il codice di risoluzione dei nomi host, quindi confronta tale comportamento con l’avvio di DockerComposeContainer. Riproduci il caso MacOS Docker Compose descritto nell’issue e verifica che un servizio Compose possa risolvere host.testcontainers.internal e raggiungere la porta host esposta senza il workaround.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
docker, docker-compose, java
Ambito
networking, testing-qa
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.