Which docker template serves both static and dynamic content

I have a simple python / flask application. It's like on a container

 /var/www/app/ appl/ static/ ... app.py wsgi.py 

I used nginx to serve static files just before using dockers. 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 are inside the container and inaccessible to nginx.

I can imagine two possible solutions:

  • run nginx inside the container, as before, and let the nginx host interact with the nginx container using a port like 8000

  • set (host)/var/www/www.domain.com/static to (container)/var/www/static and copy all the static files to run.sh

What does docker prefer?

+5
source share
2 answers

I prefer the first solution, because it remains in accordance with factor 7 of building a 12-factor application : exposing all services on the port. There is definitely some overhead with requests passing through Nginx twice, but probably this will not be enough to worry (if it is just add more containers to your pool). Using a custom script to run on the host side after starting your container will make it very difficult to scale your application with tools in the Docker ecosystem.

+1
source

I do not like the first solution, because running multiple services on the same container is not a docker way.

In general, we want to open our static folder for nginx, then Volume is the best choice. But there are several ways to do this.

  • as you mentioned, set (host)/var/www/www.domain.com/static to (container)/var/www/static and copy all the static files to run.sh

  • using nginx cache so you can use static files for nginx cache. for example, we can write our code so that nginx allows static content 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; } 
  1. trust uwsgi and use uwsgi to serve static content. Serving Static Files Using uWSGI
+1
source

Source: https://habr.com/ru/post/1202867/


All Articles