简体   繁体   English

无法将主机名“postgres”转换为地址:名称或服务未知

[英]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.

相关问题 无法将主机名“postgres”转换为地址:名称或服务未知 - DOCKER - Could not translate host name "postgres" to address: Name or service not known - DOCKER Rails 无法将主机名“postgres”转换为地址:名称或服务未知 (PG::ConnectionBad) - Rails could not translate host name “postgres” to address: Name or service not known (PG::ConnectionBad) Heroku上偶尔出现Postgres错误:无法将主机名“<pg URL>”转换为地址:名称或服务未知(PG ::错误) - Occasional Postgres error on Heroku: could not translate host name “<pg URL>” to address: Name or service not known (PG::Error) Docker 图像未运行,数据库错误:django.db.utils.OperationalError 无法将主机名“postgres”转换为地址:名称或服务未知 - Docker image not running, db error: django.db.utils.OperationalError could not translate host name "postgres" to address: Name or service not known Docker-compose with django 无法将主机名“db”转换为地址:名称或服务未知 - Docker-compose with django could not translate host name “db” to address: Name or service not known Django Docker:django.db.utils.OperationalError:无法将主机名“db”转换为地址:名称或服务未知 - Django Docker: django.db.utils.OperationalError: could not translate host name “db” to address: Name or service not known Docker 运行,在 docker-compose 构建后无法将主机名“db”转换为地址:名称或服务未知 - Docker run, after docker-compose build could not translate host name “db” to address: Name or service not known ubuntu 19.04上的“无法将主机名“ db”转换为地址:名称或服务未知)(由于升级?) - “could not translate host name ”db“ to address: Name or service not known” on ubuntu 19.04 (due to upgrade?) 无法将主机名“postgres”转换为 github 操作上的地址 - Could not translate host name "postgres" to address on github actions pg_dump:[archiver (db)] 与数据库“root”的连接失败无法将主机名“-U”转换为地址:名称或服务未知 - pg_dump: [archiver (db)] connection to database “root” failed could not translate host name “-U” to address: Name or service not known
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM