Serving Django MEDIA from DOKKU host server

So you don’t like the hassle of setting up a storage backend and WhiteNoise doesn’t support serving media folder. If you use Dokku it’s IMHO reasonable to host and serve those files from the same machine that runs Dokku pass — you already have NGINX sitting there.

The trick is to mound a local folder on a container and setup NGINX so it would not bother the container with request for media:

sudo mkdir -p /var/lib/dokku/data/storage/some-app/media
sudo chown -R dokku:dokku /var/lib/dokku/data/storage/some-app/
sudo chown -R 32767:32767 /var/lib/dokku/data/storage/some-app
dokku storage:mount some-app /var/lib/dokku/data/storage/some-app/media:/app/media
echo 'location /media/ { alias /var/lib/dokku/data/storage/some-app/media/ ; }' | sudo tee /home/dokku/some-app/nginx.conf.d/media.conf
sudo chown dokku:dokku /home/dokku/some-app/nginx.conf.d/media.conf
sudo service nginx reload

This is based on Persistent Storage and Customizing via configuration files included by the default templates from Dokku docs.

Leave a Reply

Back to Top