3

I have a docker daemon running in a VM and listening to secure connections from outside world over HOST_VM_IP:2376 . I have generated the ca.pem , cert.pem and key.pem as per docker documentation https://docs.docker.com/engine/security/https/ and started the docker daemon using them.

I am able to curl to the vm endpoint:port to make the REST api calls.

I want to use the ca.pem , cert.pem and key.pem and create a secure connection using JAVA .

How do I use these 3 files to create a https client in java to make rest api calls.

I want to specify the contents of those 3 pem files in a textbox in UI, which i will retrieve programmatically at runtime !!

Thanks!!

1 Answers1

1

Just suggestion and I think the answer also.

Why you want to create your own connector while there is great docker client module for java?

Consider using docker-java it is very easy to setup:

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

and configure with many different ways:

  • System Environment
  • System properties
  • properties on class path
  • programmatic

You want to create DockerClient programatically at runtime so you will need something like:

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 also checks whether certificates in defined path exist and there are many great features for docker and it is already implemented.

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;
}
VladoDemcak
  • 4,893
  • 4
  • 35
  • 42