I have a simple python/flask app. It's like this on the container
/var/www/app/
appl/
static/
...
app.py
wsgi.py
I used to let the nginx serve the static files directly before using docker. Like this:
location /static {
alias /var/www/www.domain.com/appl/static;
}
location / {
uwsgi_pass unix:///tmp/uwsgi/www.domain.com.sock;
include uwsgi_params;
}
But now the static files is inside the container and not accessible by nginx.
I can think of 2 possible solutions:
start a nginx inside the container as before, and let the host nginx to communicate with the container nginx using port like 8000
mount the (host)/var/www/www.domain.com/static
to (container)/var/www/static
and copy all static files in run.sh
What do the docker prefer?
I prefer the first solution because it stays in line with factor 7 of building a 12 factor app: exposing all services on a port. There's definitely some overhead with requests running through Nginx twice, but it probably won't be enough to worry about (if it is then just add more containers to your pool). Using a custom run script to do host-side work after your container starts will make it very difficult to scale your app using tools in the Docker ecosystem.
I do not like the first solution because running more than one service on one container is not a docker way.
Overall, we want to expose our static folder to nginx, then Volume is the best choice. But there are some different ways to do that.
as you mentioned, mount the (host)/var/www/www.domain.com/static
to (container)/var/www/static
and copy all static files in run.sh
using nginx cache, to let nginx cache static files for you. for example we can write our conf like this, to let nginx solving static contents with 30min
-
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:30m max_size=1G;
upstream app_upstream {
server app:5000;
}
location /static {
proxy_cache cache;
proxy_cache_valid 30m;
proxy_pass http://app_upstream;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With