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:

  • akka node in docker explicitly disassociated
  • Run a script when docker is stopped
  • Setup PhpStorm and Docker for PHP development without PHP on the host
  • docker's embedded dns on the default bridged network
  • Rabbitmq connection refused from Docker container to local host
  • Yesod Persona authentication problems when using Dockerized MongoDB
  • 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.

  • How can I give Docker containers access to a dnsmasq local DNS resolver on the host?
  • Can I run Docker-in-Docker without using the --privileged flag
  • docker registry with --insecure-registry and docker 1.5
  • Docker: use file in local folder
  • Dockerfile: Permission denied when trying to install ruby-build
  • Customize docker-java response to remove some logger like stream
  • 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.