Docker in Docker on Mac requires to use host.docker.internal
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}
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.
After much head-scratching trying to get things working with docker-desktop on Linux, I found this https://docs.docker.com/desktop/networking/
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
this is a huge blocker for people coming to testcontainers with a mac
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!