我已经创建了一个Postgres数据库,该数据库在5432端口上本地运行。数据库位于Docker容器中。当我试图将这个数据库连接到SpiceDB (另一个Docker容器)时,我会收到错误消息。
"error":"failed to create datastore: unable to instantiate datastore:
failed to connect to `host=localhost user=postgres database=spicedb`:
dial error (dial tcp [::1]:5432: connect: cannot assign requested address)",
"time":"2022-05-18T07:48:10Z","message":
"terminated with errors"
该数据库名为"spicedb“,没有任何表。对于如何正确连接SpiceDB和Postgres,有什么想法吗?
docker run --name spicedb -p 50051:50051 \
--rm authzed/spicedb serve
--grpc-preshared-key "<my-key>" \
--datastore-engine=postgres \
--datastore-conn-uri="postgres://postgres:<db-password>@localhost:5432/spicedb?sslmode=disable"
{"level":"info","new level":"info","time":"2022-05-18T07:48:10Z","message":"set log level"}
{"level":"info","new provider":"none","time":"2022-05-18T07:48:10Z","message":"set tracing provider"}
{"level":"warn","version":"1.7.1","time":"2022-05-18T07:48:10Z","message":"not running a released version of SpiceDB"}
{"level":"info","time":"2022-05-18T07:48:10Z","message":"using postgres datastore engine"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","error":"failed to create datastore: unable to instantiate datastore: failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)","time":"2022-05-18T07:48:10Z","message":"terminated with errors"}
发布于 2022-05-18 13:58:14
如果localhost
位于两个不同的容器中,则无法使用它们作为主机名。您应该使用用户定义的网桥网络,并按容器名对postgres实例进行寻址。
来自Docker的文档在这里:https://docs.docker.com/network/bridge/
创建桥梁网络:
docker network create spicedb-net
使用容器名称在网络上运行postgres:
docker run --name spicedb-datastore \
--network spicedb-net
-e POSTGRES_PASSWORD=<db-password> \
-d postgres
运行SpiceDB并使用您的容器名作为postgres的主机名。
docker run --name spicedb -p 50051:50051 \
--network spicedb-net
--rm authzed/spicedb serve
--grpc-preshared-key "<my-key>" \
--datastore-engine=postgres \
--datastore-conn-uri="postgres://postgres:<db-password>@spicedb-datastore:5432/spicedb?sslmode=disable"
https://stackoverflow.com/questions/72285637
复制相似问题