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

I have a Python web application that is located behind Nginx and is served through Gunicorn.

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

My questions:

  • Is this a good idea or a big no for attaching a web application with static assets?

  • If I want to deploy my container on 2 servers that need access to the same assets, how can I make static assets portable like a container application?

What would I like if possible:

I would like to place my application in a container, and I would like to make it as portable as possible, without spending more money on additional resources, such as a separate server for storing images (for example, a database)

+6
source share
2 answers

If you know that your application will always and forever have the same static assets, then simply download them using the application and do it with it.

But everything changes, so when you need it, I would recommend the Docker Volume Container : put your static assets in the DVC and install this DVC in the main container, so all this is pretty much “just one application container”. You can 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 continue further by starting the container with a script loading buffer that copies the source static files to the destination path at startup. That way, your application will always have a default set to get started, but when you add an application, you can add more static files. For example, take the official Jenkins container and read /usr/local/bin/jenkins.sh .

+5
source

I agree with kojiro, if something does not change much, containerize static files with your application. Regarding your second question, it seems you think the Docker Volume Container approach is still not flexible enough since you will have multiple docker nodes. Perhaps Flocker meets your requirements? From the Flocker docs ( https://docs.clusterhq.com/en/0.3.2/ ):

Flocker allows you to move Docker containers and their data together between Linux hosts. This means that you can run your databases, queues, and keystores in Docker and move them as easily as any other application. Even stateless applications are dependent on many service states and it is currently almost impossible to launch these services in Docker containers. Flocker strives to solve this problem by providing an orchestration structure that allows you to maintain stateful and stateless containers between environments.

+4
source

Source: https://habr.com/ru/post/983466/


All Articles