Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

docker logs prints nothing for a Flask or Python container

Errordocker logs returns an empty result for a running container

Short 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.

  • Applies to Docker Engine 28, Python 3.13, Flask 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-flask
  2. 2Match what you see, then jump to its fix

    1. 1The driver is json-file, and the lines appear late or only when the process stopsPython buffers its output when it is not attached to a terminalFix
    2. 2docker inspect prints noneThe container uses the none logging driverFix

What does the error look like?

  • docker logs returns an empty result for a running container
  • the output appears only after the app exits, or in large delayed bursts
  • print() 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 PYTHONUNBUFFERED

2Fix

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-flask

Confirmed by docs.python.org

Did this fix it?

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-flask

2Fix

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-flask

3Verify

Send a request to the app, then read the log.

curl -s localhost:5000/ > /dev/null
docker logs my-flask

Confirmed by docs.docker.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.