Docker Client (another java docker client api)
前一篇提到了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
出处:https://www.cnblogs.com/xiaoqi/p/java-Docker-Client.html
版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际(欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接) 」知识共享许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步