Repository navigation
Bug: Testcontainer fails to fetch server api version on 4.11 but works on 4.10 #830
Copy link
Copy link
Closed
Description
Describe the bug
Hey seems like you released a new minor version 2 hours ago and it has a bug in it.
Bug: Testcontainer fails to fetch server api version on 4.11 but works on 4.10
To Reproduce
Install version 4.11 and try to run the from testcontainers.mssql import DbContainer, SqlServerContainer
>>> from testcontainers.mssql import DbContainer, SqlServerContainer
Traceback (most recent call last):
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 787, in urlopen
response = self._make_request(
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 493, in _make_request
conn.request(
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/connection.py", line 445, in request
self.endheaders()
File "/home/bartek/.local/share/uv/python/cpython-3.10.8-linux-x86_64-gnu/lib/python3.10/http/client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/home/bartek/.local/share/uv/python/cpython-3.10.8-linux-x86_64-gnu/lib/python3.10/http/client.py", line 1037, in _send_output
self.send(msg)
File "/home/bartek/.local/share/uv/python/cpython-3.10.8-linux-x86_64-gnu/lib/python3.10/http/client.py", line 975, in send
self.connect()
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/transport/unixconn.py", line 26, in connect
sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/requests/adapters.py", line 667, in send
resp = conn.urlopen(
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 841, in urlopen
retries = retries.increment(
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/util/retry.py", line 474, in increment
raise reraise(type(error), error, _stacktrace)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/util/util.py", line 38, in reraise
raise value.with_traceback(tb)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 787, in urlopen
response = self._make_request(
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 493, in _make_request
conn.request(
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/urllib3/connection.py", line 445, in request
self.endheaders()
File "/home/bartek/.local/share/uv/python/cpython-3.10.8-linux-x86_64-gnu/lib/python3.10/http/client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/home/bartek/.local/share/uv/python/cpython-3.10.8-linux-x86_64-gnu/lib/python3.10/http/client.py", line 1037, in _send_output
self.send(msg)
File "/home/bartek/.local/share/uv/python/cpython-3.10.8-linux-x86_64-gnu/lib/python3.10/http/client.py", line 975, in send
self.connect()
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/transport/unixconn.py", line 26, in connect
sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/api/client.py", line 223, in _retrieve_server_version
return self.version(api_version=False)["ApiVersion"]
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/api/daemon.py", line 181, in version
return self._result(self._get(url), json=True)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/utils/decorators.py", line 44, in inner
return f(self, *args, **kwargs)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/api/client.py", line 246, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/requests/sessions.py", line 602, in get
return self.request("GET", url, **kwargs)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/requests/adapters.py", line 682, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/testcontainers/mssql/__init__.py", line 4, in <module>
from testcontainers.core.generic import DbContainer
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/testcontainers/core/generic.py", line 16, in <module>
from testcontainers.core.container import DockerContainer
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/testcontainers/core/container.py", line 12, in <module>
from testcontainers.core.config import ConnectionMode
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/testcontainers/core/config.py", line 52, in <module>
RYUK_DOCKER_SOCKET: str = get_docker_socket()
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/testcontainers/core/config.py", line 36, in get_docker_socket
client = docker.from_env()
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/client.py", line 94, in from_env
return cls(
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/client.py", line 45, in __init__
self.api = APIClient(*args, **kwargs)
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/api/client.py", line 207, in __init__
self._version = self._retrieve_server_version()
File "/home/bartek/projects/US/framework/.venv/lib/python3.10/site-packages/docker/api/client.py", line 230, in _retrieve_server_version
raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))Runtime environment
Ubuntu, python 3.10.8
(bartek) ➜ ~ git:(main) ✗ uv pip list
Package Version
------------------ ---------
certifi 2025.6.15
charset-normalizer 3.4.2
docker 7.1.0
idna 3.10
python-dotenv 1.1.0
requests 2.32.4
testcontainers 4.11.0
typing-extensions 4.14.0
urllib3 2.4.0
wrapt 1.17.2
EDIT:
I took a dig myself to try to find the culprit
kudos to @chbndrhnns to realizing it connects
4.10
testcontainers/core/config.py: 34
RYUK_DOCKER_SOCKET: str = environ.get("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/var/run/docker.sock")
4.11
testcontainers/core/config.py:52
RYUK_DOCKER_SOCKET: str = get_docker_socket()
Where the get_docker_socket connects
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
chbndrhnns commentedon Jun 16, 2025
I think the problem is it tries to connect on import. I find that strange. I have this case for
AsyncRedisContainerwhich imported fine until 4.10. It's fine there because I am able to set a DOCKER_HOST variable and the connection attempt happens at a later point.christianplatta1012 commentedon Jun 16, 2025
same here, version 4.11 breaks it because some docker API calls already happen during import time
alexanderankin commentedon Jun 16, 2025
does #831 fix?
alexanderankin commentedon Jun 16, 2025
if someone can use the above pip command to test it on their end i would appreciate that a lot, would prefer to get a fix out sooner rather than later.
christianplatta1012 commentedon Jun 16, 2025
When I apply this patch another error occurs (I'm using colima on a Mac M1 to run docker, if this is of any importance):
christianplatta1012 commentedon Jun 16, 2025
This is the code snippet I use to start the container:
alexanderankin commentedon Jun 16, 2025
ive yankjed the release for now. need to narrow down a root cause besides the fix above then
KantiCodes commentedon Jun 17, 2025
This solved the import error
CarliJoy commentedon Jun 17, 2025
@christianplatta1012
(Could you please edit your stacktrace so that it is more readable)
Please add from
before the
with PostgresContainerand write here what the result is.Actually the fix(es) should make the
unnecessary.
Please run
get_docker_socketwithout the monkeypatch.If this doesn't return the correct host, we need to adopt it for macos.
GaetanLepage commentedon Jun 17, 2025
#832 is also a a workaround.
christianplatta1012 commentedon Jun 23, 2025
Sorry for the late reply, kids and work kept me busy :-).
I've tried to compile a minimal example that works in 4.10 but breaks in 4.11.
My dev setup:
I'm running this on an Apple M1 Pro, Sequoia 15.5
uname -a returns:
Darwin S0184554 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000 arm64
colima version:
Details
colima version 0.8.1 git commit: 96598cc5b64e5e9e1e64891642b91edc8ac49d16runtime: docker
arch: aarch64
client: v28.1.1
server: v27.1.1
colima was installed using brew.
docker version:
Details
Client: Docker Engine - Community
Version: 28.1.1
API version: 1.46 (downgraded from 1.49)
Go version: go1.24.2
Git commit: 4eba377327
Built: Fri Apr 18 09:44:47 2025
OS/Arch: darwin/arm64
Context: colima
Server: Docker Engine - Community
Engine:
Version: 27.1.1
API version: 1.46 (minimum version 1.24)
Go version: go1.21.12
Git commit: cc13f95
Built: Tue Jul 23 20:00:07 2024
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: 1.7.19
GitCommit: 2bf793ef6dc9a18e00cb12efb64355c2c9d5eb41
runc:
Version: 1.7.19
GitCommit: v1.1.13-0-g58aa920
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Testcase:
When I run this with any testcontainers version (4.10 or 4.11) I get:
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
When I run this and add at the beginning:
then this works with 4.10
When I run this with the yanked 4.11 version, I get the API version error again with the following stack trace
Details
</ details>
When I run this with the
@fix/get_docker_socket-on-importversion (and setting the two env vars), I get:When I run this with the
@fix/get_docker_socket-on-importversion and don't set any env vars, I get the API error again.When I run
with or without env vars I get the same API or 404 errors
alexanderankin commentedon Jun 23, 2025
See if #833 fixes it for you
CarliJoy commentedon Jun 23, 2025
@christianplatta1012
As written
(Do this for the yanked 4.11 version)
I don't have MacOS to test the docker Socket.
But the idea is that
get_docker_socketdetermines the correct docker socket in all circumstances.Please also post the result of:
That would help to determine what to do to automatically detect
colimaintalls of docker.Btw: Did you follow this Guide to use colima?
You need to set the docker context correctly. That should make setting the TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE unnecessary.
If you use #833 please use
Instead of
os.environ["TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE"](I am not sure ifDOCKER_HOSTenv variable is required if you configured the docker context correctly.feat: make config monkeypatchable, fix config related startup issues (#…
alexanderankin commentedon Jun 23, 2025
4.12.0 will go out this week hopefully, any feedback in the form of yes/no would be suffice for me - ive merged 833 so just let me know if the main branch works for you or not
christianplatta1012 commentedon Jun 24, 2025
Hi,
yes, I did and my current context is colima and this works with the docker cli.
I think my problem is related to this docker-py issue: docker/docker-py#3146
docker-py seems to use always the default context regardless of the current active context that is used by the docker cli.
This explains why setting the DOCKER_HOST variable to the colima socket resolves the "docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))"
Don't know if testcontainers is willing to work around this issue for Mac users. The bug was opened on Jun 22, 2023 so, don't know if there will be a fix from docker-py anytime soon...
CarliJoy commentedon Jun 24, 2025
Thanks for finding the docker-py bug.
I would advise against hacky workarounds but maybe we find a solution that just works stable detection that works in all circumstances?
What comes when you run
docker context ls --format json 2> /dev/null?For me this shows the correct context for you as well?
Maybe a solution based on something like this:
Would be okay.
christianplatta1012 commentedon Jun 24, 2025
Hi,
get_docker_contexts returns:
{'Current': True, 'Description': 'colima', 'DockerEndpoint': 'unix:///Users/christian.platta/.colima/default/docker.sock', 'Error': '', 'Name': 'colima'}, {'Current': False, 'Description': 'colima [profile=amd64_images]', 'DockerEndpoint': 'unix:///Users/christian.platta/.colima/amd64_images/docker.sock', 'Error': '', 'Name': 'colima-amd64_images'}, {'Current': False, 'Description': 'Current DOCKER_HOST based configuration', 'DockerEndpoint': 'unix:///var/run/docker.sock', 'Error': '', 'Name': 'default'}on my system. When the "'Current': True" context is used, it should work as expected
CarliJoy commentedon Jun 27, 2025
I will create a PR once I find time that should detect this and select the proper context. Already have a draft for that.
@christianplatta1012 I kindly ask you again to execute
And post the results. This helps me to create a proper solution. Which is hard not knowing how systems behave.
christianplatta1012 commentedon Jun 27, 2025
This snippet
with testcontainers version 4.10 returns:
client: <docker.client.DockerClient object at 0x106d67470>client.api.base_url: http+docker://localhostclient.api.get_adapter(client.api.base_url: <docker.transport.unixconn.UnixHTTPAdapter object at 0x106d47230>without the DOCKER_HOST or with version 4.11 in any case it causes the
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))errorwhen I do
uv pip install -U git+https://github.com/CarliJoy/testcontainers-python.git@a4d9abcc8ae47beb9e4c229243f31731249d7942to install #833 the same output is printed like with the 4.10 version.HTH
alexanderankin commentedon Jul 21, 2025
ok so i guess i am releasing 4.12 tomorrow. if there are any objections - today is the day to voice them
alexanderankin commentedon Jul 21, 2025
we can reopen the issue if i am wrong