前提
docker-composeでDjango開発環境を構築しています。
開発環境としてDjango+PostgresSQLの環境は構築することが出来たので次のステップとして本番環境用のDjango+Nginx+Gunicorn+PostgresSQLの環境を構築しています。
実現したいこと
docker-composeでDjango+Nginx+Gunicorn+PostgresSQLの環境を構築したい
発生している問題・エラーメッセージ
docker-compose up -d コマンド実行時に下記2つのエラーが発生しています。
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect gunicorn | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) gunicorn | psycopg2.OperationalError: could not connect to server: Connection refused gunicorn | Is the server running on host "db" (192.168.96.2) and accepting gunicorn | TCP/IP connections on port 5432?
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect gunicorn | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) gunicorn | django.db.utils.OperationalError: could not connect to server: Connection refused gunicorn | Is the server running on host "db" (192.168.96.2) and accepting gunicorn | TCP/IP connections on port 5432?
dokcer-compose.yml、Dockerfile、gunicorn.conf、requirements.txtはそれぞれ下記のとおりです。
該当のソースコード
dockercompose.yml
1# バージョン 2version: '3' 3 4# サービス 5services: 6 7 # データベース 8 db: 9 image: postgres:13 10 container_name: postgres 11 ports: 12 - 5432:5432 13 volumes: 14 - postgres:/var/lib/postgresql/data 15 environment: 16 POSTGRES_USER: postgres 17 POSTGRES_PASSWORD: postgres 18 POSTGRES_INITDB_ARGS: "--encoding=UTF-8" 19 POSTGRES_DB: mydb 20 hostname: postgres 21 22 # アプリケーションサーバー 23 gunicorn: 24 build: . 25 command: > 26 sh -c "python3 manage.py makemigrations && 27 python3 manage.py migrate && 28 python3 manage.py runserver 0.0.0.0:8000" 29 container_name: gunicorn 30 ports: 31 - "8000:8000" 32 volumes: 33 - .:/code 34 - gunicorn:/var/run/gunicorn 35 depends_on: 36 - db 37 38 # WEBサーバー 39 nginx: 40 image: nginx:1.17.7 41 container_name: nginx 42 ports: 43 - "8080:8080" 44 volumes: 45 - ./gunicorn.conf:/etc/nginx/conf.d/default.conf 46 - gunicorn:/var/run/gunicorn 47 depends_on: 48 - gunicorn 49 50# データボリューム 51volumes: 52 postgres: 53 gunicorn: 54 driver: local
Dockerfile
1FROM python:3.8 2 3ENV PYTHONUNBUFFERED 1 4 5RUN mkdir /code 6WORKDIR /code 7COPY requirements.txt ./ 8 9RUN pip install --upgrade pip 10RUN pip install -r requirements.txt 11 12RUN mkdir -p /var/run/gunicorn 13CMD ["gunicorn", "config.wsgi", "--bind=unix:/var/run/gunicorn/gunicorn.sock"] 14 15ADD . /code 16 17EXPOSE 8000
gunicorn.conf
1upstream gunicorn-django { 2 server unix:///var/run/gunicorn/gunicorn.sock; 3} 4 5server { 6 listen 8080; 7 server_name localhost; 8 9 location / { 10 try_files $uri @gunicorn; 11 } 12 13 location @gunicorn { 14 proxy_pass http://gunicorn-django; 15 } 16}
requirements.txt
1Django>=3.1,<3.2 2psycopg2>=2.8,<2.9 3gunicorn==20.1.0
Djangoのsetting.pyはDATABASEの項目だけを下記のように変更しました。
setting.py
1DATABASES = { 2 'default': { 3 'ENGINE': 'django.db.backends.postgresql_psycopg2', 4 'NAME': 'postgres', 5 'USER': 'postgres', 6 'PASSWORD': 'postgres', 7 'HOST': 'db', 8 'PORT': 5432, 9 } 10}
試したこと
dbに関するエラーだと思い、データベース名、ユーザー名、ホスト、パスワード、およびポートを見直したり、gunicornの設定を変えたりしてみましたが、どこに問題があるのかいまいち分からない状況です。
Django+PostgresSQLの環境では上手くいっていたので、追加したNginx+Gunicorn、おそらくgunicorn.conf なのか、、、分かる方いましたらよろしくお願いします。
回答1件