docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

fix waiting for auto_remove container

Open huguesdk opened this issue 10 months ago • 6 comments

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.")

huguesdk avatar Feb 18 '25 13:02 huguesdk

Hi. Why CI is not executed here ?

legalsylvain avatar Apr 12 '25 07:04 legalsylvain

@legalsylvain i think it is because someone should approve this change first. i’ve just pushed some new changes to improve the code even more.

huguesdk avatar Apr 12 '25 16:04 huguesdk

hi docker-py contributors. (@thaJeztah, @milas)

Could you take a look on that PR ? Thanks a lot !

legalsylvain avatar May 28 '25 12:05 legalsylvain

Looks like CI didn't trigger; let me try a quick close/reopen

thaJeztah avatar May 28 '25 12:05 thaJeztah

@huguesdk : could you take a look on red CI ?

legalsylvain avatar May 28 '25 14:05 legalsylvain

@legalsylvain i adjusted the tests to make them pass.

@thaJeztah do you think i should add tests for the new code?

huguesdk avatar May 30 '25 13:05 huguesdk