Skip to content

fix waiting for auto_remove container #3312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion docker/models/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,24 @@ def run(self, image, command=None, stdout=True, stderr=False,
stdout=stdout, stderr=stderr, stream=True, follow=True
)

exit_status = container.wait()['StatusCode']
if kwargs.get('auto_remove'):
wait_condition = 'removed'
else:
# the wait condition should theoretically be 'next-exit' (as is
# used by the cli), but it may have exited already if its run time
# was very short, which would cause the wait to hang.
# 'not-running' works in both cases.
wait_condition = 'not-running'
try:
exit_status = container.wait(condition=wait_condition)['StatusCode']
except NotFound:
if wait_condition == 'removed':
# it has been already removed, which is why it was not found,
# so everything fine here. unfortunately, there is no way to
# have its real exit status, so assume success.
exit_status = 0
else:
raise
if exit_status != 0:
out = None
if not kwargs.get('auto_remove'):
Expand Down