前提・実現したいこと
Dockerを使ってDjango+uwsgi+nginxでアプリを作ろうとしています。バックエンドが出来次第、ここにVueのコンテナを追加しようと考えているので大まかな構成は変えないでいようと思っています。
- 参考にしたもの
- ファイル構成
app ├─back │ └─src │ ├─config--②uwsgi.ini │ ├─main │ │ ├─__pycache__ │ │ ├─__init__.py │ │ ├─asgi.py │ │ ├─⑥settings.py │ │ ├─urls.py │ │ └─wsgi.py │ ├─manage.py │ ├─static │ ├─requirements.txt │ └─③Dockerfile ├─nginx │ ├─conf--④nginx.conf │ └─⑤uwsgi_params ├─postgres │ └─dbdata └─①docker-compose.yaml
- ①docker-compose.yaml
version: '3.7' volumes: pgdata: driver_opts: type: none device: ./postgres/dbdata o: bind services: nginx: image: nginx container_name: container.nginx ports: - "8000:8000" restart: unless-stopped volumes: - ./nginx/conf:/etc/nginx/conf.d - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params - ./back/src/static:/static - ./nginx/log:/var/log/nginx depends_on: - back networks: - django_net back: build: context: ./back dockerfile: Dockerfile restart: unless-stopped container_name: container.uwsgi command: uwsgi --ini /code/config/uwsgi.ini volumes: - ./back/src:/code expose: - "8001" depends_on: - db networks: - django_net db: image: postgres restart: always container_name: container.postgres ports: - "5432:5432" environment: POSTGRES_DB: "postgresdb" POSTGRES_USER: "admin" POSTGRES_PASSWORD: "test" POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --locale=C" volumes: - ./postgres/dbdata:/var/lib/postgresql/data hostname: postgres networks: - django_net networks: django_net: driver: bridge
- ②uwsgi.ini
[uwsgi] socket = :8001 module = main.wsgi wsgi-file = /code/main/wsgi.py logto = /code/config/uwsgi.log py-autoreload = 1
- ③Dockerfile(Django)
FROM python:3.7 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code COPY requirements.txt /code/ RUN pip install --upgrade pip RUN pip install -r requirements.txt COPY . /code/
- ④nginx.conf
# the upstream component nginx needs to connect to upstream django { ip_hash; server web:8001; } # configuration of the server server { # the port your site will be served on listen 8000; # the domain name it will serve for server_name 127.0.0.1; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste location /static { alias /static; } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed } }
- ⑤uwsgi_params
uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;
- ⑥settings.py
from pathlib import Path import os DEBUG = True ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'mainproject.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'mainproject.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgresdb', 'USER': 'admin', 'PASSWORD': 'test', 'HOST': 'db', 'PORT': 5432, } } AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True STATICFILES_DIRS = ( os.path.join(BASE_DIR, "main/static/"), ) DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
発生している問題・エラーメッセージ
DB、uwsgiのコンテナは立ち上がるのですが、nginxのコンテナが立ち上がらない状況です
[emerg] 1#1: host not found in upstream "web:8001" in /etc/nginx/conf.d/nginx.conf:4
uwsgiのログもざっと調べた感じuwsgiで異常が出ているのかと見立てております。
*** Starting uWSGI 2.0.20 (64bit) on [Mon Feb 14 05:31:29 2022] *** compiled with version: 10.2.1 20210110 on 13 February 2022 13:28:18 os: Linux-5.10.47-linuxkit #1 SMP Sat Jul 3 21:51:47 UTC 2021 nodename: 6480ec1cd4a8 machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 4 current working directory: /code detected binary path: /usr/local/bin/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** your memory page size is 4096 bytes detected max file descriptor number: 1048576 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uwsgi socket 0 bound to TCP address :8001 fd 3 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** Python version: 3.7.12 (default, Feb 8 2022, 05:37:08) [GCC 10.2.1 20210110] Python main interpreter initialized at 0x55bbdfdb4f60 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** python threads support enabled your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 145840 bytes (142 KB) for 1 cores *** Operational MODE: single process *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55bbdfdb4f60 pid: 1 (default app) mountpoint already configured. skip. uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 1) spawned uWSGI worker 1 (pid: 8, cores: 1) Python auto-reloader enabled
ご了承していただきたいこと
uwsgiやnginxについて触るのが初めてで、調べながら見様見真似でしているので、意味不明な事をしているかもしれませんが、できるだけ詳細に教えていただけますと幸いです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/14 07:10