簡體   English   中英

在 Docker 容器中運行時,Django 靜態文件不提供服務

[英]Django static files not serving when running inside Docker container

我有一個 django 應用程序,它在 docker 容器外運行時呈現靜態文件。 我添加了一個 Dockerfile,以便我可以在容器中運行它。 但是,在容器中運行時不會提供靜態文件

我使用的示例應用程序可以在這里找到

我在根級別添加了以下 Dockerfile

FROM python:3.8

#add project files to the usr/src/app folder
ADD . /usr/src/app

#set directoty where CMD will execute 
WORKDIR /usr/src/app/wisdompets
COPY requirements.txt ./

# Get pip to download and install requirements:
RUN pip install --no-cache-dir -r requirements.txt

# Expose ports
EXPOSE 8000

# default command to execute    
CMD exec gunicorn wisdompets.wsgi:application --bind 0.0.0.0:8000 --workers 3

根級別的requirements.txt文件如下

django==3.0.3
gunicorn==20.0.4

我的settings.py中的靜態文件設置如下

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

如果我的配置有問題,請告訴我

您缺少collectstatic命令,該命令會將靜態文件與應用程序一起打包。

檢查此鏈接以供參考。

Revisa 讓您保持配置。

STATIC_ROOT

運行命令時應該沒有問題。

python manage.py \
        collectstatic \
        --verbosity 0 \
        --noinput \
        --clear

如果它很好但仍然沒有呈現靜態。 檢查gunicorn文檔。

我建議你使用uwsgi更優雅。 https://uwsgi-docs.readthedocs.io/en/latest/

暫無
暫無

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

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