如何使用Java连接到通过<HostVMIP>:2376监听ssl / tls连接的docker守护进程?

我有一个在VM中运行的docker守护进程,并通过HOST_VM_IP:2376监听来自外部世界的安全连接。 我已经根据docker文档https://docs.docker.com/engine/security/https/生成了ca.pemcert.pemkey.pem ,并使用它们启动了docker守护进程。

我能够curl到VM端点:端口,以进行REST API调用。

我想使用ca.pemcert.pemkey.pem并使用JAVA创build安全连接。

如何使用这3个文件在java中创build一个https客户端来进行其他api调用。

我想在UI中的文本框中指定这3个文件的内容,我将在运行时以编程方式检索!

谢谢!!

只是build议,我也想答案。

为什么你想创build自己的连接器,而有伟大的docker客户端模块的Java?

考虑使用docker-java它很容易设置:

<dependency> <groupId>com.github.docker-java</groupId> <artifactId>docker-java</artifactId> <version>3.0.3</version> </dependency> 

并用许多不同的方式进行configuration:

  • 系统环境
  • 系统属性
  • 类path上的属性
  • 纲领性

你想在运行时编程创buildDockerClient,所以你需要像这样的东西:

 DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder() .withDockerHost("tcp://my-docker-host.tld:2376") .withDockerTlsVerify(true) .withDockerCertPath("/home/user/.docker/certs") // here is the place where your certificates are located .withDockerConfig("/home/user/.docker") .withApiVersion("1.23") .withRegistryUrl("https://index.docker.io/v1/") .withRegistryUsername("dockeruser") .withRegistryPassword("ilovedocker") .withRegistryEmail("dockeruser@github.com") .build(); DockerClient docker = DockerClientBuilder.getInstance(config).build(); 

顺便说一句, CertificateUtils还检查是否存在已定义的path中的证书,并有许多伟大的functiondocker,它已经实施。

 public static boolean verifyCertificatesExist(String dockerCertPath) { String[] files = {"ca.pem", "cert.pem", "key.pem"}; boolean result = true; for (String file : files) { File path = new File(dockerCertPath, file); result &= path.exists(); } return result; }