繁体   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