testcontainers / testcontainers/testcontainers-python

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

Open
#159 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

👀 requires attention 📦 package: core
Dominant language
Python
Stars
2.3k
Forks
386
Avg merge
4h 40m
Merged PRs (30d)
1

Description

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:test@172.17.0.1: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:test@host.docker.internal:53859')
for db in mongo_client.list_databases(): print(db)
# > {'name': 'admin', 'sizeOnDisk': 102400, 'empty': False}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing MongoDbContainer.get_connection_url() and get_connection_client(), then inspect how the Docker host address is determined in the shown host and Docker-in-Docker cases. Reproduce the MongoDB example from the issue and verify that the returned URL and client work both on the host and inside Docker.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, mongodb, python
Domain
databases, devops, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.