Docker- django在连接到postgres时出错:psycopg2.OperationalError:无法连接到服务器。连接被拒绝[英] Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused

本文是小编为大家收集整理的关于Docker- django在连接到postgres时出错:psycopg2.OperationalError:无法连接到服务器。连接被拒绝的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试扩展我的django-postgres应用程序. 我的Dockerfile是:

FROM python:3

ENV PYTHONUNBUFFERED 1

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

我的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

和我的设置.py具有以下数据库代码:

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

我的DB和Web容器都已经启动,但是当我运行时: Docker-Compose Run Web Python Manage.py Migrate 我遇到了错误:

psycopg2.erationalerror:无法连接到服务器:连接拒绝 服务器是在主机上运行的服务器" 0.0.0.0"并接受 端口5432上的TCP/IP连接?

如何使我的容器通信?

推荐答案

更改HOST to:

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

0.0.0.0不是有效的IP,在您需要使用service名称的情况下进行连接,因为compose将为您解析

本文地址:https://www.itbaoku.cn/post/1764092.html

问题描述

I am trying to dockerize my Django-postgres app. My Dockerfile is:

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:

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:

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:

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?

How can I make my containers communicate?

推荐答案

change HOST to:

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