testcontainers / testcontainers/testcontainers-java

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

オープン
#6,932 コメント 1 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

type/bug
主要言語
Java
スター
8.7k
フォーク
1.9k
平均マージ
2日 17時間
マージ済み PR(30日)
9

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、参照されている添付ファイルの core/src/main/java/org/testcontainers/containers/GenericContainer.java にある exposeHostPorts と、ホスト名解決のコードを追跡し、その動作を DockerComposeContainer の起動時の動作と比較します。Issue に記載されている MacOS Docker Compose のケースを再現し、Compose サービスが host.testcontainers.internal を解決し、workaround なしで公開されたホストポートに到達できることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
docker, docker-compose, java
領域
networking, testing-qa
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。