浅析Java如何使用docker-java库操作DockerAPI

java

  除了 jnr-unixsocket,后来我发现有一个 Java Docker 的库使用还挺普遍的,就是 docker-java,github 有 2k 多star,使用也挺普遍的,一搜很多资料文章。

一、docker-java 配置

1、版本支持

<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>

2、配置Docker环境

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

  • DOCKER_HOST The Docker Host URL, e.g. tcp://localhost:2375 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

3、代码测试

    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();

  以上转载于:docker-java Docker 的 java API —— https://cloud.tencent.com/developer/article/1432721

二、如何操作 docker

1、第一步,开启 docker 远程服务。见这篇博客:浅析如何开启docker远程连接服务以便让Jenkins或Java等应用连接docker

  也可以看这篇博客:java使用dockerAPI (一) ——  https://blog.csdn.net/qq_43323720/article/details/104318121

2、Java 使用 dockerAPI 的安全连接方法,来保护对应端口及服务器的安全。

  看这篇博客:java使用dockerAPI (二)  ——  https://blog.csdn.net/qq_43323720/article/details/104318343

3、Java中使用dockerAPI的工具类封装以及常用的操作

  看这篇博客:java使用dockerAPI (三)  ——  https://blog.csdn.net/qq_43323720/article/details/104327315

以上是 浅析Java如何使用docker-java库操作DockerAPI 的全部内容, 来源链接: utcz.com/z/394280.html

回到顶部