Comments (6)
Sorry, I'd just now realised collectstatic
is not run in development and was coming here to correct myself. Apologies; it's hard to follow exactly what's going on.
from docker-django-example.
Hi,
The (2) /public*/
directories are there to hold your assets in a non-volume mounted location so they never get overwritten by your volumes. These get populated at build time when you build your Docker image, the /public_collected/
directory gets populated when collectstatic
gets run based on having set DEBUG=false
in your .env
at build time.
At runtime (thanks to https://github.com/nickjj/docker-django-example/blob/main/bin/docker-entrypoint-web) the public_collected/
directory gets copied over to /app/public_collected
where your volume can take over to allow you to persist files back to your Docker host and potentially serve these assets from a web server not running in Docker such as nginx.
It is pretty convoluted but I don't see a way around all of this. We have to meet 2 criteria here:
- Django does not let you output
collectstatic
in the same directory where your source is, this means we need 2 sets of public/ directories, I named them accordingly - We need to build both sets of public files in a non-volume mounted directory but also make them available in a volume at runtime in such a way where existing files in the volume on the Docker host at runtime won't overwrite the newly built public files, this means we need to store both sets of public/ directories in 2 locations
If you have a way to meet both of these criteria in a cleaner way I'm all for improving it, keep in mind it would need to work in development too where assets aren't collected. I'd love to be able to get rid of this workflow.
from docker-django-example.
Thanks that makes sense. I've only been using this for local development so far, so haven't quite grasped the differences with how it would work in production.
Previously I've only used manage.py runserver
during development, not gunicorn, and added "whitenoise.runserver_nostatic"
to INSTALLED_APPS
if DEBUG=True
. Which I think means I don't need to run collectstatic
except on the production server (where I wasn't using Docker).
I assume we need to run collectstatic
in development because we're using gunicorn?
Now that I'm thinking of using Docker for both dev and production I have this extra level of complication to get my head round I guess!
In my dabblings with Docker and Django I'm always frustrated that dealing with static files ends up being so complicated!
from docker-django-example.
You don't need to run collectstatic
in development.
The exact instructions in the readme file combined with the source code in this repo will work out of the box and your assets will get served in development.
The collection process is mainly around digesting the files with an md5 hash, it's aimed at production although there's nothing stopping you from digesting them manually in development. It happens in production by default due to the Dockerfile having a condition to run it when DEBUG=false
:
docker-django-example/Dockerfile
Lines 64 to 66 in b87f465
from docker-django-example.
Related Issues (20)
- New project name psql role doesn't exist HOT 5
- ERROR: Service 'css' failed to build : COPY failed: forbidden path outside the build context: HOT 4
- Celery worker crashes HOT 2
- adding separate frontend spa (react/vue/etc) HOT 5
- Service 'worker' failed to build : Build failed HOT 2
- tailwindcss --watch is not detecting changes HOT 8
- Update README.md about need to "chown 1000 ./public*" HOT 5
- freeze no longer used. HOT 1
- unbound variable HOT 13
- HTML templates reloading issue (TailwindCSS) HOT 31
- IPDB debugging HOT 4
- Code reloading stopped working consistently when updating HTML templates HOT 4
- docker compose build : Unable to locate package build-essential HOT 3
- update docker compose sub commands HOT 4
- Add `LOG_LEVEL` env var? HOT 3
- Getting Server Error (500) on debug=false on env file HOT 3
- Updating dependencies during development HOT 2
- Any interest in adding Sentry and/or Mailhog services? HOT 4
- [Error: EACCES: permission denied, mkdir '/app/public/css'] HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docker-django-example.