title: Permission denied on mkdir inside of a django Docker container when running collectstatic
body: <p>I've modified django-cookiecutter default production template to make <strong>caddy</strong> web server serve static files. I'm using volumes to map the <code>./static</code> directories in <strong>django</strong> and <strong>caddy</strong> containters through host <code>./static</code> directory, but I'm getting permissions error when docker executes <code>python manage.py collectstatic --noinput</code> while trying to create a subfolder of <code>./static</code>.</p>
  
  <p>However, if I don't switch to <code>django</code> user in django container's Dockerfile, hence execute <code>collectstatic</code> as root, everything works perfectly. I guess <code>django</code> user in the container is not allowed to write to host directory, even despite the fact that <code>chown -R django /app/static</code> was successfully executed.</p>
  
  <blockquote>
    <p>Traceback (most recent call last): <br>
    File "/app/manage.py", line 30, in &lt;\module> <br>
    execute_from_command_line(sys.argv) <br>
    ... <br>
    File "/usr/local/lib/python3.6/site-packages/collectfast/management/commands/collectstatic.py", line 111, in copy_file <br>
    self.do_copy_file(args) <br>
    File "/usr/local/lib/python3.6/site-packages/collectfast/management/commands/collectstatic.py",
    line 100, in do_copy_file <br>
    path, prefixed_path, source_storage) <br>
    File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 354, in copy_file <br> 
    self.storage.save(prefixed_path, source_file) <br>
    File "/usr/local/lib/python3.6/site-packages/django/core/files/storage.py",
    line 49, in save <br>
    return self._save(name, content) <br>
    File "/usr/local/lib/python3.6/site-packages/django/core/files/storage.py", line 236, in _save <br>
    os.makedirs(directory) <br>
    File "/usr/local/lib/python3.6/os.py", line 220, in makedirs <br>
    mkdir(name, mode) <br>
    PermissionError: <br>
    [Errno 13] Permission denied: '/app/static/sass'</p>
  </blockquote>
  
  <p>I tried <code>chown -R systemd-timesync:root static</code> inside host, creating <code>./static</code> folder beforehand inside host as root, and adding <code>RUN mkdir /app/static &amp;&amp; chown -R django /app/static</code> to django container's Dockerfile (to execute as container's root user).</p>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: '3'
  
  volumes:
    production_postgres_data: {}
    production_postgres_data_backups: {}
    production_caddy: {}
  
  services:
    django:
      build:
        context: .
        dockerfile: ./compose/production/django/Dockerfile
      volumes:
        - ./static:/app/static
      depends_on:
        - postgres
        - redis
      env_file:
        - ./.envs/.production/.django
        - ./.envs/.production/.postgres
      command: /start
  
    postgres:
      build:
        context: .
        dockerfile: ./compose/production/postgres/Dockerfile
      volumes:
        - production_postgres_data:/var/lib/postgresql/data
        - production_postgres_data_backups:/backups
      env_file:
        - ./.envs/.production/.postgres
  
    caddy:
      build:
        context: .
        dockerfile: ./compose/production/caddy/Dockerfile
      depends_on:
        - django
      volumes:
        - production_caddy:/root/.caddy
        - ./static:/srv/static
      env_file:
        - ./.envs/.production/.caddy
      ports:
        - "0.0.0.0:80:80"
        - "0.0.0.0:443:443"
  
    redis:
      image: redis:3.2
  </code></pre>
  
  <p>django container Dockerfile</p>
  
  <pre><code>FROM nickgryg/alpine-pandas
  
  ENV PYTHONUNBUFFERED 1
  
  RUN apk update \
    # psycopg2 dependencies
    &amp;&amp; apk add --virtual build-deps gcc python3-dev musl-dev \
    &amp;&amp; apk add postgresql-dev \
    # Pillow dependencies
    &amp;&amp; apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
    # CFFI dependencies
    &amp;&amp; apk add libffi-dev py-cffi \
    # lxml dependencies
    &amp;&amp; apk add libxml2-dev libxslt-dev
  
  RUN addgroup -S django \
      &amp;&amp; adduser -S -G django django
  
  # Requirements are installed here to ensure they will be cached.
  COPY ./requirements /requirements
  RUN pip install --no-cache-dir -r /requirements/production.txt \
      &amp;&amp; rm -rf /requirements
  
  COPY ./compose/production/django/entrypoint /entrypoint
  RUN sed -i 's/\r//' /entrypoint
  RUN chmod +x /entrypoint
  RUN chown django /entrypoint
  
  COPY ./compose/production/django/start /start
  RUN sed -i 's/\r//' /start
  RUN chmod +x /start
  RUN chown django /start
  
  COPY . /app
  
  RUN chown -R django /app
  
  USER django
  
  WORKDIR /app
  
  ENTRYPOINT ["/entrypoint"]
  </code></pre>
  
  <p>django container start script</p>
  
  <pre><code>#!/bin/sh
  
  set -o errexit
  set -o pipefail
  set -o nounset
  
  python /app/manage.py collectstatic --noinput
  /usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app
  </code></pre>
  
  <p>I don't want my container to be executed as root, so I'm looking for any solutions / ideas.</p>