Docker Compose with Django and Postgres Fails with "django.db.utils.OperationalError: could not connect to server:"

huangapple go评论69阅读模式
英文:

Docker Compose with Django and Postgres Fails with "django.db.utils.OperationalError: could not connect to server:"

问题

我正在尝试使用Docker Compose运行我的Django/PostgreSQL应用程序。当我运行 docker compose up -d 时,我在运行在端口5432上的PostgreSQL容器上得到以下日志:

2023-02-18 00:10:25.049 UTC [1] LOG:  starting PostgreSQL 13.8 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
2023-02-18 00:10:25.049 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-02-18 00:10:25.049 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-02-18 00:10:25.052 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-02-18 00:10:25.054 UTC [22] LOG:  database system was shut down at 2023-02-18 00:10:06 UTC
2023-02-18 00:10:25.056 UTC [1] LOG:  database system is ready to accept connections

看起来我的PostgreSQL容器正常工作。然而,我的Python/Django容器有以下日志:

django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

docker-compose.yml

version: '3.8'
services:
  web:
    build: .
    command: sh -c "python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    env_file:
      - ./xi/.env
    depends_on:
      - db
  db:
    image: postgres:13-alpine
    environment:
      - POSTGRES_DB=***
      - POSTGRES_USER=***
      - POSTGRES_PASSWORD=***
    volumes:
      - dev-db-data:/var/lib/postgresql/data
    ports:
      - 5432:5432
volumes:
  dev-db-data:

Dockerfile:

FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code

# install psycopg2 dependencies
RUN apt-get update
RUN apt-get install nano python3-dev libpq-dev -y

COPY requirements-prod.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

我必须遗漏了一些小细节,这样Python容器可以与PostgreSQL容器通信。

此外,还有一些附加问题:

"listening on IPv4 address '0.0.0.0', port 5432" 是什么意思?根据我的理解,0.0.0.0包括所有IP地址,包括127.0.0.1。因此,在这种情况下,这不应该是一个问题(如果我错了,请纠正我)。

我已经为此挣扎了几天。我已经按照入门文档以及Docker文档上的Python使用指南的步骤进行操作,看起来我理解了一切,但我无法有效地调试我的容器。我可以获得哪些额外的辅助知识,以帮助我像调试Python脚本一样自如地调试容器?

我尝试了一些事情:

  • 用硬编码的凭据替换了env_file
  • 将python更改为python3
  • 删除sh -c
  • 我尝试首先构建数据库,然后用 docker-compose up -d --build web 构建我的Web应用程序,问题仍然存在。

我已经尝试了所有与环境变量有关的事情,似乎不是凭据不正确的问题。在没有Docker的情况下运行 python manage.py runserver 时,它成功连接到数据库。有一些类似的堆栈溢出问题,但我尝试了它们的解决方案,它们都无法解决我的问题。我的问题部分原因是我不知道要尝试什么以及如何有效地调试Docker容器(正如上面的问题所提到的)。

请注意: 由于您要求不提供回答,我只提供了翻译和部分解释,没有回答您的问题。如果您需要进一步的帮助,请随时提出更多问题。

英文:

I am trying to run my django/postgres application with docker compose. When I run docker compose up -d I get the the following logs on my postgres container running on port 5432:

2023-02-18 00:10:25.049 UTC [1] LOG:  starting PostgreSQL 13.8 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
2023-02-18 00:10:25.049 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-02-18 00:10:25.049 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-02-18 00:10:25.052 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-02-18 00:10:25.054 UTC [22] LOG:  database system was shut down at 2023-02-18 00:10:06 UTC
2023-02-18 00:10:25.056 UTC [1] LOG:  database system is ready to accept connections

It appears my postgres container is working properly. However my python/django container has the following logs:

django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

docker-compose.yml

version: '3.8'
services:
  web:
    build: .
    command: sh -c "python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    env_file:
      - ./xi/.env
    depends_on:
      - db
  db:
    image: postgres:13-alpine
    environment:
      - POSTGRES_DB=***
      - POSTGRES_USER=***
      - POSTGRES_PASSWORD=***
    volumes:
      - dev-db-data:/var/lib/postgresql/data
    ports:
      - 5432:5432
volumes:
  dev-db-data:

Dockerfile:

FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code

# install psycopg2 dependencies
RUN apt-get update
RUN apt-get install nano python3-dev libpq-dev -y

COPY requirements-prod.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

I must be missing something small that allows the python container to communicate with the postgres container.

Also a few additional questions:

What does it mean that the container is "listening on IPv4 address '0.0.0.0', port 5432"? To my understanding 0.0.0.0 encapsulates all ip addresses including 127.0.0.1. So, in this case that shouldn't be an issue (correct me if I'm wrong)

I have been struggling with this for a few days. I have followed the getting started docs as well as the python usage guides on the docker docs, and it appears that I feel that I understand everything, but I am unable to debug my containers efficiently. What additional supplemental knowledge can I acquire that helps me debug a container with the same level of comfortability as I would a python script?

I tried a few things:

  • swapping env_file with the credentials hard coded in
  • changing python with python3
  • removing sh -c
  • I tried building my database first with docker-compose up -d --build db and then building my web app with docker-compose up -d --build web and the issue persisted.

I tried everything with the environment variables and it appears improper credentials is not the issue. Running python manage.py runserver without docker it successfully connects to the database. There are some similar stack overflow questions, but I have tried their solutions and they do not work.
Part of my issue is I don't know what to try and how to efficiently debug docker containers yet (hence the question above).

答案1

得分: 0

你在settings.py文件的DATABASES['default']中设置的HOST变量是什么?如果是'127.0.0.1',尝试更改为'db'以匹配容器服务名称。

英文:

What have you set as your HOST variable in DATABASES['default'] in settings.py? If it's '127.0.0.1', try changing to 'db' to match the container service name.

huangapple
  • 本文由 发表于 2023年2月18日 21:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定