testcontainers-python icon indicating copy to clipboard operation
testcontainers-python copied to clipboard

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

Open alexanderankin opened this issue 1 year ago • 1 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

alexanderankin avatar Mar 24 '24 02:03 alexanderankin

#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")

alexanderankin avatar Apr 02 '24 13:04 alexanderankin