Skip to content

Bug: Testcontainers in Docker container #644

Open

Description

@joeg-ita

method get_container_host_ip(self) in testcontainers/core/container.py

have the following code commented (you drive me crazy) and should be uncommented

    # # check testcontainers itself runs inside docker container
    # if inside_container() and not os.getenv("DOCKER_HOST") and not host.startswith("http://"):
    #     # If newly spawned container's gateway IP address from the docker
    #     # "bridge" network is equal to detected host address, we should use
    #     # container IP address, otherwise fall back to detected host
    #     # address. Even it's inside container, we need to double check,
    #     # because docker host might be set to docker:dind, usually in CI/CD environment
    #     gateway_ip = self.get_docker_client().gateway_ip(self._container.id)

    #     if gateway_ip == host:
    #         return self.get_docker_client().bridge_ip(self._container.id)
    #     return gateway_ip

Activity

  1. alexanderankin commented on Jul 10, 2024

    @alexanderankin
    Member

    am planning on fixing in #622

  2. tholu commented on Jul 16, 2024

    @tholu

    This should also come with documentation on how to setup a working CI pipeline on Gitlab and Github.

  3. alexanderankin commented on Jul 16, 2024

    @alexanderankin
    Member
  4. meiswjn commented on Jul 31, 2024

    @meiswjn
    Contributor

    It looks like this was introduced in #388
    @kiview this change sadly broke our setup I think - can't we find a way to support both?

    It essentially prevents all docker in docker use cases from what I see.

  5. mkemmerz commented on Jul 31, 2024

    @mkemmerz

    We were impacted by this as well, costed us almost 2 days 👎
    Solution was to go back to testcontainers 3.7.*, not a nice solution but it works at least.

  6. gaby commented on Aug 6, 2024

    @gaby

    @alexanderankin We are not using "dind" on a private hosted GitLab/Runner and it still doesnt work. Our runner does run in Docker, with "host" network and "/var/run/docker.sock" access. This used to work fine in v3.x hasnt work since 4.x.

    I'm also using testcontainers-go, and that one works fine, it's something with this package.

  7. tholu commented on Aug 6, 2024

    @tholu

    For me, it works like this (in a Gitlab Runner):

    my-job:
      variables:
        TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE: "/var/run/docker.sock"
        TESTCONTAINERS_RYUK_DISABLED: true
    
  8. malltshik commented on Sep 10, 2024

    @malltshik

    This workaround worked for us in github runner (it works well with ryuk being enabled):

    from testcontainers.core.container import DockerContainer
    
    def fixed_get_container_host_ip(self) -> str:
        host = self.get_docker_client().host() or "localhost"
        if inside_container() and not os.getenv("DOCKER_HOST") and not host.startswith("http://"):
            gateway_ip = self.get_docker_client().gateway_ip(self._container.id)
            if gateway_ip == host:
                return self.get_docker_client().bridge_ip(self._container.id)
            return gateway_ip
        return host
    
    DockerContainer.get_container_host_ip = fixed_get_container_host_ip
    
    # run your testcontainer afterwards
    
  9. kiview commented on Oct 15, 2024

    @kiview
    Member

    As @asafm reported in Slack, starting the container in Docker-Wormhole-Pattern mode with host network seems to work:

    --network host \
    -v /var/run/docker.sock:/var/run/docker.sock \
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions