0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails/Docker】container is unhealthy で起動できない時の対処法(PostgreSQL 18 の仕様変更)

Posted at

はじめに

WEBエンジニア転職を目指している初学者です!
Docker環境、Railsで開発している中、

docker compose upのコマンドを叩いたら

Gracefully stopping... (press Ctrl+C again to force)

dependency failed to start: container xxxxxxx is unhealthy

のようなエラーが起きて焦ったため、理由と対処法をまとめます!

エラーの意味

Gracefully stopping... (press Ctrl+C again to force)

dependency failed to start: container xxxxxxx is unhealthy

これは、DBコンテナの「ヘルスチェック(Healthcheck)」が失敗し、依存しているRailsコンテナが起動できなくなった状態です。

PostgreSQL 18以降では、データの保存先ルールが変わったため、古いデータが入っているディレクトリを見つけると「このままだと壊れる可能性があるから起動しないよ」と安全装置が働いています。

対処法

PstgreSQL 17以前は

    volumes:
      - postgresql_data:/var/lib/postgresql/data

でしたが、
PstgreSQL 18では

    volumes:
      - postgresql_data:/var/lib/postgresql

と記述します。

マウント先(ymlの書き方)を変えるだけでは、古い中途半端なディレクトリがコンテナ内に残っていて、同じエラーが続くことがあるため、一度ボリュームをリセットして立ち上げ直す必要があります。 (※開発環境のDBデータは消えるので注意してください!)

docker compose down -v
docker compose up

 
仕様変更といえばそれまでですが、
例えるなら、今までの「ピンポイント指定(17)」から「お任せ(18)」に変わった、というイメージですね。

項目 PostgreSQL 17 以前 PostgreSQL 18 以降
コンテナ内のデータ場所 /var/lib/postgresql/data /var/lib/postgresql/18/data (など)
Dockerでのマウント先 /var/lib/postgresql/data /var/lib/postgresql
サブディレクトリ作成 なし(直接その場所に置く) あり(バージョン名で自動作成)

参考文書

まとめ

エラーが出た時はびっくりしましたが、postgresql_data:/var/lib/postgresqlで自動で上手いことやってくれるので楽ちんですね!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?