[英]How to connect to a docker daemon listening to ssl/tls connections over <HostVMIP>:2376 using Java?
我有一个在VM中运行的docker守护程序,并通过HOST_VM_IP侦听来自外部世界的安全连接:2376。 我根据docker文档https://docs.docker.com/engine/security/https/生成了ca.pem , cert.pem和key.pem ,并使用它们启动了docker守护程序。
我能够卷曲到vm端点:端口来进行REST api调用。
我想使用ca.pem , cert.pem和key.pem并使用JAVA创建安全连接。
如何使用这3个文件在java中创建https客户端以进行rest api调用。
我想在UI中的文本框中指定这3个pem文件的内容,我将在运行时以编程方式检索!
谢谢!!
只是建议,我认为答案也是。
为什么你想创建自己的连接器,而有一个伟大的docker客户端模块的Java?
考虑使用docker-java,它很容易设置:
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.0.3</version>
</dependency>
并配置许多不同的方式:
您希望在运行时以编程方式创建DockerClient,因此您需要以下内容:
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();
BTW, CertificateUtils还会检查定义路径中的证书是否存在,并且docker有很多很棒的功能,并且已经实现了。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.