简体   繁体   English

Docker compose,从另一个容器访问一个 Postgres 容器

[英]Docker compose, accessing a Postgres container from an other container

I have this really simple setup with a web app in one container and a Postgres service running in another container.我有一个非常简单的设置,一个容器中的 Web 应用程序和另一个容器中运行的 Postgres 服务。

I need to connect to the Postgres container and thought PGHOST="db" would point to that container ..?我需要连接到 Postgres 容器并认为PGHOST="db"会指向该容器..?

But I keep getting Error: getaddrinfo ENOTFOUND "db" at GetAddrInfoReqWrap.onlookup that I read as;但我一直Error: getaddrinfo ENOTFOUND "db" at GetAddrInfoReqWrap.onlookup收到Error: getaddrinfo ENOTFOUND "db" at GetAddrInfoReqWrap.onlookup ,我读为; can't find the "db" host ...找不到“db”主机...

What am I missing here?我在这里缺少什么?

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8081:3011"
    links:
      - db
    environment:
      - PGHOST="db"
      - PGDATABASE="testdb"
      - PGUSER="postgres"
      - PGPASSWORD="postgres"
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /usr/local/var/postgresql@13

Try this config.试试这个配置。 You don't need quotes when passing env variables.传递环境变量时不需要引号。 And it is better to use depends_on here to make sure DB is up and running before your app starts.最好在此处使用depends_on以确保在您的应用程序启动之前数据库已启动并正在运行。

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8081:3011"
    depends_on:
      - db
    environment:
      - PGHOST=db
      - PGDATABASE=testdb
      - PGUSER=postgres
      - PGPASSWORD=postgres
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - /usr/local/var/postgresql@13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM