Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve static files from a Dockerized Python web app?

I have a Python web application that sits behind Nginx, and is served via Gunicorn.

I have configured it so that Nginx servers static files directly from the disk and it only talks to Gunicorn for static assets such as images.

My questions:

  1. Is it a good idea or a big "no" to dockerize the web app together with static assets?

  2. If I want to deploy my container in 2 servers, which need access to same assets, how can I make the static assets portable just like the containerized app?

What I'd like to have if possible:

I'd like to put my app in a container and I would like to make it as portable as possible, without spending more funds on additional resources such as a separate server to keep the images (like a DB)

like image 508
Phil Avatar asked Mar 07 '15 13:03

Phil


People also ask

How do you serve static content?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets. For more information on the options argument, see express.static.

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.


1 Answers

If you know your app will always-and-forever have the same static assets, then just containerize them with the app and be done with it.

But things change, so when you need it I would recommend a Docker Volume Container approach: put your static assets in a DVC and mount that DVC in the main container so it's all pretty much "just one app container". You could use Docker Compose something like this:

appdata:
    image: busybox
    volumes:
        - /path/to/app/static
    command: echo "I'm just a volume container"
app:
    build: .
    volumes_from:
        - appdata
    command: …

You can expand further by starting your container with a bootstrap script that copies initial static files into the destination path on startup. That way your app is guaranteed to always have a default set to get started, but you can add more static files as the app grows. For an example of this, pull the official Jenkins container and read /usr/local/bin/jenkins.sh.

like image 197
kojiro Avatar answered Oct 13 '22 00:10

kojiro