かもメモ

自分の落ちた落とし穴に何度も落ちる人のメモ帳

Docker Postgres DB コンテナが起動しないにハマる

Django の docker 環境をクイックスタートのドキュメントを参考に作っていたけど DB にしていされていた Postgres のコンテナが起動しなかった。

docker-compose.yml

version: '3'
services:
  # 略
  db:
    image: postgres
    restart: always
    # 永続化
    volumes:
      - ./db/pgsql-data:/var/lib/pgsql

Postgres が起動しない

$ docker-compose up
db_1   | Error: Database is uninitialized and superuser password is not specified.
db_1   |        You must specify POSTGRES_PASSWORD to a non-empty value for the
db_1   |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
db_1   | 
db_1   |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
db_1   |        connections without a password. This is *not* recommended.
db_1   | 
db_1   |        See PostgreSQL documentation about "trust":
db_1   |        https://www.postgresql.org/docs/current/auth-trust.html

Postgres コンテナの起動にはパスワードの設定が必要になっていた

セキュリティ強化の一環でパスワードの設定が必須になっていたようです。

version: '3'
services:
  db:
    image: postgres
    restart: always
    # 永続化
    volumes:
      - ./db/pgsql-data:/var/lib/pgsql
+   environment:
+     POSTGRES_PASSWORD: password

POSTGRES_HOST_AUTH_METHOD=trust を設定すれば以前の通りパスワードなしでも使用できる

If you know that you want to be insecure (i.e. any one can connect without a password from anywhere), then POSTGRES_HOST_AUTH_METHOD=trust is how you opt in to that.

We really recommend setting a password and it was really a bad decision by me in the beginning to continue to allow running the database without a password (#31 (comment) & #36)
cf. Behaviour change between 9.5.20 and 9.5.21? · Issue #681 · docker-library/postgres · GitHub

version: '3'
services:
  db:
    image: postgres
    restart: always
    # 永続化
    volumes:
      - ./db/pgsql-data:/var/lib/pgsql
+   environment:
+     POSTGRES_HOST_AUTH_METHOD: trust

これでも OK っぽい。セキュリティの強化と言う意味ではパスワードを設定するほうが望ましそう。

所感

docker-compose のイメージはちゃんとバージョン指定しよう!

おわり


[参考]