testcontainers / testcontainers/testcontainers-python
Docker in Docker on Mac requires to use host.docker.internal
未关闭
还没有人认领这个 Issue。
👀 requires attention
📦 package: core
- 主要语言
- Python
- 星标
- 2.3k
- 派生
- 386
- 平均合并
- 4 小时 40 分钟
- 30 天内合并 PR
- 1
描述
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}
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先跟踪 MongoDbContainer.get_connection_url() 和 get_connection_client(),然后检查在所示的主机和 Docker-in-Docker 场景中如何确定 Docker 主机地址。复现 issue 中的 MongoDB 示例,并验证返回的 URL 和客户端在主机上以及 Docker 内部都能正常工作。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- docker, mongodb, python
- 领域
- databases, devops, networking
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100