Skip to content

Bug: K3SContainer does not support other connection modes (in get_container_host_ip) #861

Open

Description

@ndellosa95

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.

Activity

  1. alexanderankin commented on Aug 13, 2025

    @alexanderankin
    Member
  2. alexanderankin commented on Aug 13, 2025

    @alexanderankin
    Member
  3. alexanderankin commented on Aug 14, 2025

    @alexanderankin
    Member
  4. marbon87 commented on Sep 11, 2025

    @marbon87

    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
    
  5. alexanderankin commented on Sep 11, 2025

    @alexanderankin
    Member

    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

  6. changed the title [-]Bug: `K3SContainer` calls `get_container_host_ip` in constructor before container is started[/-] [+]Bug: `K3SContainer` does not support DinD (in `get_container_host_ip`)[/+] on Sep 11, 2025
  7. changed the title [-]Bug: `K3SContainer` does not support DinD (in `get_container_host_ip`)[/-] [+]Bug: `K3SContainer` does not support other connection modes (in `get_container_host_ip`)[/+] on Sep 11, 2025
  8. marbon87 commented on Sep 12, 2025

    @marbon87

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions