docker logs prints nothing for a Flask or Python container
docker logs returns an empty result for a running containerShort answer
docker logs only shows what the process writes to standard output and standard error. A Python app that buffers its output shows nothing until the buffer fills, and a container started with the none logging driver never stores any logs. Set PYTHONUNBUFFERED=1 and check the logging driver.
Fix it fast
1Find out which cause you have
Ask Docker which logging driver the container uses. If it says none, no logs exist; otherwise the problem is in how the app writes.
docker inspect -f '{{.HostConfig.LogConfig.Type}}' my-flask2Match what you see, then jump to its fix
What does the error look like?
docker logs returns an empty result for a running containerthe output appears only after the app exits, or in large delayed burstsprint() lines show up on the terminal with docker run but not in docker logs
Cause 1 of 2
Python buffers its output when it is not attached to a terminal
1Check
Check whether the container sets PYTHONUNBUFFERED. The Python docs say a non-empty value forces the stdout and stderr streams to be unbuffered, the same as python -u.
docker exec my-flask printenv PYTHONUNBUFFERED2Fix
Set the variable in the Dockerfile, or start Python with -u.
# Dockerfile
ENV PYTHONUNBUFFERED=1
# or
CMD ["python", "-u", "app.py"]3Verify
Rebuild, run, and follow the log while you call an endpoint.
docker build -t my-flask .
docker run -d --name my-flask -p 5000:5000 my-flask
docker logs -f my-flaskConfirmed by docs.python.org
Cause 2 of 2
The container uses the none logging driver
1Check
With the none driver, Docker stores nothing, so docker logs has nothing to return. It can come from a --log-driver flag or from the daemon default.
docker inspect -f '{{.HostConfig.LogConfig.Type}}' my-flask2Fix
Recreate the container with the default json-file driver, or set a driver in the run command. A daemon.json change only applies to containers created after the restart.
docker rm -f my-flask
docker run -d --name my-flask --log-driver json-file -p 5000:5000 my-flask3Verify
Send a request to the app, then read the log.
curl -s localhost:5000/ > /dev/null
docker logs my-flaskConfirmed by docs.docker.com
How do I stop it coming back?
- Set PYTHONUNBUFFERED=1 in every Python image you build.
- Write application logs to stdout or stderr, not to a file inside the container, because docker logs shows only those two streams.
- Check the logging driver after changing daemon.json, and recreate old containers.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
docker logs fails because the logging driver cannot be read back
Error response from daemon: configured logging driver does not support reading
docker logs can only read from a driver that stores logs locally.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
pip install cryptography tries to compile and fails in a Docker build
fatal error: 'openssl/aes.h' file not found
cryptography ships prebuilt wheels.
3 causes, each with a fixServers
Docker build fails with apk: not found in a Python or Ubuntu image
/bin/sh: 1: apk: not found
apk is the package manager of Alpine Linux only.
2 causes, each with a fixServers
Docker cannot find manage.py or Permission denied on '/manage.py'
PermissionError: [Errno 13] Permission denied: '/manage.py'
The container is looking for manage.py in a directory where the file is not.
3 causes, each with a fixServers
Compose cannot start the service: exec "python manage.py runserver" failed
starting container process caused: exec: "python manage.py runserver
The container runtime was asked to run a program literally named 'python manage.py runserver', including the spaces.
2 causes, each with a fix