下記を参考にDockerでDjangoの環境構築するかーと思ったところ少し詰まったのでメモ程度に残します。
https://docs.docker.jp/compose/django.html
docker-compose up -d でコンテナ立ち上げ用としたところDBのコンテナだけうまく立ち上がらず...
toruchan:~/work/py-work$ docker-compose up -d
Starting pywork_db_1 ...
Starting pywork_db_1 ... done
Creating pywork_web_1 ...
Creating pywork_web_1 ... done
toruchan:~/work/py-work$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------
pywork_db_1 docker-entrypoint.sh postgres Exit 1
pywork_web_1 python3 manage.py runserve ... Up 0.0.0.0:8000->8000/tcp
toruchan:~/work/py-work$ docker-compose logs
Attaching to pywork_web_1, pywork_db_1
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
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
どうやらパスワードの設定がされていないとエラーになるらしい...?
そこで思いついたのは下記2つ
- パスワード設定を無効化
- パスワードを設定
とりあえずローカル環境で立ち上げたいだけだし1のパスワード無効化を選択
1の場合の対応方法としてdocker-compose.ymlファイルにPOSTGRES_HOST_AUTH_METHOD: 'trust'
の一行を追記
全体としてはこんな感じに
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_HOST_AUTH_METHOD: 'trust'
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
これでコンテナ立ち上げ直したらいけた
後ほどパスワードはちゃんと設定しようと思う...