[PYTHON] Output log to console with Flask + Nginx on Docker

Premise

Normally, I recognize that it is better to set up a uwsgi server in addition to flask and nginx, but it is the result of doing what can be done from the current configuration.

background

I set up a Flask + Nginx server on Docker, but the Flask log is only output to a file, and I can't check it at the prompt for docker run or docker logs. I want to be able to do it somehow.

Current configuration

I will omit it considerably, but it is as follows.

Dockerfile


FROM centos:centos7

CMD ["/bin/bash", "./start.sh"]

start.sh


# !/bin/bash

uwsgi --ini=uwsgi.ini
nginx -g "daemon off;"

uwsgi.ini


daemonize = /var/log/uwsgi/uwsgi.log

In the above state, the uwsgi process will be daemonized and run, and Nginx will run as a foreground job. And all the logs are output to /var/log/uwsgi/uwsgi.log specified by daemonize.

Correspondence

I tried a few things, but it didn't work, and as a result, the third method worked.

Explicitly add console output on flask

I think that the console output has already been done, but check if it can be done by adding an additional stream of console output.

logger.addHandler(logging.StreamHandler())

However, daemonize seems to send what is output as standard to a file, and the contents of the added stream are also output to a file.

Set daemonize setting to / dev / stdout

daemonize outputs standard output and error output to the specified file, but I dared to try to output to / dev / stdout to make it standard output. I was able to output to the console output successfully, but the log rotation function of flask goes to check the file, and an error message is constantly displayed. I searched for a way to suppress the log rotation function, but I gave up because I could not find it.

uwsgi_check_logrotate()/lseek(): Illegal seek [core/logging.c line 494]

Monitor log files with tail

Added tail monitoring on the startup shell. Confirm that the log can be output to both the file and the console. I need to match the output files in both start.sh and uwsgi.ini, but I was able to do it for the time being.

start.sh


#Additional part
touch /var/log/uwsgi/uwsgi.log
tail -F /var/log/uwsgi/uwsgi.log > /dev/stdout &

uwsgi --ini=uwsgi.ini
nginx -g "daemon off;"

Recommended Posts

Output log to console with Flask + Nginx on Docker
Output Python log to console with GAE
How to log in to Docker + NGINX
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 1]
(Note) Notes on building TensorFlow + Flask + Nginx environment with Docker Compose
Launch Flask application with Docker on Heroku
How to delete log with Docker, not to collect log
Output python log to both console and file
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Output debug log with GAE dev_appserver.py on Eclipse + PyDev
From installing Flask on CentOS to making it a service with Nginx and uWSGI
API with Flask + uWSGI + Nginx
Output to syslog with Loguru
Output large log with discord.py
From environment construction to deployment for flask + Heroku with Docker
Until API made with Flask + MySQL is converted to Docker
Post Jenkins console output to Slack
Make it possible to output a log to a file with go echo
Creating a Flask server with Docker
Output to csv file with Python
Output cell to file with Colaboratory
Unit test log output with python
How to overwrite the output to the console
It's time to install DB with Docker! DB installation for beginners on Docker
Try to output audio with M5STACK
Application development with Docker + Python + Flask
I tried to draw a system configuration diagram with Diagrams on Docker
Wrapper when you want to output utf-8 + ansi color on Windows console
I want to output to the console coolly
Link WAS and FLASK applications on docker
Send CSS compressed to Gzip with Flask
Connect to MySQL with Python within Docker
Create Python + uWSGI + Nginx environment with Docker
I was addicted to Flask on dotCloud
How to get colored output to the console
Nginx setting summary (Flask + Docker + Gunicorn edition)
Create a web service with Docker + Flask
Output user information etc. to Django log
Build NGINX + NGINX Unit + MySQL environment with Docker
How to install python3 with docker centos
Build a flask app made with tensorflow and dlib to work on centos7
How to upload with Heroku, Flask, Python, Git (4)
Log in to the remote server with SSH
A memorandum to make WebDAV only with nginx
Build Django + NGINX + PostgreSQL development environment with Docker
Setting to output the log of cron execution
Vienna with Python + Flask web app on Jenkins
Python log is not output with docker-compose up
Hello World with nginx + uwsgi + python on EC2
Run Python web apps on NGINX + NGINX Unit + Flask
I tried to output LLVM IR with Python
Create Nginx + uWSGI + Python (Django) environment with docker
Log in to Yahoo Business with Selenium Python
Start Nginx with docker without putting Nginx in CentOS8
Easily log in to AWS with multiple accounts
How to install OpenGM on OSX with macports
Introduction to Python with Atom (on the way)
Send msgpack with ajax to flask (werkzeug) environment
I want to log file I / O on Linux
Output test function docstring to report with pytest-html
[CentOS8] How to output Python standard output to systemd log