从另一个容器(Docker)连接到postgresql容器
我正在尝试按照本教程设置一个postgresql容器。
我有以下脚本:
#!/bin/bash # wait-for-postgres.sh set -e host="$1" shift cmd="$@" until psql -h "$host" -U "postgres" -c '\l'; do >&2 echo "Postgres is unavailable - sleeping" sleep 1 done >&2 echo "Postgres is up - executing command" exec $cmd
而下面docker-compose.yml
:
version: '2' services: server: build: . ports: - 3030:3030 depends_on: - database command: ["./setup/wait-for-postgres.sh", "localhost:5432", "--", "node", "src"] database: image: postgres environment: - "POSTGRES_USER=postgres" - "POSTGRES_PASSWORD=postgres" - "POSTGRES_DB=tide_server" ports: - 5432:5432
问题是,当我运行docker-compose up
,出现以下错误:
server_1 | Postgres is unavailable - sleeping server_1 | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi ce not known server_1 | Postgres is unavailable - sleeping server_1 | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi ce not known server_1 | Postgres is unavailable - sleeping server_1 | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi ce not known
现在我已经尝试将主机设置为database
, localhost
, 0.0.0.0
,甚至是容器IP,但没有任何工作,我不知道应该是什么或如何debugging它,我不是100%确定如何docker-compose
链接容器。
不要使用depends_on。 尝试与“链接”
version: '2' services: server: build: . ports: - 3030:3030 links: - database #environment could be usefull too environment: DATABASE_HOST: database command: ["./setup/wait-for-postgres.sh", "localhost:5432", "--", "node", "src"] database: image: postgres environment: - "POSTGRES_USER=postgres" - "POSTGRES_PASSWORD=postgres" - "POSTGRES_DB=tide_server" ports: - 5432:5432