11

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.

Community
  • 1
  • 1
Lion
  • 16,606
  • 23
  • 86
  • 148

2 Answers2

12

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

Community
  • 1
  • 1
Yannic Hamann
  • 4,655
  • 32
  • 50
0

I'm not sure if it's the same problem I was struggling with, but in my situation I couldn't see any errors informations from Django in my docker console using docker-compose. If for example I had a typo in any of my imports it wouldn't show me anything, my container just wouldn't get up.

What helped me was an answer from this thread and after adding 'tty: true' in my docker-compose file it got fixed. Now I can see all errors as well as regular prints.

Hope it's gonna help someone.

Piotr Wieleba
  • 409
  • 4
  • 9