testcontainers / testcontainers-python

Testcontainers is a Python library that providing a friendly API to run Docker container. It is designed to create runtime environment to use during your automatic tests.
https://testcontainers-python.readthedocs.io/en/latest/
Apache License 2.0
1.54k stars 281 forks source link

Bug: Testcontainers in Docker container #644

Open joeg-ita opened 2 months ago

joeg-ita commented 2 months ago

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
alexanderankin commented 2 months ago

am planning on fixing in #622

tholu commented 2 months ago

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

alexanderankin commented 2 months ago

Git hub is easy, it does not require dind

On Tue, Jul 16, 2024, 7:05 PM Thomas Lutz @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/testcontainers/testcontainers-python/issues/644#issuecomment-2231958513, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACECGJA33YMVZKRT54KPA5TZMWRLDAVCNFSM6AAAAABKUPDO5GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMZRHE2TQNJRGM . You are receiving this because you commented.Message ID: @.***>

meiswjn commented 2 months ago

It looks like this was introduced in https://github.com/testcontainers/testcontainers-python/pull/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.

mkemmerz commented 2 months ago

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.

gaby commented 2 months ago

@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.

tholu commented 2 months ago

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
malltshik commented 3 weeks ago

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