さぁて、今日もDevContainerで遊ぶ(仕事する)かぁ
コレとかコレで作ったDevContainerのDockerコンテナーを起動して作業しようとしら。。。
# psql -U postgres
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
なんなんお前?1回だけしか使えんのか?
えっ、DevContainerって初回起動のときだけしかDBって使えないの?
マジでクソみたいなツールやんけ!!
って思ってた時期が俺にもありました。
原因
ポスグレのサービスが死んでた(おいおいおい、アイツ死んだわ。。。
コンテナを再起動した際にDockerDesktopで見たLogsに以下のような内容が記述されていた。
Container started
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
initdb: hint: If you want to create a new database system, either remove or empty the directory "/var/lib/postgresql/data" or run initdb with an argument other than "/var/lib/postgresql/data".
何なんコイツ、毎回initdbでDB作ろうとしてるんか?
/var/lib/postgresql/data は ボリュームマウントされているみたいなので、Dockerを止めても値が残るのは別にいいんだけど、それが有るおかげでinitdbでコケてサービスが立ち上がらないのがマジで納得いかない。。。
→コレが原因か分からんけど、とにかくサービスが起動していないのは間違いない
解決策
とりあえず、ポスグレのサービスが立ち上がっているかの確認コマンドをぶっ叩く
service --status-all
結果としては以下のような感じになる
vscode ➜ /workspaces/src $ service --status-all
[ - ] apache-htcacheclean
[ + ] apache2
[ - ] cron
[ - ] exim4
[ ? ] hwclock.sh
[ - ] postgresql
[ - ] procps
[ - ] rsync
[ - ] sudo
[ - ] sysstat
結果がハイフンになっている場合
サービスが死んでるという認識でいいと思われる。
以下コマンドでサービスを立ち上げる
service postgresql start
結果
コレでポスグレにアクセスが出来るようになったので、
とりあえずコレが解決策としてひとまずメモとして残す。
まとめ
たぶんdevcontainer.json
とかにちゃんと設定をすれば大丈夫だとは思うんだけど、現在そこまで調べる余裕がないので一旦コレでおしまい!!