首页 技术 正文
技术 2022年11月6日
0 收藏 643 点赞 548 浏览 2135 个字

前一篇提到了docker-java,这里介绍另一个docker client 库,Docker Client

版本兼容

兼容17.03.1~ce – 17.12.1~ce (点 [here][1]查看).

下载jar包

点击 [via Maven][maven-search]搜索和下载最新的jar包.

pom.xml配置如下:

<dependency>
<groupId>com.spotify</groupId>
<artifactId>docker-client</artifactId>
<version>LATEST-VERSION</version>
</dependency>

当前最新的是8.15.0

<dependency>
<groupId>com.spotify</groupId>
<artifactId>docker-client</artifactId>
<version>8.15.0</version>
</dependency>

使用举例

// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
final DockerClient docker = DefaultDockerClient.fromEnv().build();// Pull an image
docker.pull("busybox");// Bind container ports to host ports
final String[] ports = {"80", "22"};
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
for (String port : ports) {
List<PortBinding> hostPorts = new ArrayList<>();
hostPorts.add(PortBinding.of("0.0.0.0", port));
portBindings.put(port, hostPorts);
}// Bind container port 443 to an automatically allocated available host port.
List<PortBinding> randomPort = new ArrayList<>();
randomPort.add(PortBinding.randomPort("0.0.0.0"));
portBindings.put("443", randomPort);final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();// Create container with exposed ports
final ContainerConfig containerConfig = ContainerConfig.builder()
.hostConfig(hostConfig)
.image("busybox").exposedPorts(ports)
.cmd("sh", "-c", "while :; do sleep 1; done")
.build();final ContainerCreation creation = docker.createContainer(containerConfig);
final String id = creation.id();// Inspect container
final ContainerInfo info = docker.inspectContainer(id);// Start container
docker.startContainer(id);// Exec command inside running container with attached STDOUT and STDERR
final String[] command = {"sh", "-c", "ls"};
final ExecCreation execCreation = docker.execCreate(
id, command, DockerClient.ExecCreateParam.attachStdout(),
DockerClient.ExecCreateParam.attachStderr());
final LogStream output = docker.execStart(execCreation.id());
final String execOutput = output.readFully();// Kill container
docker.killContainer(id);// Remove container
docker.removeContainer(id);// Close the docker client
docker.close();

作者:Jadepeng

出处:jqpeng的技术记事本–http://www.cnblogs.com/xiaoqi

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,286