title: Django collectstatic Permission denied with docker-compose
body: <p>I'm making a Django app and I'm using Docker por production deployment, the application almost runs fine on the VPS(Ubuntu 20.04) except that when I try to run collectstatic with this command:</p>
  <pre><code>sudo docker-compose -f docker-compose.prod.yml exec web python manage.py collectstatic --no-input
  </code></pre>
  <p>Always throws this error:</p>
  <pre><code>PermissionError: [Errno 13] Permission denied: '/home/app/web/staticfiles/img/app_logo.png'
  </code></pre>
  <p>This is my docker-compose:</p>
  <pre><code>version: '3'
  services:
    db:
      restart: unless-stopped
      image: &quot;postgres:13.2-alpine&quot;
      container_name: db
      ports:
        - &quot;5432:5432&quot; # HOST:CONTAINER
      env_file:
        - ./.env.prod
      volumes:
        - ./pgdata:/var/lib/postgresql/data
    web:
      container_name: web
      restart: unless-stopped
      build:
        context: .
        dockerfile: ./docker/production/Dockerfile
      environment:
        DJANGO_SETTINGS_MODULE: MyApp.app_settings.production
      command: gunicorn MyApp.wsgi:application --bind 0.0.0.0:8000
      expose:
        - &quot;8000&quot;
      env_file:
        - ./.env.prod
      volumes:
        - .:/app # Enable code reload
        - ./run/static:/home/app/web/staticfiles
        - ./run/media:/home/app/web/mediafiles
      depends_on:
        - db
    nginx:
      container_name: nginx
      restart: unless-stopped
      build: docker/production/nginx
      volumes:
        - ./run/static:/home/app/web/staticfiles
        - ./run/media:/home/app/web/mediafiles
      ports:
        - &quot;80:80&quot;
        - &quot;443:443&quot;
      depends_on:
        - web
  </code></pre>
  <p>and this is my Dockerfile:</p>
  <pre><code># pull official base image
  FROM python:3.8
  
  # create directory for the app user
  RUN mkdir -p /home/app
  
  # create the app user
  #RUN addgroup -S app &amp;&amp; adduser -S app -G app
  RUN adduser --system --group app
  
  # create the appropriate directories
  ENV HOME=/home/app
  ENV APP_HOME=/home/app/web
  RUN mkdir $APP_HOME
  RUN mkdir $APP_HOME/staticfiles
  RUN mkdir $APP_HOME/mediafiles
  WORKDIR $APP_HOME
  
  # Own home directory
  RUN chown -R app:app $HOME
  
  # set environment variables
  ENV PYTHONDONTWRITEBYTECODE 1
  ENV PYTHONUNBUFFERED 1
  
  # install dependencies
  #RUN apk update &amp;&amp; apk add libpq
  #COPY --from=builder /usr/src/app/wheels /wheels
  #COPY --from=builder /usr/src/app/requirements.txt .
  #RUN pip install --no-cache /wheels/*
  
  COPY ./requirements.txt $APP_HOME
  RUN pip install --no-cache-dir -r requirements.txt
  
  RUN apt-get clean
  RUN apt-get update
  # Install netcat
  RUN apt install -y netcat
  # Install wkhtml2pdf
  RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb
  RUN DEBIAN_FRONTEND=noninteractive apt install -y -q ./wkhtmltox_0.12.6-1.buster_amd64.deb
  
  # copy entrypoint-prod.sh
  COPY ./docker/prod/ep.prod.sh $APP_HOME
  
  # execution permissions
  RUN [&quot;chmod&quot;, &quot;+x&quot;, &quot;/home/app/web/ep.prod.sh&quot;]
  
  # copy project
  #COPY . $APP_HOME
  COPY --chown=app:app . $APP_HOME
  
  
  # chown all the files to the app user
  #RUN chown -R app:app $APP_HOME
  
  # change to the app user
  USER app
  
  # run entrypoint.prod.sh
  ENTRYPOINT [&quot;/home/app/web/ep.prod.sh&quot;]
  </code></pre>
  <p>I believe that my Dockerfile should have something wrong, but I don't know exactly what. Anyone can help me?</p>
  <p><strong>Edit:</strong>
  With the answer below, they suggest to run it this way and works fine:</p>
  <pre><code>docker exec -it -u 0 [CONTAINER_NAME] python manage.py collectstatic --no-input
  </code></pre>
  <p>Also I found this solution, but I believe that this is not safe, any other suggestion?</p>
  <pre><code>chmod a+rwx -R run/
  </code></pre>
  <p>I followed this tutorial: <a href="https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/" rel="nofollow noreferrer">https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/</a></p>