这是my项目的Dockerfile:
FROM python:3.10.5-alpine
ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1
ENV PYTHONUNBUFFERED=1
RUN adduser --disabled-password appuser
USER appuser
WORKDIR /home/appuser/app
COPY requirements.txt .
USER root
RUN python -m pip install --no-cache-dir --disable-pip-version-check --requirement requirements.txt
USER appuser
COPY . .
ENTRYPOINT [ "./entrypoint.sh" ]和Django关于静态资产的设置:
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static/'和entrypoint.sh
#!/bin/sh
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic --no-input
gunicorn project.wsgi:application --bind=0.0.0.0:8000 --workers=4 --timeout=300 --log-level=debug --log-file=-
exec "$@"当我启动容器时,我会将其放入其中,并看到创建了static文件夹,并使用管理人员填充了。
然而,浏览http://127.0.0.1:8000/admin会打开管理登录页面,而不需要CSS,我在开发人员控制台中得到了很多404错误。
我还把STATIC_ROOT改成了/home/appuser/app/static/,得到了同样的信息。
请协助。
发布于 2022-08-02 15:04:46
是否检查了创建的静态文件夹的权限?
我必须手动更改文件夹的权限。
您可以尝试使用以下用于nginx的Dockerfile:
FROM nginx:latest
RUN apt-get update && apt-get install -y procps
RUN mkdir -p /home/app/staticfiles
RUN chmod -R 755 /home/app/staticfiles发布于 2022-08-06 01:31:54
你能试着使用这个配置吗?
Django:
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)Dockerfile:
RUN mkdir -p /home/appuser/app/staticfilesNginx.conf
location /static/ {
alias /home/appuser/app/staticfiles/;
}docker-compouse.yml类似于:
web:
container_name: test_table_django
build: .
command:
sh -c "python manage.py collectstatic --no-input --clear &&
python manage.py migrate &&
gunicorn --workers=4 --bind=0.0.0.0:8000 test_table.wsgi:application"
volumes:
- .:/home/appuser/app
- static_volume:/home/appuser/app/staticfiles/
env_file:
- .env.prod
expose:
- 8000
depends_on:
- db
restart: always
nginx:
build: ./nginx
container_name: test_table_nginx
ports:
- 80:80
volumes:
- static_volume:/home/appuser/app/staticfiles/发布于 2022-08-06 16:50:25
静态URL应该以/开头。您还可以检查日志以查看它试图到达的位置。
BASE_DIR = Path(__file__).resolve().parent.parent.parent
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static')https://stackoverflow.com/questions/73182138
复制相似问题