docker-java Docker的java API

版本支持

Supports a subset of the Docker Remote API v1.37, Docker Server version since 1.12.6

<dependency>
      <groupId>com.github.docker-java</groupId>
      <artifactId>docker-java</artifactId>
      <!-- use latest version https://github.com/docker-java/docker-java/releases -->
      <version>3.X.Y</version>
</dependency>

当前最新的版本是3.1.0,可以点击这里查看最新版本。

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

wiki文档

For code examples, please look at the Wiki or Test cases

配置Docker环境

系统的可配置项及默认值如下:

  • DOCKER_HOST The Docker Host URL, e.g. tcp://localhost:2376 or unix:///var/run/docker.sock
  • DOCKER_TLS_VERIFY enable/disable TLS verification (switch between http and https protocol)
  • DOCKER_CERT_PATH Path to the certificates needed for TLS verification
  • DOCKER_CONFIG Path for additional docker configuration files (like .dockercfg)
  • api.version The API version, e.g. 1.23.
  • registry.url Your registry's address.
  • registry.username Your registry username (required to push containers).
  • registry.password Your registry password.
  • registry.email Your registry email.

There are three ways to configure, in descending order of precedence:

编程方式配置:

DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
    .withDockerHost("tcp://my-docker-host.tld:2376")
    .withDockerTlsVerify(true)
    .withDockerCertPath("/home/user/.docker/certs")
    .withDockerConfig("/home/user/.docker")
    .withApiVersion("1.30") // optional
    .withRegistryUrl("https://index.docker.io/v1/")
    .withRegistryUsername("dockeruser")
    .withRegistryPassword("ilovedocker")
    .withRegistryEmail("dockeruser@github.com")
    .build();
DockerClient docker = DockerClientBuilder.getInstance(config).build();

通过Properties (docker-java.properties)

DOCKER_HOST=tcp://localhost:2376
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/home/user/.docker/certs
DOCKER_CONFIG=/home/user/.docker
api.version=1.23
registry.url=https://index.docker.io/v1/
registry.username=dockeruser
registry.password=ilovedocker
registry.email=dockeruser@github.com

通过System Properties:

java -DDOCKER_HOST=tcp://localhost:2375 -Dregistry.username=dockeruser pkg.Main

通过 Environment

export DOCKER_HOST=tcp://localhost:2376
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=/home/user/.docker/certs
export DOCKER_CONFIG=/home/user/.docker

测试

我们来简单测试下:

    DockerClient dockerClient = createClient();

    // docker info
    Info info = dockerClient.infoCmd().exec();
    System.out.print(info);

    // 搜索镜像
    List<SearchItem> dockerSearch = dockerClient.searchImagesCmd("busybox").exec();
    System.out.println("Search returned" + dockerSearch.toString());

    // pull

    dockerClient.pullImageCmd("busybox:latest").exec(new ResultCallback<PullResponseItem>() {
        public void onStart(Closeable closeable) {

        }

        public void onNext(PullResponseItem object) {
            System.out.println(object.getStatus());
        }

        public void onError(Throwable throwable) {
            throwable.printStackTrace();
        }

        public void onComplete() {
            System.out.println("pull finished");
        }

        public void close() throws IOException {

        }
    });




    // 创建container 并测试

    // create
    CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
            .withCmd("/bin/bash")
            .exec();
    // start
    dockerClient.startContainerCmd(container.getId()).exec();
    dockerClient.stopContainerCmd(container.getId()).exec();
    dockerClient.removeContainerCmd(container.getId()).exec();

    EventsResultCallback callback = new EventsResultCallback() {
        @Override
        public void onNext(Event event) {
            System.out.println("Event: " + event);
            super.onNext(event);
        }
    };

    dockerClient.eventsCmd().exec(callback).awaitCompletion().close();

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券