Docker error about name conflict
For some reason I was getting an error about name conflict even that no other dotrun instance was running at a time:
~/Canonical/anbox-cloud.io [main] $ dotrun
Checking for dotrun image updates...
Traceback (most recent call last):
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/docker/api/client.py", line 268, in _raise_for_status
response.raise_for_status()
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/requests/models.py", line 1021, 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=dotrun-anbox-cloud.io
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/dotrun", line 8, in <module>
sys.exit(cli())
File "/Library/Python/3.8/site-packages/dotrun.py", line 129, in cli
container = dotrun.create_container(command)
File "/Library/Python/3.8/site-packages/dotrun.py", line 107, in create_container
return self.docker_client.containers.create(
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/docker/models/containers.py", line 878, in create
resp = self.client.api.create_container(**create_kwargs)
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/docker/api/container.py", line 428, in create_container
return self.create_container_from_config(config, name)
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/docker/api/container.py", line 439, in create_container_from_config
return self._result(res, True)
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/docker/api/client.py", line 274, in _result
self._raise_for_status(response)
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/docker/api/client.py", line 270, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/Users/bartaz/Library/Python/3.8/lib/python/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 409 Client Error for http+docker://localhost/v1.41/containers/create?name=dotrun-anbox-cloud.io: Conflict ("Conflict. The container name "/dotrun-anbox-cloud.io" is already in use by container "931abce5c5396d440b64f0d0a654b76d58ca2648bfc9d06533e1763c7692f360". You have to remove (or rename) that container to be able to reuse that name.")
I fixed it by manually deleting container with "anbox-cloud" in the name.
Not sure what could cause that issue.
I just noticed I have the same issue with dotrun in other project.
There is some old exited container:

And until I delete it manually, I can't run dotrun in this project.
I think this may not be an issue anymore in 2.0.2, as timestamp is now added to the container number. But again, I had a situation when old containers were not removed automatically:
So, this is quite a common issue in my workflow.
While container names are unique now, they do reserve the port number. So running another container at the same port is impossible. And it happens a lot to me, because I use terminal inside VSCode to run dotrun, and when switching projects terminal gets detached. Docker container is still running in the background, but I lose the shell connected to it, so it never terminates and keeps the port locked.
I switch back to original project and can't dotrun again, because port is in use (previous container is still running).
I have to manually go to docker, close any running containers and try again.
I don't know if there is any good way for dotrun to detect on start if there is already another container listening on same port and either stop it automatically, or reuse it, or at least ask me if I want to stop it?