詰まったので知見として残しておきます
Supabaseコンテナの起動
% supabase start
Seeding data supabase/seed.sql...
Started supabase local development setup.
API URL: http://localhost:54321
GraphQL URL: http://localhost:54321/graphql/v1
DB URL: postgresql://postgres:postgres@localhost:54322/postgres
Studio URL: http://localhost:54323
Inbucket URL: http://localhost:54324
ホストからはpostgresql://postgres:postgres@localhost:54322/postgres
でDBにアクセスできる
しかし,NextコンテナからDBにアクセスするにはSupabaseコンテナと同一ネットワークに配置させる必要がある
supabase start
するとSupabaseコンテナはsupabase_network_<project_name>
というネットワークに割り当てられる
このネットワークにdocker-compose
でNextコンテナを所属させる
docker-compose.yml
略
services:
next:
略
networks:
- supabase_network_<project_name>
networks:
supabase_network_<project_name>:
external: true
疎通確認
コンテナ間の通信ではipアドレスではなくDockerホスト名を指定する
% docker compose exec next /bin/sh
/app # ping -c 3 supabase_db_<project_name>
PING supabase_db_<project_name> (192.168.128.2): 56 data bytes
64 bytes from 192.168.128.2: seq=0 ttl=64 time=0.560 ms
64 bytes from 192.168.128.2: seq=1 ttl=64 time=0.139 ms
64 bytes from 192.168.128.2: seq=2 ttl=64 time=0.198 ms
--- supabase_db_<project_name> ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.139/0.299/0.560 ms
Postgresへの接続
SupabaseのDBコンテナは54322:5432
でフォワードされているので,コンテナの5432番にアクセス
# psql 'postgresql://postgres:postgres@supabase_db_<project_name>:5432/postgres'
psql (15.2 (Debian 15.2-1.pgdg110+1), server 15.1 (Ubuntu 15.1-1.pgdg20.04+1))
Type "help" for help.
postgres=>
54322番ではなく5432番にアクセスすることに注意.NextコンテナでDATABASE_URLみたいなものを設定するときも5432番を指定する
完了