2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

docker-compose DBの起動を待つ

Posted at

docker-compose時にDBの起動前に接続を試みてエラー。
なんでやねん…と調べた結果、公式にもある通り wait-for-it を使うとの事。そうなのね…
他にもやり方あるようですが、一旦これで解決したので、忘れないように備忘録として残します。
環境はdjango & postgrsql & React

backendのDockerfile

Dockerfile
FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt

wait-for-it取得と実行権限付与

curl -L https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -o wait-for-it.sh
chmod 755 wait-for-it.sh

docker-compose.yml

docker-compose-yml
version: "3"
services:
  db:
    image: postgres
    environment:
        - POSTGRES_DB=postgres
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
    ports:
        - 5432:5432

  backend:
    build: ./backend
    command: ["./wait-for-it.sh", "db:5432","--","python","manage.py","runserver", "0.0.0.0:8000"]
    volumes:
      - ./backend:/app
    depends_on:
      - db
    links:
      - db:db
    ports:
      - 8000:8000
  
  frontend:
    build: ./frontend
    volumes:
      - ./frontend:/app
    depends_on:
      - db
    ports:
      - 80:80

["./wait-for-it.sh", "db:5432",~] この部分。
これで5432ポートが開放されたらコマンド実行される。

djangoのsettings.py

settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

settings.pyも忘れずに書き換えとく。
これだけだけど、最初原因が分からず四苦八苦…
公式ちゃんと読みます。すみません。

参考記事

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?