python - 使用另一个容器连接到 MongoDB docker 容器的问题
问题描述
将 docker 容器连接到另一个容器时出现问题。我可以在容器外部本地连接到它,但从容器内部不起作用。这是堆栈跟踪
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 5f9cf7b6d9d395e79548d42a, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
我的 docker-compose.yml 文件非常简单明了。没有什么独特的
mongod:
restart: always
image: mongo:latest
volumes:
- ./mongodb/mongod.conf:/etc/mongod.conf
ports:
- "27017:27017"
command: mongod
app:
build: ./app
container_name: django-gunicorn
restart: always
env_file:
- ./app/django.env
ports:
- "8000:8000"
command:
"gunicorn --workers=2 --bind=0.0.0.0:8000 webapp.wsgi:application"
我已在 mongod.conf 中将端口绑定到 0.0.0.0。我还缺少什么?
解决方案
您正在尝试连接localhost
,但 mongodb 容器的主机名将是docker-compose.yml
文件中给出的服务名称
更新要使用的连接参数
host="mongod",# name of service in compose file
推荐阅读
- android - 将 .aar 库模块日志包含到 Android 应用程序文件记录器中?
- android - "No view found for id for fragment" error of Nested Viewpager
- go - 随着请求数量的增加,Go 网络服务器的性能急剧下降
- reactjs - How to pull an SVG linearGradient id from a map function
- javascript - How to enable Froala editor inside the wrapper div?
- xaml - 如何使用 xamarin.forms 中选择的选项设置主详细信息页面?
- java - “一个组件需要一个类型的 bean”,但是哪一个呢?
- xslt - XSLT - 创建新元素和值
- reactjs - Why my react application not displaying on google chrome browser but it is displayed on chrome ingonito , IE, and other browsers
- python - 在Python中将字符串解码为日文字符