title: serving Django static files with Docker, nginx and gunicorn
body: <p>I am setting us a <code>Django 2.0</code> application with <code>Docker</code>, <code>nginx</code> and <code>gunicorn</code>. </p>
  
  <p>It's running the server but static files are not working.</p>
  
  <p>Here is the <code>settings.py</code> content</p>
  
  <pre><code>STATIC_URL = '/static/'
  
  STATICFILES_DIRS = [
      os.path.join(BASE_DIR, 'static_my_project')
  ]
  
  STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'static_root')
  </code></pre>
  
  <p>While developing, I put my static files inside <code>static_my_project</code>, which on running <code>collectstatic</code> copies to <code>static_cdn/static_root</code></p>
  
  <p>The directory structure is like</p>
  
  <pre><code>app
   |- myapp
      |- settings.py
   |- static_my_project
   |- static_cdn
      |- static_root
   |- config
      |- nginx
         |- nginx.conf
   |- manage.py
   |- Docker
   |- docker-compose.yml
  </code></pre>
  
  <p>on running</p>
  
  <pre><code>docker-compose up --build
  </code></pre>
  
  <p>on running <code>collectstatic</code>, it gives path where static files will be copied</p>
  
  <blockquote>
    <p>koober-dev | --: Running collectstatic<br>
    koober-dev |<br>
    koober-dev | You have requested to collect static files at the destination<br>
    myapp-dev | location as specified in your settings:<br>
    myapp-dev |<br>
    myapp-dev |     /app/static_cdn/static_root<br>
    myapp-dev |<br>
    myapp-dev | This will overwrite existing files!<br>
    myapp-dev | Are you sure you want to do this?<br>
    myapp-dev |<br>
    myapp-dev | Type 'yes' to continue, or 'no' to cancel:<br>
    myapp-dev | 0 static files copied to '/app/static_cdn/static_root', 210 unmodified.</p>
  </blockquote>
  
  <p>the <strong>config/nginx/nginx.conf</strong> file contains following settings</p>
  
  <pre><code>upstream web {
      ip_hash;
      server web:9010;
  }
  
  server {
      location /static {
          autoindex on;
          alias /static/;
      }
  
      location / {
          proxy_pass http://web;
      }
      listen 10080;
      server_name localhost;
  }
  </code></pre>
  
  <p><strong>docker-compose.yml</strong></p>
  
  <pre><code>version: '3'
  
  services:
    nginx:
      image: nginx:latest
      container_name: "koober-nginx"
      ports:
        - "10080:80"
        - "10443:43"
      volumes:
        - .:/app
        - ./config/nginx:/etc/nginx/conf.d
        - ./static_cdn/static_root/:/static
      depends_on:
        - web
    web:
      build: .
      container_name: "koober-dev"
      command: ./start.sh
      volumes:
        - .:/app
        - ./static_cdn/static_root/:/app/static_cdn/static_root
      ports:
        - "9010:9010"
      depends_on:
        - db
    db:
      image: postgres
      container_name: "koober-postgres-db"
  </code></pre>
  
  <p><strong>Dockerfile</strong></p>
  
  <pre><code>FROM ubuntu:18.04
  
  # -- Install Pipenv:
  FROM python:3
  ENV PYTHONUNBUFFERED 1
  
  ENV LC_ALL C.UTF-8
  ENV LANG C.UTF-8
  
  # -- Install Application into container:
  RUN set -ex &amp;&amp; mkdir /app
  
  WORKDIR /app
  ADD requirements.txt /app/
  
  RUN pip install -r requirements.txt
  
  # -- Adding dependencies:
  ADD . /app/
  </code></pre>
  
  <p><strong>But it is not loading static files.</strong></p>