gpt4 book ai didi

postgresql - postgres 容器抛出 "forward host lookup failed: Unknown host"

转载 作者:行者123 更新时间:2023-12-02 19:55:44 32 4
gpt4 key购买 nike

我正在使用以 Python3.6 为基础的 docker 镜像。突然间它开始崩溃(启动后立即退出)。所以我冲进了容器,发现它崩溃是因为与容器化 postgres 数据库的连接突然失败。我设法得到的唯一错误输出是 forward host lookup failed: Unknown host 这并没有告诉我太多信息。

入口点.sh:

echo "Waiting for postgres..."

while ! nc -z users-db 5432; do
sleep 0.1
done

echo "PostgreSQL started"

python manage.py run -h 0.0.0.0

错误输出:

Waiting for postgres...
users-db: forward host lookup failed: Unknown host
users-db: forward host lookup failed: Unknown host
users-db: forward host lookup failed: Unknown host
...
...

docker 文件:

FROM python:3.6.9-slim

LABEL maintainer="abc"

RUN apt-get update && \
apt-get install -y netcat && \
apt-get clean

WORKDIR /usr/src/app

COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh


COPY . /usr/src/app

CMD ["/usr/src/app/entrypoint.sh"]

令我印象深刻的是,直到现在它都运行良好,并且在我没有对数据库容器进行任何更改的情况下连接失败。

我该怎么做才能解决这个问题?如果您需要查看任何文件,请直接询问,我会在此处分享。

docker ps:

72d344cc61bf        tdd_nginx           "nginx -g 'daemon of…"   25 minutes ago      Restarting (1) 55 seconds ago                            tdd_nginx_1
8ee2f8082e69 tdd_client "npm start" 26 minutes ago Up 25 minutes 0.0.0.0:3007->3000/tcp tdd_client_1
1ccfc3ca5600 tdd_users-db "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 0.0.0.0:5435->5432/tcp tdd_users-db_1
--> 62af29277b78 tdd_users "/bin/bash -s" 22 minutes ago Exited (130) 2 minutes ago # <-- keeps crashing

docker-compose 文件:

version: '3.7'

services:
users:
build:
context: ./services/users
dockerfile: Dockerfile
volumes:
- './services/users:/usr/src/app'
ports:
- 5001:5000
environment:
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev
- DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test
- SECRET_KEY=bart_simpson
depends_on:
- users-db

client:
build:
context: ./services/client
dockerfile: Dockerfile
volumes:
- './services/client:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- 3007:3000
environment:
- NODE_ENV=development
- REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
depends_on:
- users

nginx:
build:
context: ./services/nginx
dockerfile: Dockerfile
restart: always
ports:
- 80:80
depends_on:
- users
- client

users-db:
build:
context: './services/users/project/db'
dockerfile: Dockerfile
ports:
- 5435:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres

最佳答案

问题是我安装了用于生成网络 token 的 pyjwt 库,并且我在代码中使用了 pyjwt.encode() 而不是 jwt.encode()。这对容器之间的连接功能产生了重大影响。仍然不知道为什么。容器现在再次运行。如果有人投票结束这个话题,我会理解,因为没有人会猜到这一点。

关于postgresql - postgres 容器抛出 "forward host lookup failed: Unknown host",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58189480/

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