Docker 提供 Docker Engine API 来和Docker Daemon进行交互,Docker Engine API 可以通过curlwget 或者其他HTTP库直接发起RESTful API请求来进行调用。 目前Docker 官方提供了对PythonGo等语言的SDK支持,其他语言的SDK也有比较多的一些开源实现,详见Docker Engine API SDKs

本文主要介绍Docker Engine API 的 Java SDK: docker-java 的集成使用。docker-java 的源码地址见:https://github.com/docker-java/docker-java

Dependencies

使用docker-java 至少需要增加以下两个依赖:

  1. DockerClient 的核心库 com.github.docker-java:docker-java-core
  2. 和Docker daemon交互的transport层。Available Transports 
        <dependency>
            <groupId>com.github.docker-java</groupId>
            <artifactId>docker-java</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>com.github.docker-java</groupId>
            <artifactId>docker-java-transport-httpclient5</artifactId>
            <version>3.2.7</version>
        </dependency>

初始化DockerClientConfig

DockerClientConfig 用来配置访问Docker Daemon 的一些信息,如访问证书、Docker Registry、拉取镜像的秘钥等等。

DockerClientConfig custom = DefaultDockerClientConfig.createDefaultConfigBuilder()
    .withDockerHost("tcp://docker.somewhere.tld:2376")
    .withDockerTlsVerify(true)
    .withDockerCertPath("/home/user/.docker")
    .withRegistryUsername(registryUser)
    .withRegistryPassword(registryPass)
    .withRegistryEmail(registryMail)
    .withRegistryUrl(registryUrl)
    .build();

构建 DockerHttpClient

httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .sslConfig(config.getSSLConfig())
                .build();

拉取镜像使用示例

    @Test
    public void test_DockerClientPullImage() {
        DockerClient dockerClient = DockerClientImpl.getInstance(config, httpClient);

        // docker image pull
        try {
            dockerClient.pullImageCmd(testImage).exec(new ResultCallbackTemplate<ResultCallback<PullResponseItem>, PullResponseItem>() {
                @Override
                public void onNext(PullResponseItem pullResponseItem) {
                    System.out.println(pullResponseItem.toString());
                }
            }).awaitCompletion();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

运行一个容器并且返回结果使用示例

    @Test
    public void test_DockerClientExecCmdWithResult() {
        DockerClient dockerClient = DockerClientImpl.getInstance(config, httpClient);

        // 创建container 并测试
        // create
        HostConfig hostConfig = new HostConfig().withNetworkMode("host");
        CreateContainerResponse container = dockerClient.createContainerCmd(testImage)
                .withHostConfig(hostConfig)
                .withEntrypoint("ls")
                .exec();
        System.out.println(container.getId());
        // start
        dockerClient.startContainerCmd(container.getId()).exec();
        WaitContainerResultCallback waitContainerResultCallback = dockerClient.waitContainerCmd(container.getId()).exec(new WaitContainerResultCallback(){});
        System.out.println("hello:" + waitContainerResultCallback.awaitStatusCode());

        List<String> result = new ArrayList<>();
        try {
            dockerClient.logContainerCmd(container.getId())
                    .withStdOut(Boolean.TRUE)
                    .withStdErr(Boolean.TRUE)
                    .exec(new ResultCallbackTemplate<ResultCallback<Frame>, Frame>() {
                        @Override
                        public void onNext(Frame frame) {
                            result.add(new String(frame.getPayload()).trim());
                        }
                    }).awaitCompletion();
            Assert.assertNotNull(result);
            System.out.println("hello2:" + result.toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        dockerClient.stopContainerCmd(container.getId()).exec();
        dockerClient.removeContainerCmd(container.getId()).exec();
    }