testcontainers / testcontainers-go

Testcontainers for Go is a Go package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests. The clean, easy-to-use API enables developers to programmatically define containers that should be run as part of a test and clean up those resources when the test is done.
https://golang.testcontainers.org
MIT License
3.3k stars 450 forks source link

[Enhancement]: support Docker contexts #2607

Open ash2k opened 1 week ago

ash2k commented 1 week ago

Proposal

Add support for Docker contexts. Currently the code below doesn't look at contexts:

https://github.com/testcontainers/testcontainers-go/blob/e22f2faa2e1accc96b2a00662005fcc17016497d/internal/core/docker_host.go#L99-L124

I'm running Rancher Desktop and the Docker unix socket does not exist at the usual location. Hence, test containers fails to find it. I'm getting an error Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?: failed to create container.

My current context is a different one and the socket file is somewhere else:

$ docker context list
NAME                DESCRIPTION                               DOCKER ENDPOINT                      ERROR
default             Current DOCKER_HOST based configuration   unix:///var/run/docker.sock
rancher-desktop *   Rancher Desktop moby context              unix:///Users/mike/.rd/docker.sock

If I set DOCKER_HOST=unix:///Users/mike/.rd/docker.sock, everything works fine. But this shouldn't be necessary.

If this enhancement is implemented, the code should probably print the current context name (for debugging), not just the path to the socket file.

mdelapenya commented 6 days ago

Hi @ash2k thanks for opening this issue. There existed another issue on this, https://github.com/testcontainers/testcontainers-go/issues/815, but it was closed as Done as it just targeted colima.

I think we can keep this issue as reference for the context awareness.

On the other hand, I can confirm we would need to tackle this initiative in a cross-lang manner, as it should be implemented in a similar way for all the Testcontainers libraries, to keep the seamless experience we want to provide.

So please let me put this in the back burner until we have clear lines on how to abstract the interactions with the Docker context, so that it's "implementable" across all languages. Hope you understand.

In any case, I encourage you to open a discussion a draft a design doc if you're interested; I'm pretty sure it will help us drive the conversation from Go to other languages.

Cheers!