问题
$ 去运行 cmd/syndicate/main.go 2021/01/25 16:37:25 错误连接到数据库:拨号 tcp:查找数据库:没有这样的主机
尝试运行时无法连接到数据库:
$ go run cmd/syndicate/main.go
2021/01/25 16:37:25 error connecting to database: dial tcp: lookup db: no such host
&
$ migrate -source file://migrations -database postgres://postgres:secret@db:5432/syndicate?sslmode=disable up
error: dial tcp: lookup db on [2001:558:feed::1]:53: no such host
这两个命令有什么共同点?... 数据库 URL。我几乎可以肯定我的数据库 URL 不正确。
我已验证我的 postgres 容器正在运行:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e578bf646c7 adminer "entrypoint.sh docke…" 3 days ago Up 3 days 0.0.0.0:8080->8080/tcp syndicate_adminer_1
729fc179aa6f postgres "docker-entrypoint.s…" 3 days ago Up 3 days 5432/tcp syndicate_db_1
这里是我可能会忽略某些东西的地方...
$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
syndicate_adminer_1 entrypoint.sh docker-php-e ... Up 0.0.0.0:8080->8080/tcp
syndicate_db_1 docker-entrypoint.sh postgres Up 5432/tcp
5432/tcp
???我看到我的管理员容器清楚地映射到我的本地端口 (0.0.0.0:8080->8080/tcp
),但是我的 postgres 容器只显示 5432/tcp
(而不是 0.0.0.0:5432->5432/tcp
)
这是我的 docker-compose.yml:
version: "3.8"
services:
db:
image: postgres
environment:
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
migrate:
image: migrate/migrate
volumes:
- ./migrations:/migrations
depends_on:
- db
command: -source=file://migrations -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@db:5432/$POSTGRES_DB?sslmode=disable up
adminer:
image: adminer
restart: always
ports:
- "8080:8080"
environment:
ADMINER_DEFAULT_SERVER: db
depends_on:
- db
port: "5432:5432"
servicddb
变量
Browse my repository at this time in history
谢谢! 康纳
答案 0 :(得分:0)
添加到数据库服务
ports:
- "5432:5432"