java docker client_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配置如下:

com.spotify

docker-client

LATEST-VERSION

当前最新的是8.15.0

com.spotify

docker-client

8.15.0

使用举例

// 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> portBindings = new HashMap<>();

for (String port : ports) {

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以的,以下是一段Java操作Docker的代码: ``` import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.CreateContainerResponse; import com.github.dockerjava.api.model.ExposedPort; import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.PortBinding; import com.github.dockerjava.core.DockerClientBuilder; import com.github.dockerjava.core.command.ExecStartResultCallback; public class DockerClientJavaDemo { public static void main(String[] args) { DockerClient dockerClient = DockerClientBuilder.getInstance().build(); // 创建一个容器 CreateContainerResponse container = dockerClient.createContainerCmd("nginx").exec(); // 启动容器 dockerClient.startContainerCmd(container.getId()).exec(); // 获取容器的IP地址 String containerIpAddress = dockerClient.inspectContainerCmd(container.getId()).exec().getNetworkSettings().getIpAddress(); // 在容器内执行命令 String[] command = {"sh", "-c", "echo Hello, Docker!"}; HostConfig hostConfig = new HostConfig().withPortBindings(new PortBinding(new ExposedPort(80), new Binding("0.0.0.0", "8080"))); dockerClient.execCreateCmd(container.getId()).withCmd(command).withAttachStdout(true).withAttachStderr(true).withHostConfig(hostConfig).exec(new ExecStartResultCallback()).awaitCompletion(); // 停止容器 dockerClient.stopContainerCmd(container.getId()).exec(); // 删除容器 dockerClient.removeContainerCmd(container.getId()).exec(); } } ``` 这段代码演示了如何使用Java操作Docker,包括创建容器、启动容器、在容器内执行命令、停止容器和删除容器。需要注意的是,需要在本机上安装并运行Docker,才能正常执行这段代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值