testdrivenio/django-on-docker

named volume inside bind mount is not good

glebmikha opened this issue · 7 comments

In your docker-compose you put:

volumes:
- ./app/:/usr/src/app/
- static_volume:/usr/src/app/staticfiles
- media_volume:/usr/src/app/mediafiles

Static_volume and media_volume are inside bind mount /usr/src/app/. It's kinda double mount. One overrides another.

I've noticed that after some file uploads or some service restarts it stops working correctly: nginx stops serve files, like in this issue .

After removing - ./app/:/usr/src/app/ from docker-compose volumes service begins to work correctly.

I thing volumes shouldn't be used like that. But I understand that bind mount - ./app/:/usr/src/app/ is convenient for development. In production mode this mount should be switched off.

It could be two separate docker-compose files in this case: dev and prod. And in dev you could use just django default web server without nginx. See this guide -- they do exactly that: separate nginx from app.

What do you think?

P.S. Thank you for your tutorial: it's very clear and well written. Your work helped me a lot.

I agree with you on almost all of this. I am planning on refreshing the tutorial very soon. Do you want to submit a PR for the changes to the compose file?

There also seems to be an issue with Postgres, there isnt a local db file that gets created in the folder postgres_data that is supposed to be mounted.

Comment out the bold line. This clears out the database every time the container is built. Only took me two weeks and countless google searches to figure out.

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $SQL_HOST $SQL_PORT; do
  sleep 0.1
done

echo "PostgreSQL started"

fi

python manage.py flush --no-input
python manage.py migrate

exec "$@"

I have new compose version I made on my own and works great; Entrypoint was a thing we did back in the day when Docker was in its infancy. If you use Docker CE + Compose Entrypoint is not meeded, unless you are doing complex tasks like Docker swarm.

@joehoeller can I take a look at your compose file? Thanks!

Fixed #8

Thanks for you help on this @glebmikha. I made some additional changes and refreshed the post. https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/