gpt4 book ai didi

postgresql - 无法将主机名 "postgres"转换为地址 : Name or service not known

转载 作者:行者123 更新时间:2023-12-05 05:06:06 25 4
gpt4 key购买 nike

我正在尝试运行依赖于 postgresql 数据库的服务,但我不知道如何将两者链接在一起/连接。

我“依赖于”数据库,这似乎意味着我应该能够使用服务名称作为数据库主机名(即 due_database ),但每当我这样做时,我都会收到错误消息:

could not translate host name "due_database" to address: Name or service not known

这里有什么问题吗?

version: '3'
services:
due:
restart: always
build:
context: ./
dockerfile: ./docker/bot/Dockerfile
command: ./due
depends_on:
- due_database
due_database:
build:
context: ./
dockerfile: ./docker/database/Dockerfile
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD="docker"
entrypoint: /entry.sh
volumes:
- ./.dueutil_db:/dueutil_data

更新:

version: '2.1'
services:
due:
restart: always
build:
context: ./
dockerfile: ./docker/bot/Dockerfile
entrypoint: /entry.sh
command: ./due
depends_on:
due_database:
condition: service_healthy
environment:
- PGPASSWORD="docker"
due_database:
image: postgres:11
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD="docker"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
volumes:
- ./.dueutil_db:/var/lib/postgresql/data

我通过将我的创建数据库脚本移到 postgres 镜像之外解决了第一个问题,因为这与 postgres 入口点冲突。但是我仍然不知道如何从我的应用程序容器连接到数据库。

我目前正在尝试 psql -U postgres -h due_database postgres -f <sql script>但我刚刚得到:

psql: could not connect to server: Connection refused
Is the server running on host "due_database" (172.18.0.2) and accepting
TCP/IP connections on port 5432?

注意我确实看到了:psql: could not translate host name "somePostgres" to address: Name or service not known但这个问题似乎已经过时并且没有得到任何好的答案。

最佳答案

尝试使用network ,我不确定,但也许它可以帮助你:

version: '3'
# ----------------
# CREATE NEW NETWORK
networks:
app:
driver: bridge
# ----------------
services:
due:
restart: always
build:
context: ./
dockerfile: ./docker/bot/Dockerfile
command: ./due
depends_on:
- due_database
# ---------------
# SPECIFY NETWORK
networks:
- app
# ---------------

due_database:
build:
context: ./
dockerfile: ./docker/database/Dockerfile
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD="docker"
entrypoint: /entry.sh
volumes:
- ./.dueutil_db:/dueutil_data
# ---------------
# SPECIFY NETWORK
networks:
- app
# ---------------

关于postgresql - 无法将主机名 "postgres"转换为地址 : Name or service not known,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60132697/

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