gpt4 book ai didi

python - pymongo.errors.ServerSelectionTimeoutError : localhost:27017: [Errno 111] Connection refused

转载 作者:可可西里 更新时间:2023-11-01 09:14:45 25 4
gpt4 key购买 nike

我正在尝试将 docker-compose 与我的 django-rest 应用程序一起使用。当我自己运行它时 python manage.py runserver 它运行良好。

如果我尝试使用 docker-compose sudo docker-compose up 它也会运行服务器,但是当我在浏览器中打开页面时出现错误。

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

我已经有了数据库,所以我只是在 settings.py

中使用这些行
MONGODB_DATABASES = {
"default": {
"name": 'api',
"host": 'localhost',
"port": 27017
},
}

这是我的 Dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY . /code
RUN pip install -r requirements.txt

我的 docker-compose.yml:

version: '3.0'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
mongo:
image: mongo

已经试过了:

Pymongo keeps refusing the connection at 27017

最佳答案

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

根据 docker-compose.yaml 文件,mongo 容器可通过 mongo:27017web 访问> 容器,因此 settings.py 应更改为:

MONGODB_DATABASES = {
"default": {
"name": 'api',
"host": 'mongo',
"port": 27017
},
}

关于python - pymongo.errors.ServerSelectionTimeoutError : localhost:27017: [Errno 111] Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49499438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com