Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]: support Docker contexts #2607

Open
ash2k opened this issue Jun 26, 2024 · 5 comments
Open

[Enhancement]: support Docker contexts #2607

ash2k opened this issue Jun 26, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@ash2k
Copy link
Contributor

ash2k commented Jun 26, 2024

Proposal

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

// extractDockerHost Extracts the docker host from the different alternatives, without caching the result.
// This internal method is handy for testing purposes.
func extractDockerHost(ctx context.Context) string {
dockerHostFns := []func(context.Context) (string, error){
testcontainersHostFromProperties,
dockerHostFromEnv,
dockerHostFromContext,
dockerSocketPath,
dockerHostFromProperties,
rootlessDockerSocketPath,
}
outerErr := ErrSocketNotFound
for _, dockerHostFn := range dockerHostFns {
dockerHost, err := dockerHostFn(ctx)
if err != nil {
outerErr = fmt.Errorf("%w: %w", outerErr, err)
continue
}
return dockerHost
}
// We are not supporting Windows containers at the moment
return DockerSocketPathWithSchema
}

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.

@ash2k ash2k added the enhancement New feature or request label Jun 26, 2024
@mdelapenya
Copy link
Member

mdelapenya commented Jun 27, 2024

Hi @ash2k thanks for opening this issue. There existed another issue on this, #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!

@RafalSkolasinski
Copy link

I think that #815 was incorrectly marked as Done as I still had to resolve to

sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock

workaround

@mdelapenya
Copy link
Member

@RafalSkolasinski would you like to contribute it to the docs?

@RafalSkolasinski
Copy link

@mdelapenya the only contribution to docs now could be linking this issue on https://golang.testcontainers.org/system_requirements/using_colima/ to warn people that this feature is currently not working correctly, as reading

As of Colima v0.4.0 it's recommended to set the active Docker context to use
Colima. After the context is set _Testcontainers for Go_ will automatically be
configured to use Colima.

one would expect it should all be fine

@Shikachuu
Copy link

Changing just the context is indeed broken.

However here is a workaround to avoid linking:

DOCKER_HOST=$(docker context inspect | jq ".[0].Endpoints.docker.Host" -r) \
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=$(docker context inspect | jq ".[0].Endpoints.docker.Host" -r) \
go test $(FLAGS) -cover ./...

this gets the current docker context's endpoint and sets it as an env var

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants