Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the docker pattern of serving both static and dynamic content

Tags:

docker

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:

  1. start a nginx inside the container as before, and let the host nginx to communicate with the container nginx using port like 8000

  2. 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?

like image 238
hbrls Avatar asked Oct 31 '22 16:10

hbrls


2 Answers

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.

like image 80
Kevan Ahlquist Avatar answered Nov 10 '22 16:11

Kevan Ahlquist


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.

  1. 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

  2. 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;
}
  1. trust uwsgi and using uwsgi to serving static contents. Serving static files with uWSGI
like image 27
Freeznet Avatar answered Nov 10 '22 17:11

Freeznet