3 つの docker コンテナー (postgresql、adminer、および go/migrate) があり、adminer ポートと postgres ポートの両方をホストに公開しました。ブラウザでadminerにアクセスでき、posticoもDBに接続できます。管理者内からデータベースに接続しようとすると、次のエラーがスローされます。
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP
connections on port 5432? could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
移行コンテナーもこのエラーをスローします。
error: dial tcp: 127.0.0.1:5432: connect: connection refused
したがって、コンテナが相互に通信できる方法に問題があることは明らかです。docker ネットワークを作成する必要がありますか?
version: '3.1'
services:
db:
image: postgres
environment:
POSTGRES_DB: mydbname
POSTGRES_USER: mydbuser
POSTGRES_PASSWORD: mydbpwd
ports:
- "5432:5432"
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mydbpwd@localhost:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
links:
- db
adminer:
image: adminer
restart: always
ports:
- "8081:8080"
depends_on:
- db