Bug: `K3SContainer` does not support other connection modes (in `get_container_host_ip`)
Describe the bug
K3SContainer calls get_container_host_ip in its __init__ method here and here before the container is started, causing it to fail with an AssertionError here.
To Reproduce
from testcontainers.k3s import K3SContainer
with K3SContainer() as k3s:
print("gonna throw an error before I even get here :(")
Runtime environment
I'm on an ARM Mac and this is in Python 3.12, but that shouldn't make a difference.
If there is a bug, it was probably introduced with the new waiting strategies, thanks for reporting, I'll take a look
On Wed, Aug 13, 2025, 5:19 PM ndellosa95 @.***> wrote:
ndellosa95 created an issue (testcontainers/testcontainers-python#861) https://github.com/testcontainers/testcontainers-python/issues/861
Describe the bug
K3SContainer calls get_container_host_ip in its init method here and here https://github.com/testcontainers/testcontainers-python/blob/main/modules/k3s/testcontainers/k3s/__init__.py#L45-L46 before the container is started, causing it to fail with an AssertionError here https://github.com/testcontainers/testcontainers-python/blame/main/core/testcontainers/core/container.py#L229-L231 .
To Reproduce
from testcontainers.k3s import K3SContainer with K3SContainer() as k3s: print("gonna throw an error before I even get here :(")
Runtime environment
I'm on an ARM Mac and this is in Python 3.12, but that shouldn't make a difference.
— Reply to this email directly, view it on GitHub https://github.com/testcontainers/testcontainers-python/issues/861, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACECGJGKK2FYZLF55ITU6ML3NOTVJAVCNFSM6AAAAACD23I4WKVHI2DSMVQWIX3LMV43ASLTON2WKOZTGMYTSOJTGA3TQNY . You are receiving this because you are subscribed to this thread.Message ID: @.***>
oh, seems like we have to re-think container modes - or k3s - the new container modes that try to help address dind/dood are not compatible with the k3s container. ill move the assert inside the if branches that need it, and then it will work by default (not dind or dood or whatever0
On Wed, Aug 13, 2025 at 6:07 PM David Ankin @.***> wrote:
If there is a bug, it was probably introduced with the new waiting strategies, thanks for reporting, I'll take a look
On Wed, Aug 13, 2025, 5:19 PM ndellosa95 @.***> wrote:
ndellosa95 created an issue (testcontainers/testcontainers-python#861) https://github.com/testcontainers/testcontainers-python/issues/861
Describe the bug
K3SContainer calls get_container_host_ip in its init method here and here https://github.com/testcontainers/testcontainers-python/blob/main/modules/k3s/testcontainers/k3s/__init__.py#L45-L46 before the container is started, causing it to fail with an AssertionError here https://github.com/testcontainers/testcontainers-python/blame/main/core/testcontainers/core/container.py#L229-L231 .
To Reproduce
from testcontainers.k3s import K3SContainer with K3SContainer() as k3s: print("gonna throw an error before I even get here :(")
Runtime environment
I'm on an ARM Mac and this is in Python 3.12, but that shouldn't make a difference.
— Reply to this email directly, view it on GitHub https://github.com/testcontainers/testcontainers-python/issues/861, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACECGJGKK2FYZLF55ITU6ML3NOTVJAVCNFSM6AAAAACD23I4WKVHI2DSMVQWIX3LMV43ASLTON2WKOZTGMYTSOJTGA3TQNY . You are receiving this because you are subscribed to this thread.Message ID: @.***>
https://github.com/testcontainers/testcontainers-python/pull/862
Still having the same error with 4.13.0:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv/lib/python3.12/site-packages/testcontainers/k3s/__init__.py:45: in __init__
self.with_env("K3S_URL", f"https://{self.get_container_host_ip()}:{self.KUBE_SECURE_PORT}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <testcontainers.k3s.K3SContainer object at 0x7f087cac1250>
def get_container_host_ip(self) -> str:
connection_mode: ConnectionMode
connection_mode = self.get_docker_client().get_connection_mode()
if connection_mode == ConnectionMode.docker_host:
return self.get_docker_client().host()
elif connection_mode == ConnectionMode.gateway_ip:
# mypy:
container = self._container
> assert container is not None
^^^^^^^^^^^^^^^^^^^^^
E AssertionError
the fact that k3scontainer does not support other connection modes is not an issue with the core library, thats an issue with the community module. PR's welcome. before we introduced this change, this should have never worked with dind in the first place - connection modes allow for use of gateway_ip specifically. so in this case you can set the connection mode to docker_host and see if that works, otherwise we have to think together about how to solve this - getting the container's private ip before it exists - dont think this is possible. I believe the earlier behavior was just using connection mode = docker host
Hi @alexanderankin ,
setting the conncetion-mode to docker_host with
os.environ["TESTCONTAINERS_CONNECTION_MODE"] = "docker_host"
from testcontainers.k3s import K3SContainer
does solve the problem.