1

The Docker image / container worked fine on Ubuntu, however I wanted to have access to the static folder. Therefore I added "Host volumes" to the docker-compose.yml (volumes was empty before):

version: '3.3'

services:
  api:
    build:
      context: .
    restart: always
    ports:
      - "8000:8000"
    volumes:
      - ./app_static:/app/static

$ docker-compose up

After building the image and running the python manage.py collectstatic command, I get the following error:

PermissionError: [Errno 13] Permission denied: 'static/media'

The folder structure inside the docker image looks like this:

appuser@1e2bc02404a2:/app$ ls -la
total 24
drwxr-xr-x 1 appuser appuser 4096 Mar 29 21:50 .
drwxr-xr-x 1 root    root    4096 Mar 29 21:51 ..
drwxr-xr-x 1 appuser appuser 4096 Mar 29 21:00 api
-rw-r--r-- 1 appuser appuser  765 Mar 29 21:00 manage.py
drwxr-xr-x 1 appuser appuser 4096 Mar 29 21:00 mausefallentest
drwxr-xr-x 2 root    root    4096 Mar 29 21:51 static

The problem is, that after adding Host volumes to the docker-compose file, the user for the static folder changed to root. But the Docker image is runnung under the "appuser" user. Thus i get the permission denied error.

But how can i solve this problem ? I only have this problem on my Ubuntu server. On Windows its working fine.

Here the endpart of the Django-Dockerfile:

...
RUN pip install --no-cache-dir -r /scripts/requirements.txt

# Creating Work Directory
WORKDIR /app

RUN useradd appuser
RUN chown -R appuser:appuser /app
RUN chmod -R 777 /app/static
USER appuser

CMD ["sh", "-c", "python manage.py collectstatic --no-input; python manage.py migrate; gunicorn api.wsgi:application --bind 0.0.0.0:8000"]
spoont
  • 41
  • 7
  • Are you running `python manage.py collectstatic` inside the `api` container? And if so, is the default user for that container `root` (which it is by default)? Then consider adding `user: 'appuser'` to the `api ` service configuration in your `docker-compose.yaml`. – Jeroen van der Laan Mar 29 '21 at 22:54
  • hi, for a better understanding i have attached the docker file to the question. But adding a user to the `docker-compose.yaml`doesent solve the problem. The user inside the container is appuser: `appuser@e421101bba13:/app$ whoami` `appuser` but the static folder has only write permission for root user – spoont Mar 30 '21 at 07:32
  • i think my solution is, to change to a Windows Server. – spoont Mar 30 '21 at 20:06

1 Answers1

1

I had to give the "appuser" permission to the "Host Volume" folder outside the docker container.

spoont
  • 41
  • 7
  • Can I ask how you did that? I'm having a dreadful time trying to get my collectstatic to work. This is my current question which has taken days of my life to try and solve and I'm still no nearer: https://stackoverflow.com/q/68720155/423961. I'm using Ubuntu on a Digital Ocean droplet. – Darren Aug 11 '21 at 07:46
  • 1
    get the "appuser" uid from outside the container $ sudo docker run --rm id appuser give the appuser (uid 1000) the permissions for the static folder $ root@h2872409:~/api# sudo chown -R 1000:1000 app_static but today i am using "named volumes", thats much easier to handle – spoont Aug 13 '21 at 07:34