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

[Enhancement]: Support different docker context

Open diegolovison opened this issue 2 years ago • 23 comments

Module

Core

Proposal

% docker context ls
NAME                  DESCRIPTION                               DOCKER ENDPOINT                                  KUBERNETES ENDPOINT                                           ORCHESTRATOR
default               Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                      https://api.xpto.net:6443 (dlovison)   swarm
desktop-linux                                                   unix:///Users/dlovison/.docker/run/docker.sock                                                                 
my-remote-engine2 *                                             ssh://[email protected]                                       

When I execute my test

16:25:20,932 ERROR (testng-ExampleJdg4931) [org.testcontainers.dockerclient.DockerClientProviderStrategy] Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
16:25:20,932 ERROR (testng-ExampleJdg4931) [org.testcontainers.dockerclient.DockerClientProviderStrategy]     UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)
16:25:20,933 ERROR (testng-ExampleJdg4931) [org.testcontainers.dockerclient.DockerClientProviderStrategy] As no valid configuration was found, execution cannot continue

diegolovison avatar Sep 12 '22 19:09 diegolovison

Require https://github.com/docker-java/docker-java/issues/1946

diegolovison avatar Sep 13 '22 18:09 diegolovison

@diegolovison I think you are using Colima, right? Check the docs on how to set the env vars.

eddumelendez avatar Oct 03 '22 23:10 eddumelendez

It would be very nice, if Colima would be detected automatically, just like Docker-Desktop is. I assume this is not a political thing 😘

t1 avatar Feb 04 '23 06:02 t1

@t1 I think this assumption is correct. My efforts so far about implementing support for Docker contexts have been well received by the maintainers. I am working on a pull request for docker-java here. Hope to be able to finish it up soon. I am also blogging a bit about my progress, starting here.

skagedal avatar Feb 04 '23 12:02 skagedal

Is there any update on this? The relevant PR in docker-java was merged a couple of weeks ago and I think they've done a release 3.3.0 with that change included.

Do we just need a new release of testcontainers-java to pick this change up?

chriskilding avatar Feb 27 '23 10:02 chriskilding

Oh cool, I missed that – indeed 3.3.0 seems to include it. And the dependency in testcontainers-java was also bumped. So yeah, I guess that's what we need!

skagedal avatar Feb 27 '23 12:02 skagedal

@eddumelendez would you be able to publish a new release? (Alternatively, when does the Testcontainers project have a release scheduled next?)

chriskilding avatar Feb 27 '23 16:02 chriskilding

I build custom release locally, but it does not work out of box, so anyway some adjustment need to be done to make it work before release

piotrwielgolaski-tomtom avatar Mar 23 '23 07:03 piotrwielgolaski-tomtom

@piotrwielgolaski-tomtom Could you provide some details on what you tried and in what you saw?

skagedal avatar Mar 23 '23 10:03 skagedal

@skagedal my scenario was simple, use docker context to connect with remote docker instance. when I rebuild latest code I wanted to give a try, but it don't work. it seems that test containers have concept of DockerClientProviderStrategy, but any of existing one will take into account context set by docker, maybe EnvironmentAndSystemPropertyClientProviderStrategy requires same extension or extra strategy need to be added

piotrwielgolaski-tomtom avatar Mar 26 '23 09:03 piotrwielgolaski-tomtom

I take another swing on this and simple change in EnvironmentAndSystemPropertyClientProviderStrategy https://github.com/piotrwielgolaski-tomtom/testcontainers-java/blob/de1324ed2800eff4da326d0c23d281399d006bc0/core/src/main/java/org/testcontainers/dockerclient/EnvironmentAndSystemPropertyClientProviderStrategy.java#L51

            case "autoIgnoringUserProperties":
                dockerClientConfig = configBuilder.build();
                applicable = dockerClientConfig.getDockerHost() != null;
                break;

make it work a bit more, but you need to opt in for dockerconfig.source=autoIgnoringUserProperties

@skagedal I noticed that you focused on docker host, but all TLS is missing, so when I use remote connection over tcp with tls it will not work. It would be lovely if docker-java also support resolution of getSSLConfig based on context

piotrwielgolaski-tomtom avatar Mar 26 '23 14:03 piotrwielgolaski-tomtom

@piotrwielgolaski-tomtom Right, there is a SkipTLSVerify boolean property in the context that also should have been taken into account. I missed/forgot that. When you say "all TLS", do you mean other things as well?

skagedal avatar Mar 31 '23 05:03 skagedal

Oh, there is a TLSMaterial property... https://docs.docker.com/engine/context/working-with-contexts/

I never found any complete documentation on this format, does anyone know what that might be?

skagedal avatar Mar 31 '23 05:03 skagedal

@skagedal The best that I can find https://github.com/docker/cli/blob/a0756c3c2cacebf5e5dc6454cc280c3ddf675176/docs/reference/commandline/context_inspect.md?plain=1#L42 I thin that this path if exist should be used for SSL config, of course if skip is not true.

piotrwielgolaski-tomtom avatar Apr 02 '23 12:04 piotrwielgolaski-tomtom

And what would it expect to find in that path?

skagedal avatar Apr 03 '23 09:04 skagedal

And what would it expect to find in that path?

I would expect that whatever docker expects to configure SSL https://github.com/skagedal/docker-java/blob/88cea47d18cce4339a202ae7ad66fe68ee00d533/docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java#LL450C58-L450C72 It should be directory with certificate

piotrwielgolaski-tomtom avatar Apr 03 '23 12:04 piotrwielgolaski-tomtom

Now that 1.8.0 is out which uses docker-java 3.3, is this supposed to work out of the box? It's not working for me but maybe I'm doing something wrong.

glasser avatar Apr 11 '23 07:04 glasser

It does not work out of box, I wrote details above.

  • to make it work with docker host and TLS it requires https://github.com/docker-java/docker-java/pull/2105
  • ignoring this it still need extra handling in test containers - snippet above

piotrwielgolaski-tomtom avatar Apr 13 '23 14:04 piotrwielgolaski-tomtom

Ah cool — is there a PR open for that last bit? Is that something I can work around as a user without forking testcontainers?

glasser avatar Apr 14 '23 00:04 glasser

While I don't know if this supports all context use cases, #6927 is sufficient to support Colima without any configuration.

glasser avatar Apr 19 '23 00:04 glasser

FYI in the latest build of Docker Desktop for macOS it no longer automatically sets/updates /var/run/docker.sock

  • To re-enable use Settings > Advanced > Allow the default Docker socket to be used (requires password).
  • If /var/run/docker.sock isn't valid then testcontainers fails with:
Could not find a valid Docker environment. Please check configuration. Attempted configurations were: ...
(Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)

If testcontainers supported contexts, I could use docker context use desktop-linux which points to a user specific socket: unix:///Users/$USER/.docker/run/docker.sock

g0t4 avatar Jun 16 '23 00:06 g0t4

what is the current status?

dominik-kovacs avatar Sep 05 '23 11:09 dominik-kovacs

In https://github.com/testcontainers/testcontainers-java/pull/5039, @kiview and @bsideup both mention it is possible to do it with custom Docker configurations. Would it be possible to please get some guidance on how to achieve this?

OscarPalafox avatar Feb 20 '24 16:02 OscarPalafox