You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Testcontainers fails to get container_host_ip when running in docker network #141
Hi, I have the same issue when trying to run an integration test inside a docker container.
As described in the README, when testcontainers-py is running inside a docker container a docker client is required and the sock must be mounted as volume (with -v /var/run/docker.sock:/var/run/docker.sock)
Anyway, when the tests are running they fails because of:
Hello, I'm trying to run integration test inside .devcontainer (with docker-compose) and got the same error with @dlmiddlecote
Any workaround or solution so far?
Thanks.
Sharing this test as an as the workaround I've taken to getting testcontainers working from within our devcontainer (noting we originally had DOCKER_HOST set too, which needed changed as it causes a code path to be ignored). Maybe it will help someone else, or point to what I'm doing wrong.
fromtestcontainers.postgresimportPostgresContainerimportloggingimportsqlalchemyimportoslogger=logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler=logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
deftest_container_runs():
postgres_container=PostgresContainer("postgres:14.6")
# If we are running in a container, we need to set an environment variable to allow test# containers to work, this might only be required in the devcontainer - might need to # validate that, for now we'll assume it is only in the dev container by looking for# our marker file# The first check is from how testcontainers determines if we are in a container, # the second is our own check to a file that we know is contained in our devcontainer# Reference links:# https://github.com/testcontainers/testcontainers-python/blob/main/core/testcontainers/core/utils.py#L47# https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25ifos.path.exists('/.dockerenv') andos.path.exists('/installation-scripts/versions.properties'):
logger.info("In DevContainer workaround...")
gateway_ip=postgres_container.get_docker_client().gateway_ip(os.environ["HOSTNAME"])
logger.info("Setting TC_HOST to gateway ip: "+gateway_ip)
os.environ["TC_HOST"] =gateway_ipwithpostgres_containeraspostgres:
engine=sqlalchemy.create_engine(postgres.get_connection_url())
withengine.begin() asconnection:
result=connection.execute(sqlalchemy.text("select version()"))
version, =result.fetchone()
logger.info("Version is "+version)
I'm not quite sure if there is a bug or if I'm doing something wrong (our devcontainer should be setup as DIND, but is actually docker-from-docker which causes all manner of fun issues).
The key point in this is we have some code that detects if it is running in our devcontainer, and if so sets the TC_HOST environment variable to the gateway IP for the docker network that the devcontainer is running in. With this set testcontainers will correctly determine that the postgres container has started.
Faced the same issue in the tests. The working workaround for me is to patch the current implementation of this method (pytest example)
defgateway_ip(self, container_id):
"""Default DockerClient.gateway_ip doesn't know how to work with Docker networks, so this is the workaround. Fix as soon as the https://github.com/testcontainers/testcontainers-python/issues/141 is fixed. """container=self.get_container(container_id)
networks=container['NetworkSettings']['Networks']
if'bridge'innetworks:
returncontainer['NetworkSettings']['Networks']['bridge']['Gateway']
iflen(networks) ==1:
returnlist(networks.values())[0]['Gateway']
raiseValueError(container['NetworkSettings']['Networks'])
@pytest.fixture()defcontainer_network(monkeypatch) ->Iterable[Network]:
monkeypatch.setattr(
DockerClient,
'gateway_ip',
gateway_ip,
)
client=docker.from_env()
network_name=f'test_{uuid.uuid4().hex}'network=client.networks.create(network_name)
yieldnetworknetwork.remove()
Activity
dcc-sapienza commentedon May 7, 2021
Hi, I have the same issue when trying to run an integration test inside a docker container.
As described in the README, when testcontainers-py is running inside a docker container a docker client is required and the sock must be mounted as volume (with -v /var/run/docker.sock:/var/run/docker.sock)
Anyway, when the tests are running they fails because of:
PS: the testcontainers java implementation work within docker container with the same configuration.
hungphamvn commentedon May 25, 2023
Hello, I'm trying to run integration test inside .devcontainer (with docker-compose) and got the same error with @dlmiddlecote
Any workaround or solution so far?
Thanks.
Fix testcontainers#141 - getting ip addresses on custom networks
Fix testcontainers#141 - getting ip addresses on custom networks
dhutchison commentedon Sep 21, 2023
Sharing this test as an as the workaround I've taken to getting testcontainers working from within our devcontainer (noting we originally had
DOCKER_HOSTset too, which needed changed as it causes a code path to be ignored). Maybe it will help someone else, or point to what I'm doing wrong.I'm not quite sure if there is a bug or if I'm doing something wrong (our devcontainer should be setup as DIND, but is actually docker-from-docker which causes all manner of fun issues).
The key point in this is we have some code that detects if it is running in our devcontainer, and if so sets the
TC_HOSTenvironment variable to the gateway IP for the docker network that the devcontainer is running in. With this set testcontainers will correctly determine that the postgres container has started.alexanderankin commentedon Dec 12, 2023
the original links in the issue were from these two methods respectively
testcontainers-python/testcontainers/core/docker_client.py
Line 52 in 8119ccc
from
testcontainers-python/testcontainers/core/docker_client.py
Lines 50 to 54 in 8119ccc
and
testcontainers-python/testcontainers/core/docker_client.py
Line 48 in 8119ccc
from
testcontainers-python/testcontainers/core/docker_client.py
Lines 43 to 48 in 8119ccc
ivanychev commentedon Jan 22, 2024
Faced the same issue in the tests. The working workaround for me is to patch the current implementation of this method (
pytestexample)fix(core): DinD issues #141, #329 (#368)
chore(main): release testcontainers 4.2.0 (#484)
fix(core): DinD issues testcontainers#141, testcontainers#329 (testco…
chore(main): release testcontainers 4.2.0 (testcontainers#484)