A widespread configuration for Django/Flask applications includes Nginx as webserver and uWSGI for serving the web app. It’s also highly typical to manage this multi-process deployment using the Supervisor process manager — as it provides advanced process management and logging configuration.
Docker expects a single primary process. The container’s primary process controls the container lifecycle. Whenever the main process exits, the container exits, and whatever the primary process outputs to its stdout becomes the container log.
Many resources out there explain the reasoning behind using this stack configuration and the best practices in wiring the different pieces into a production-grade Dockerfile. However, the missing piece in most of them is the logging part — how to redirect the Nginx and uWSGI logs to the container log with the ability to separate between them based on the message process origin.
The following demonstrates a logging configuration of a flask container with uWSGI and Nginx in a docker-compose using Supervisor, Syslog-ng and FluentD
uWSGI and Nginx logs to Syslog which routes its output to Supervisor stdout, becoming the container log
For a complete example of Flask container, see the following repo:
Contribute to assafk/uwsgi-docker-logging development by creating an account on GitHub.
Supervisor is a multi-process manager for Linux with enhanced logging support. The most basic configuration for our scenario has Supervisor launching and monitoring Nginx and uWSGI processes, as reflected in the following supervisor configuration file (supervisor-app.conf):
[supervisord]
nodaemon = true
[program:nginx]
command = /usr/sbin/nginx
[program:uwsgi]
command = /usr/local/bin/uwsgi --ini /opt/app/uwsgi.ini
The following Dockerfile installs the python dependencies, Supervisor, and Nginx and sets Supervisor as the primary process running in a non-daemon mode.
#fluentd #docker #uwsgi #nginx