Skip to content
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

fix waiting for auto_remove container #3312

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

huguesdk
Copy link

use the same logic as the cli (see here and here) to wait for the exit of a container when running one: ensure that a container run with auto_remove set to True has been removed when the function returns.

this prevents a race condition when trying to run another container with the same name directly afterwards.

the race condition can be reproduced like this:

import docker
client = docker.from_env()
while True:
    client.containers.run(
        "some_image",
        name="race_condition_test",
        command="sleep 1",
        auto_remove=True,
    )

after some time, this error happens:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/docker/api/client.py", line 275, in _raise_for_status
    response.raise_for_status()
  File "/usr/lib/python3.10/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 409 Client Error: Conflict for url: http+docker://localhost/v1.41/containers/create?name=race_condition_test

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python3.10/site-packages/docker/models/containers.py", line 876, in run
    container = self.create(image=image, command=command,
  File "/usr/lib/python3.10/site-packages/docker/models/containers.py", line 935, in create
    resp = self.client.api.create_container(**create_kwargs)
  File "/usr/lib/python3.10/site-packages/docker/api/container.py", line 440, in create_container
    return self.create_container_from_config(config, name, platform)
  File "/usr/lib/python3.10/site-packages/docker/api/container.py", line 457, in create_container_from_config
    return self._result(res, True)
  File "/usr/lib/python3.10/site-packages/docker/api/client.py", line 281, in _result
    self._raise_for_status(response)
  File "/usr/lib/python3.10/site-packages/docker/api/client.py", line 277, in _raise_for_status
    raise create_api_error_from_http_exception(e) from e
  File "/usr/lib/python3.10/site-packages/docker/errors.py", line 39, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation) from e
docker.errors.APIError: 409 Client Error for http+docker://localhost/v1.41/containers/create?name=race_condition_test: Conflict ("Conflict. The container name "/race_condition_test" is already in use by container "2f632c9f52b2f9286574972a97ee615e3714204bf84aecae399623b609948301". You have to remove (or rename) that container to be able to reuse that name.")

use the same logic as the cli to wait for the exit of a container when
running one: ensure that a container run with auto_remove set to True
has been removed when the function returns.

this prevents a race condition when trying to run another container with
the same name directly afterwards.

signed-off-by: hugues de keyzer <[email protected]>
@huguesdk huguesdk force-pushed the fix_wait_auto_remove_container branch from 825ac17 to 4418a23 Compare February 18, 2025 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant