testcontainers-python icon indicating copy to clipboard operation
testcontainers-python copied to clipboard

Docker in Docker on Mac requires to use host.docker.internal

Open timbmg opened this issue 4 years ago • 4 comments

I want to use a mongodb testcontainer within docker (to run in CI). When I use the testcontainer on the host, all works fine. However, when I use it inside docker, I need to access the testcontainer via the host (host.docker.internal). Is there some way for get_connection_url() to always return the "right" connection url?

I tried to put together an MWE:

docker run -v "/var/run/docker.sock":"/var/run/docker.sock"  --privileged=true -it --entrypoint /bin/bash python:3.7.6
>> pip install testcontainers pymongo -q
>> python
import pymongo
from testcontainers.mongodb import MongoDbContainer

mongo = MongoDbContainer()
mongo.start()
print(mongo.get_connection_url())
# > 'mongodb://test:[email protected]:53859'

testcontainer_client = mongo.get_connection_client()
for db in testcontainer_client.list_databases(): print(db)
# > results in pymongo.errors.ServerSelectionTimeoutError

mongo_client = pymongo.MongoClient( 'mongodb://test:[email protected]:53859')
for db in mongo_client.list_databases(): print(db)
# > {'name': 'admin', 'sizeOnDisk': 102400, 'empty': False}

timbmg avatar Nov 29 '21 20:11 timbmg

Running into the same issue on the macOS at least, but haven't yet tried in the actual CI. Looking at the source it looks like it's possible to pass TC_HOST environment variable and it's value will be returned from DockerClient.host() method.

But that means I have to initialise it with the gateway IP first in order to everything to work correctly.

inikolaev avatar May 16 '22 18:05 inikolaev

After much head-scratching trying to get things working with docker-desktop on Linux, I found this https://docs.docker.com/desktop/networking/

image

So using host.docker.internal to route container-to-container connections though the host machine is not limited to Mac, it will be required for anyone using Docker Desktop.

A possible solution would be to detect the docker context as docker-desktop for when to use host.docker.internal

razor-x avatar Dec 01 '22 23:12 razor-x

this is a huge blocker for people coming to testcontainers with a mac

mattfysh avatar Dec 14 '23 06:12 mattfysh

hi @mattfysh thanks for the note and sorry for the delays on answering! :)

I think we're missing a few labels to categorise the issue and see where the weaknesses of the project lie!

We've also made some effort to re-vitalise the project so it's easier to contribute. If you think you have an idea how to solve it (I didn't have the time myself yet) then please raise a PR and we can give you a review!

totallyzen avatar Feb 29 '24 15:02 totallyzen