testcontainers / testcontainers-python

Testcontainers is a Python library that providing a friendly API to run Docker container. It is designed to create runtime environment to use during your automatic tests.

Home Page:https://testcontainers-python.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: tc.host support issue - failing to connect to dockerd if tc.host is present

alexanderankin opened this issue · comments

Describe the bug

when we added #388 it reads stale data (or even in an older format?) and fails to connect to docker, preferring to connect to a tc desktop that may not be running anymore.

this library should validate the format before consuming the value, and potentially also check for a healthy server running on that port.

To Reproduce

Add/comment/uncomment the below line in ~/.testcontainers.properties

tc.host=tcp\://127.0.0.1\:42319

Runtime environment

n/a

#524 related kind of, was thinking of consolidating PRs:

def get_docker_host() -> Optional[str]:
    tc_host = read_tc_properties().get("tc.host")

    # make sure if we have a value, that it is still valid (we can talk to it via tcp)
    if tc_host:
        try:
            with socket(AF_INET, SOCK_STREAM) as sock:
                sock.settimeout(1)
                sock.connect(*(urllib.parse.urlparse(tc_host).netloc.rsplit(":", 1)))
        except ConnectionRefusedError:
            tc_host = None

    return tc_host or os.getenv("DOCKER_HOST")