简体   繁体   中英

Django static files not serving when running inside Docker container

I have a django application thats is rendering the static files when run outside docker container. I added a Dockerfile so that I can run it in a container. However, the static files are not getting served when running in the container

The sample app that I have used can be found here

I have added the following Dockerfile at the root level

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

And the requirements.txt file at root level as below

django==3.0.3
gunicorn==20.0.4

The static files settings in my settings.py is as follows

STATIC_URL = '/static/'

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

Please let me know if there's something wrong with my configuration

You are missing collectstatic command which will package static files alongside the app.

Check this link for reference.

Revisa keeps you configured.

STATIC_ROOT

You should have no problems when you run the command.

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

If it's fine but still doesn't render the statics. Check the gunicorn documentation.

I recommend you to use uwsgi is much more elegant. https://uwsgi-docs.readthedocs.io/en/latest/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM