简体   繁体   English

Docker- django 在连接到 postgres 时抛出错误:psycopg2.OperationalError:无法连接到服务器:连接被拒绝

[英]Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused

I am trying to dockerize my Django-postgres app.我正在尝试 dockerize 我的 Django-postgres 应用程序。 My Dockerfile is:我的 Dockerfile 是:

FROM python:3

ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

My docker-compose.yml is:我的 docker-compose.yml 是:

version: '3'

services:
  web:
    build: .
    command: python app/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: password

and my settings.py has the following database code:我的 settings.py 有以下数据库代码:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'password',
        'HOST': '0.0.0.0',
        'PORT': '5432',
    }
}

My db and web containers are up but when I run: docker-compose run web python manage.py migrate I am getting the error:我的 db 和 web 容器已启动,但是当我运行时: docker-compose 运行 web Z23EEEB4347BDD26BDD26B7pyEE9A出现错误:5DDFC6B7pyEE9A3

psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "0.0.0.0" and accepting TCP/IP connections on port 5432? psycopg2.OperationalError:无法连接到服务器:连接被拒绝服务器是否在主机“0.0.0.0”上运行并接受端口 5432 上的 TCP/IP 连接?

How can I make my containers communicate?如何让我的容器进行通信?

change HOST to:HOST更改为:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': '5432',
    }
}

0.0.0.0 is not a valid IP , beside you need to connect using the service name since compose will resolve it for you 0.0.0.0不是有效的IP ,此外您需要使用service名称进行连接,因为compose会为您解析

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (psycopg2.OperationalError)无法连接到服务器:连接被拒绝是服务器 - (psycopg2.OperationalError) could not connect to server: Connection refused Is the server Docker/Django 应用程序中的 Postgres 不起作用 - OperationalError:无法连接到服务器:连接被拒绝 - Postgres in Docker/ Django app does not work - OperationalError: could not connect to server: Connection refused Docker Django 无法连接到服务器:连接被拒绝 - Docker Django could not connect to server: Connection refused Heroku + Postgres:psycopg2.OperationalError:无法翻译主机名“无” - Heroku + Postgres: psycopg2.OperationalError: could not translate host name “None” Django psycopg2.OperationalError:fe_sendauth错误,但未连接到PostgreSQL数据库 - Django psycopg2.OperationalError: fe_sendauth error but not connecting to postgresql database (psycopg2.OperationalError)SSL SYSCALL错误:软件导致连接中止 - (psycopg2.OperationalError) SSL SYSCALL error: Software caused connection abort 流浪者上的Django,psycopg2 OperationalError:无法连接到服务器:没有这样的文件或目录 - Django on vagrant, psycopg2 OperationalError: could not connect to server: No such file or directory 得到 django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - Got django.db.utils.OperationalError: could not connect to server: Connection refused psycopg2.OperationalError:无法使用 psycopg2 和 docker 将主机名转换为地址 - psycopg2.OperationalError: could not translate host name to address using psycopg2 and docker psycopg2.OperationalError 错误 - psycopg2.OperationalError errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM