Serving static files with nginx located in another docker container

All the ideas I've had in the past:

Sharing a docker volume from app to nginx

You can make a volume in the app's Dockefile and copy in the staticfiles when the container runs. Then share the volume with nginx container using volumes_from. This is kind of ugly and doesn't work at all if your app depends_on nginx. I'd say this is definitely a no-go as it works terribly when scale up your app container.

The idea of also mapping staticfiles from the host into the nginx container is also not optimal. You'll have an extra weird step to deal with them.

Separate static container

Build another nginx container serving only the static files on a different virtualhost. static.foo.bar.

Use a CDN

There are tons of CDNs out there you can put your staticfiles and most frameworks have plugins for handling that. I have some projects doing this. Works great.

Use uWSGI

You can serve staticfiles with uWSGI using --static-map. See docs. This is what I ended up doing as it was a cheap and easy... and friendly when it comes to scaling. Then you probably also need to use http-socket so uWSGI talks http instead.


This is a very late answer, but adding it in case someone else finds this.

Perhaps you could make use of server caching so that NGINX will effectively serve the static files from it's file system after the first request to the app being proxied.

Here is a good guide to caching with NGINX

You could set the inactive flag to a long time as the assets are static.


Basic example based on the guide above:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=30d use_temp_path=off;

server {
  ...

  location /static {
    proxy_cache my_cache;
    proxy_pass http://app:8000/static; # <--- wherever static files are on app server
  }

  ...
}