[英]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.