docker / docker-py

A Python library for the Docker Engine API

Home Page:https://docker-py.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Capturing container logs for a very-short-lived detached container

exposedcranium opened this issue · comments

There seems to be a race condition where the logs generator container.logs() returns a HTTPError: 404 Not Found when the container runs and quits very quickly and detach=True is used. It may also be related to rm=True, but I haven't checked.

I would expect container.logs() to remain valid and yield log lines even if the container has terminated. I currently have to add a && sleep 1 to the run command to prevent this from happening.

This is working as designed - if you have rm=True, the container will be removed on exit, so it's expected the logs won't be available after.

A common pattern here with the Docker API:

  • Create container
  • Attach to logs
  • Start container

Ordering that way ensures that there's no race between container start and log tailing.

Alternatively, don't use rm=True and manually delete the container after reading the logs. (But note that this means you might "leak" a container if your Python app crashes, for example, since the Engine will no longer be responsible for the removal.)