Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static files with nginx located in another docker container

Just to start off, I have seen this. But he/she uses build, and I use image.

I have a docker-compose file that pulls an image that I have made previously onto my server.

app:
  restart: always
  image: some-app-image:latest

nginx config

location /static/ {
    root /data/app/web_interface;  <--- this exists in some-app-image container
}

Normally, I would have a volume mounted onto the app image that contains the static files.

However, this is becoming redundant since the app container has the static files in itself.

All the nginx container needs to do is "peer" into the app container and serve those static files. Like:

location /static/ {
    root http://app:8000/web_interface;
}

or

location /static/ {
    root app/web_interface;
}

Any chance there is a way to serve static files located in another container from a nginx container?

like image 975
Andrew Graham-Yooll Avatar asked Jul 18 '17 14:07

Andrew Graham-Yooll


People also ask

How do I serve a static file in nginx Docker?

Serving Static Files To deploy the container, use Docker Compose. The Docker Compose output. Your static folder and all of its contents are now being served at http://localhost:8080/ using Nginx running inside Docker. Our static files being served on port 8080.

Where are nginx static files stored?

To serve static files with nginx, you should configure the path of your application's root directory and reference the HTML entry point as the index file. In this example, the root directory for the snake deployment is /home/futurestudio/apps/snake which contains all the files.

Can nginx be used for serving static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

How to use nginx to serve static files?

Nginx is a good choice for serving static files because it is easy to deploy and it is easy to scale. We will use Nginx to serve our static files. You will also need to install Docker. Visit the official Docker website to get the installer. After it downloads, run the installer until the end.

Why is my Nginx not working with Docker?

Your issue isn't related to docker but to your nginx configuration. In your nginx config file, you define /var/www/ as the document root (I guess to serve your static files). But below that you instruct nginx to act as a reverse proxy to your node app for all requests.

How to serve static files in Django using Docker?

This is more of a follow up post of my previous article. Before I start, I am assuming you have successfully deployed django using docker and nginx, but having some problems serving static files. No worries, it is easy. Just follow these steps: 1. In your django settings.py file, add static file directory i.e. STATIC_ROOT=/static.

What is Nginx and how to use it?

Nginx is a popular open-source web server and reverse proxy. It is used to serve static files and to proxy requests to other servers. Nginx is a good choice for serving static files because it is easy to deploy and it is easy to scale. We will use Nginx to serve our static files. You will also need to install Docker.


Video Answer


2 Answers

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.

like image 182
Grimmy Avatar answered Sep 28 '22 07:09

Grimmy


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
  }

  ...
}
like image 44
Steve Holgado Avatar answered Sep 28 '22 07:09

Steve Holgado