Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
timbmg opened this issue Nov 29, 2021 · 4 comments
Open

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

timbmg opened this issue Nov 29, 2021 · 4 comments

Comments

@timbmg
Copy link
Collaborator

timbmg commented Nov 29, 2021

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}
@inikolaev
Copy link

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.

@razor-x
Copy link

razor-x commented Dec 1, 2022

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

@mattfysh
Copy link

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

@totallyzen
Copy link
Collaborator

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants