'Uploading files to Django + Nginx doesn't save it in the media volume in Docker
Basically whenever I try to upload a file using my website, the file doesn't get saved on the media volume.
I don't think its a code issue as it works perfectly fine without the container even when paired with nginx.
I followed this tutorial to setup my docker containers.
Here is my Dockerfile:
# pull official base image
FROM python:3.9.6-alpine
# set work directory
WORKDIR /home/azureuser/ecommerce3
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# fixing alpine related pip errors
RUN apk update && apk add gcc libc-dev make git libffi-dev openssl-dev python3-dev libxml2-dev libxslt-dev
RUN apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev
# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy entrypoint.sh
COPY ./entrypoint.sh .
RUN sed -i 's/\r$//g' ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
# copy project
COPY . .
# running entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
docker-compose.yml:
version: '3.8'
services:
web:
build:
context: ./
dockerfile: Dockerfile
command: sh -c "cd DVM-Recruitment-Task/ && gunicorn DVM_task3.wsgi:application --bind 0.0.0.0:8000"
volumes:
- static_volume:/home/azureuser/ecommerce3/staticfiles:Z
- media_volume:/home/azureuser/ecommerce3/mediafiles:Z
- log_volume:/home/azureuser/ecommerce3/logs
expose:
- 8000
depends_on:
- db
db:
image: postgres:13.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=---
- POSTGRES_PASSWORD=---
- POSTGRES_DB=---
nginx:
image: nginx
ports:
- 80:80
- 443:443
restart: always
volumes:
- ./nginx/DVM_task3:/etc/nginx/conf.d/default.conf
- static_volume:/home/azureuser/ecommerce3/staticfiles/:Z
- media_volume:/home/azureuser/ecommerce3/mediafiles/:Z
- log_volume:/home/azureuser/ecommerce3/logs
- (ssl certificate stuff here)
volumes:
postgres_data:
media_volume:
static_volume:
log_volume:
entrypoint.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"
fi
python DVM-Recruitment-Task/manage.py makemigrations ecommerce
python DVM-Recruitment-Task/manage.py migrate --noinput
python DVM-Recruitment-Task/manage.py collectstatic --no-input --clear
exec "$@"
Also my nginx file already has this inside a server block
location /media/ {
autoindex on;
alias /home/azureuser/ecommerce3/mediafiles/;
}
settings.py has this:
MEDIA_URL = '/media/'
MEDIA_ROOT = 'mediafiles'
urls.py already has this line in it
urlpatterns[...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Also my project structure looks something like this:
.
├── DVM-Recruitment-Task
│ ├── DVM_task3
│ ├── README.md
│ ├── ecommerce
│ ├── manage.py
│ ├── static
│ └── templates
├── Dockerfile
├── docker-compose.yml
├── entrypoint.sh
├── nginx
│ └── DVM_task3
└── requirements.txt
everything inside a directory named 'ecommerce3'.
The mediafiles, staticfiles and logs volume are supposed to be created inside the same directory (ecommerce3).
On running --collectstatic the staticfiles load correctly, the logs work as well but the media files just won't save to the mediafiles folder.
if I go into the web container's shell and manually create a file inside the mediafiles directory, I am able to view in the /media url so I assume nginx is pointing in the right direction. However when it comes to saving the files, the files never get saved on this volume.
I am very new to django and docker so any help or nudge in the right direction will be greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|