4

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:

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.

Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100

2 Answers2

5

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?)

Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
  • Do you have updates after 2 years? Cause this is def not the right way to do it since static files should be updated after each release so you would need to shut the volume down and re-up it everytime. – HRK44 Jul 17 '18 at 09:41
  • @HRK44 -- The above requires re-running `collectstatic` each time you want to update the static files. If you're in a dev environment and you want the static files to update as you're developing, an easier setup is to let Django serve the static files as described in [Django docs](https://docs.djangoproject.com/en/2.0/howto/static-files/#serving-static-files-during-development) (thus bypassing Nginx). If you're in production and pushing the static files up to a remote server, then this can be slow. Is that your complaint with it? – Dolan Antenucci Jul 17 '18 at 15:05
  • For some reason my ``collectstatic`` wasn't updating the files located in the volume after each build. When I dug into this, people were saying that you should generate the files with a builder then COPY the files into the volume (which is what I did and it worked). – HRK44 Jul 17 '18 at 15:17
  • @HRK44 -- That's odd if you're calling collectstatic via docker (e.g., `docker-compose exec django python manage.py collectstatic`). I suppose you could try `collectstatic --clear` to clear the volume first, but there's no reason why that would help (other than debugging timestamps maybe). Anyway, at least you got it working – Dolan Antenucci Jul 17 '18 at 19:54
  • @HRK44 I have the same issue. When I collectstatic, it does not update the shared volume. My shared volume uses the local driver. – radish25 Mar 25 '20 at 21:30
1

I stumbled across this question because my static files would not update after deploying my Docker Compose setup.

Removing all Docker volumes helped – as Dolan suggests at the end of his answer. My static files were updated after running:

docker volume prune

Of course this deletes the volumes and their contents, so be careful!

steventilator
  • 205
  • 2
  • 8