Confusion on using docker-compose volumes to serve django static files

Docker rookie here, trying to setup a simple Django project using Compose. I’ve had success with this in the past, but I’m trying a different setup this time, which I can’t figure out why it doesn’t work.

I have the following docker-compose.yml file:

  • how to configure sensu keepalive to throw an alert to slack
  • Elastic beanstalk: Failed to buld docker image
  • Jenkins Declarative Pipeline - how to display Gatling results?
  • Is Docker capable of providing container with gigabit network?
  • Write journald metadata to rsyslog
  • How to configure Apache running in a docker container to log both to files and console at the same time?
  • data:
      image: postgres:9.4
      volumes:
        - /var/lib/postgresql
      command: /bin/true
    
    db:
      image: postgres:9.4
      restart: always
      expose:
        - "5432"
      volumes_from:
        - data
    
    app:
      build: .
      restart: always
      env_file: .env
      expose:
        - "8000"
      links:
        - db:db
      volumes:
        - .static:/static_files
    
    web:
      build: docker/web
      restart: always
      ports:
        - "80:80"
      links:
        - app:app
      volumes_from:
        - app
    

    My /Dockerfile is:

    FROM python:3.5
    ENV PYTHONUNBUFFERED 1
    
    ADD . /app
    WORKDIR /app
    RUN pip install -r requirements.txt
    RUN SECRET_KEY=tmpkey python manage.py collectstatic --noinput
    
    CMD gunicorn mysite.wsgi:application -b 0.0.0.0:8000
    

    My /docker/web/Dockerfile is:

    FROM nginx:1.9
    RUN rm /etc/nginx/conf.d/default.conf
    ADD default.conf /etc/nginx/conf.d/
    

    And my /docker/web/default.conf file is:

    server {
        listen 80 default_server;
    
        location /static/ {
          autoindex on;
          alias /static_files/;
        }
    
        location / {
            proxy_pass http://app:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    

    The output from docker shows the static files installing into /static_files, but nginx returns 404 for all the files under /static. If I look in my .static folder (in root of project), the directory is empty (aside from a .gitkeep file I have in there). If I run docker-compose app run ls -la /static_files, the directory is empty, but docker-compose app run ls -la /app/.static has the .gitkeep file. Clearly I’m misunderstanding something with Docker and Compose. Any thoughts on what I’m doing wrong? My understanding is that the RUN SECRET_KEY=tmpkey python manage.py collectstatic --noinput should be writing files to my local .static folder and that nginx should see these files; neither is happening.

    Software versions: docker-compose version 1.7.0, build 0d7bf73 and Docker version 1.11.0, build 4dc5990 on OS X, with docker-machine connected to cloud instance.

  • Reproducible Docker builds - long-term storage/restoring
  • Cannot get image on Docker - Network timed out [Windows]
  • does the FROM directive in a dockerfile allways pull the latest version of an image
  • What are the security and performance constraints of using docker containers in a RESTful API in order to execute untrusted code?
  • Docker does not have default machine in Windows 10
  • Mount specific EBS volume to Docker under AWS beanstalk
  • One Solution collect form web for “Confusion on using docker-compose volumes to serve django static files”

    I am still unclear of why my original code does not work, but switching my code to use Compose’s v2 format works, where I have my volumes defined outside of services.

    Here’s my updated docker-compose.yml file:

    version: '2'
    services:
      db:
        image: postgres:9.4
        restart: always
        expose:
          - "5432"
        volumes:
          - postgres-data:/var/lib/postgresql
      app:
        build: .
        restart: always
        env_file: .env
        expose:
          - "8000"
        links:
          - db:db
        volumes:
          - static-data:/static_files
      web:
        build: docker/web
        restart: always
        ports:
          - "80:80"
        links:
          - app:app
        volumes:
          - static-data:/static_files
    volumes:
      postgres-data:
        driver: local
      static-data:
        driver: local
    

    The rest of the config files remained the same.

    (It may be worth noting that before I ran this new config, I deleted all existing Docker volumes listed in docker volume ls — perhaps this was my actual fix?)

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.