简体   繁体   中英

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)

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 .

I agree with kojiro, if things do not change much, containerize the static files with your app. Regarding your second question, it seems that you think the Docker Volume Container approach is still not flexible enough since you will have multiple docker hosts. Maybe Flocker addresses your requirements? From the Flocker docs ( https://docs.clusterhq.com/en/0.3.2/ ):

Flocker lets you move your Docker containers and their data together between Linux hosts. This means that you can run your databases, queues and key-value stores in Docker and move them around as easily as the rest of your app. Even stateless apps depend on many stateful services and currently running these services in Docker containers in production is nearly impossible. Flocker aims to solve this problem by providing an orchestration framework that allows you to port both your stateful and stateless containers between environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM