Skip to content

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

Open
@RemiBou

Description

@RemiBou

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

connectToPortForwardingNetwork(createCommand.getNetworkMode());

here to add the hostname resolution

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions