Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manage.py doesn't log to stdout/stderr in Docker on Raspberry Pi

On a Raspberry Pi 2, I used the image resin/rpi-raspbian:stretch for running a Django application. In my Dockerfile, I Install the python3 package and start the application using ENTRYPOINT python3 manage.py runserver 0:8000. This works, BUT when my code throws error, I got NO output using docker log command.

Example

I have a ImportError. When I run the command manually using docker exec, I got the exception as expected:

pi@pi2:/etc/docker/container/pms $ sudo docker exec -it pms_app_1 python3 manage.py runserver 0:8000
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x75de3228>
[...]
ImportError: No module named 'ws4redisfrontend'

But when I run the container using docker-compose and then open the logs, they're empty:

pi@pi2:/myapp $ sudo docker logs myapp_1
pi@pi2:/myapp $ 

This behaviour is only present for the manage.py call. For example, when I extend the entrypoint like this:

ENTRYPOINT python3 -c "print('printed by python inline script')" && python3 manage.py runserver 0:8000

I see printed by python inline script in the container-logs. As a newbie in Python/Django, I can't understand why this happens. But as my print example works, it seems a Django issue, and no general problem of Python. What am I missing? Debug mode is activated in settings.py.

like image 239
Lion Avatar asked Oct 16 '17 18:10

Lion


People also ask

Does Docker logs show stderr?

By default, docker logs shows the command's STDOUT and STDERR .


1 Answers

docker logs shows by default the I/O streams STDOUT and STDERR. Check if Django logging in your settings.py is properly configured.

For example:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler'
        },
    },
    'loggers': {
        '': {  # 'catch all' loggers by referencing it with the empty string
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    },
}

Source

Also worth to mention:

Changed in Django 1.10:

In older versions, log messages were written to sys.stderr instead of being handled through Python logging.

https://docs.djangoproject.com/en/1.11/ref/django-admin/#runserver

like image 140
Yannic Hamann Avatar answered Sep 21 '22 12:09

Yannic Hamann