簡體   English   中英

Dockerizing django、postgres、gunicorn、ngnix - 僅管理員 static 工作,其他 404

[英]Dockerizing django, postgres, gunicorn, ngnix - only admin static works, other 404

如題。 我試圖應用我在互聯網上找到的解決方案,但沒有成功:(我對編程很陌生,因為制作應用程序是一回事,部署它讓我不知所措。我設法把它放在服務器上,在 docker 上運行它with SSL, but main problem is that only admin static files load, other give 404 error. Weird thing is that static subdirectories structure is kept, but there are no files inside. Please help me, I am really stuck:( If you require any其他數據,請寫

請注意, STATIC_ROOT 或 STATIC_DIRS 都不起作用(在某些情況下更改它會有所幫助,而不是在這里)。

非常感謝您提前

編輯:我不知道為什么,但文件存在於 /home/app/web/staticfiles 在我將此目錄用於 static 之前。 某人可以解釋發生了什么嗎?

目錄結構:

├── app
│   ├── Dockerfile.prod
│   ├── entrypoint.prod.sh
│   ├── manage.py
│   ├── requirements.txt
│   ├── sancor
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   ├── settings.py
│   │   ├── urls.py
│   │   ├── views.py
│   │   └── wsgi.py
│   ├── static
│   │   ├── css
│   │   ├── favicon.ico
│   │   ├── fonts
│   │   ├── js
│   │   ├── less
│   │   ├── sancor
│   │   │   ├── css
│   │   │   └── js
│   │   └── scss
├── docker-compose.staging.yml
├── env
└── nginx
    ├── custom.conf
    ├── Dockerfile
    ├── nginx.conf.old
    └── vhost.d
        └── default

應用程序/Dockerfile:

###########
# BUILDER #
###########

# pull official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev musl-dev

# lint
RUN pip install --upgrade pip
#RUN pip install flake8
COPY . .
#RUN flake8 --ignore=E501,F401 .

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.8.3-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/static
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

應用程序/入口點.sh:

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started succesfully"
fi

exec "$@"

應用程序/sancor/settings.py:

from pathlib import Path
import os

BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')

SECRET_KEY = os.environ.get("SECRET_KEY")

DEBUG = False

ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")

# template context processor
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
)

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap4',
    'tempus_dominus',
    'crispy_forms',
    'django_filters',
    'accounts',
    'pacjenci',
]

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 = 'sancor.urls'

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [STATIC_DIR,]
#STATIC_ROOT = os.path.join(BASE_DIR, "static") 

LOGIN_REDIRECT_URL = 'test'
LOGOUT_REDIRECT_URL = 'thanks'

TEMPUS_DOMINUS_LOCALIZE = 'True'

CRISPY_TEMPLATE_PACK = 'bootstrap'

SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

docker-compose.staging.yml:

version: '3.7'

services:
  web:
    build:
      context: ./app
      dockerfile: Dockerfile.prod
    command: gunicorn sancor.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/static
      - media_volume:/home/app/web/mediafiles
    expose:
      - 8000
    env_file:
      - ./.env.staging
    depends_on:
      - db

  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.staging.db
  
  nginx-proxy:
    container_name: nginx-proxy
    build: ./nginx
    restart: always
    ports:
      - 443:443
      - 80:80
    volumes:
      - static_volume:/home/app/web/static
      - media_volume:/home/app/web/mediafiles
      - certs:/etc/nginx/certs
      - html:/usr/share/nginx/html
      - vhost:/etc/nginx/vhost.d
      - /var/run/docker.sock:/tmp/docker.sock:ro
    depends_on:
      - web

  nginx-proxy-letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    env_file:
      - ./.env.staging.proxy-companion
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - certs:/etc/nginx/certs
      - html:/usr/share/nginx/html
      - vhost:/etc/nginx/vhost.d
      - acme:/etc/acme.sh
    depends_on:
      - nginx-proxy

volumes:
  postgres_data:
  static_volume:
  media_volume:
  certs:
  html:
  vhost:
  acme:

請看一下這些答案: dockerizing django配置 static 文件

沒有 STATIC_DIR 選項,它是STATIC_ROOT

STATICFILES_DIRS = [STATIC_DIR,] - 無意義的設置。 STATICFILES_DIRS是在collectstatic期間將文件收集STATIC_ROOT文件夾。 在要傳遞到目標文件夾的源文件夾列表中列出目標文件夾沒有多大意義。

DEBUG = False沒有用(如果您嘗試使用它修復 static 文件),因為 nginx 將處理 static url。

考慮將所有文件存儲在任何用戶主文件夾之外的圖像/容器中 - 這可能會導致安全問題。 將它們放在 /var/opt 任何您選擇的位置,並為用戶提供適當的權限 並限制其他人的權限。

static_volume:/home/app/web/static - nginx 圖像對用戶“應用程序”有什么了解嗎? 它在“應用程序”下運行嗎?

另請查看/分享 nginx.conf

static subdirectories structure is kept, but there are no files inside

如果我沒記錯的話,您首先嘗試收集靜態文件,他們會構建包含這些文件的 docker 映像並通過虛擬卷共享它們。 避免將static 文件包含到 django 應用程序 docker 映像中是一種常見情況。 目前要更新其中任何一個,您必須重建並重新部署整個映像,重新啟動容器。 考慮在沒有 static 文件的情況下構建映像,分別收集它們,作為“僅文件”傳遞到文件夾並將容器映射到真正的共享文件夾(實際上只有 nginx 需要它們)。

即使您選擇保留當前概念,請注意,當您將 map 文件寫入 nginx 時,絕對沒有必要堅持使用home/app Nginx 有它自己的配置,它不必尊重“whatever settings.py”配置。 Nginx 將任何location映射到任何alias/root文件夾。 因此,在將卷附加到 nginx 容器時,您可以自由選擇所需的任何目標文件夾名稱(位置)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM