[英]could not translate host name "postgres" to address: Name or service not known
I'm trying to run a service that depends on a postgresql database but I can't figure out how to link the two together / connect.我正在尝试运行依赖于 postgresql 数据库的服务,但我不知道如何将两者链接在一起/连接。
I'm 'depending on' the database which seems to mean I should be able to use the service name as the database host name (ie due_database
) but whenever I do I just get the error:我“依赖”数据库,这似乎意味着我应该能够使用服务名称作为数据库主机名(即
due_database
),但是每当我这样做时,我都会收到错误消息:
could not translate host name "due_database" to address: Name or service not known
What's wrong here?这里有什么问题?
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
Update :更新:
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
I fixed the first issue by moving my create database scripts outside the postgres image, since that conflicts with the postgres entrypoint.我通过将我的创建数据库脚本移到 postgres 图像之外来解决第一个问题,因为这与 postgres 入口点冲突。 However I still can't figure out how to connect to the database from my app's container.
但是我仍然无法弄清楚如何从我的应用程序的容器连接到数据库。
I'm currently trying psql -U postgres -h due_database postgres -f <sql script>
but I'm just getting:我目前正在尝试
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?
Note I did see: psql: could not translate host name "somePostgres" to address: Name or service not known but the question seems outdated & did not get any good answers.注意我确实看到了: psql: could not translate host name "somePostgres" to address: Name or service not known但问题似乎已经过时并且没有得到任何好的答案。
Try to use network , I'm not sure but maybe it can help you :尝试使用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
# ---------------
So my first issue was trying to make changes to the PostgreSQL image, copy some scripts in and add a custom entry point.所以我的第一个问题是尝试更改 PostgreSQL 映像,复制一些脚本并添加自定义入口点。 That overrides the postgres startup script & means the database fails to load.
这会覆盖 postgres 启动脚本并意味着数据库无法加载。
The second issue was using the non-alpine postgres image.第二个问题是使用非高山 postgres 图像。 Switching to
postgres:12.1-apline
'just worked'.切换到
postgres:12.1-apline
'刚刚工作'。 I'm not sure what's wrong with the other image though.不过,我不确定另一张图片有什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.