前一篇提到了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

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

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

Docker Client (another java docker client api)的更多相关文章

  1. Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...

  2. .net core 微服务架构-docker的部署-包括网关服务(Ocelot)+认证服务(IdentityServer4)+应用服务(asp.net core web api)

    本文主要介绍通过Docker来部署通过.Net Core开发的微服务架构,部署的微服务主要包括统一网关(使用Ocelot开发).统一认证(IdentityServer4).应用服务(asp.net c ...

  3. 初试kafka消息队列中间件二(采用java代码收发消息)

    初试kafka消息队列中间件二(采用java代码收发消息) 上一篇 初试kafka消息队列中间件一 今天的案例主要是将采用命令行收发信息改成使用java代码实现,根据上一篇的接着写: 先启动Zooke ...

  4. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  5. 【Linux】【Services】【SaaS】Docker+kubernetes(8. 安装和配置Kubernetes)

    1. 概念 1.1. 比较主流的任务编排系统有mesos+marathon,swarm,openshift(红帽内部叫atom服务器)和最著名的kubernetes,居然说yarn也行,不过没见过谁用 ...

  6. Docker学习(1安装docker)

    1.在安装之前首先查看系统版本,ubuntu centos6 7 安装方式并不相同,所以首先确定系统版本 #cat /etc/issue 确定系统版本  2.开始安装Docker (1)Ubuntu下 ...

  7. Apache Commons Digester 一 (基础内容、核心API)

    前言 在许多需要处理XML格式数据的应用环境中, 如果能够以“事件驱动”的方式来处理XML文档,比如,当识别出特定的XML元素时,触发“创建对象”操作事件(或者触发调用对象的方法事件),这对于应用程序 ...

  8. 搭建rtmp直播流服务之2:使用java实现ffmpeg命令接口化调用(用java执行ffmpeg命令)

    欢迎大家积极开心的加入讨论群 群号:371249677 (点击这里进群) 一.环境搭建 1.安装ffmpeg 下载对应系统的ffmpeg安装包,个人采用windows平台进行开发,所以安装了windo ...

  9. Mysql事务开启方式(客户端+java手动+Spring Boot)

    一:概念 作为单个逻辑单元执行一系列操作,要么完全执行,要么完全不执行.举例 我们需要向数据库插入3条数据(我们希望这三条数据要么全部插入成功,要么全部失败), 比如第一条数据插入成功,插入第二条数据 ...

随机推荐

  1. PHPExcel导出数据时字段超过26列出错Invalid cell coordinate [1

    http://blog.csdn.net/dl425134845/article/details/46650961 以下是解决方案函数 /** *   方法名:    getExcel *   作用  ...

  2. Light OJ 1020

    简单推理题: #include<bits/stdc++.h> using namespace std; int main() { int T, n; string Name; cin &g ...

  3. 修改oracle数据库允许连接的数

    当前连接数:select count(*) from v$process;查询数据库允许的最大连接数: select value from v$parameter where name = 'proc ...

  4. mixins,generics(ApiView)

    #生成序列化对象class BookModelSerizter(serializers.ModelSerializer): class Meta: model=Book fields='__all__ ...

  5. Confluence 6 MySQL 3.x 字符集编码问题

    MySQL 3.x is 已知在大写和小写转换的时候有些问题(non-ASCII). 问题诊断 请按照 Troubleshooting Character Encodings 页面中的内容对问题进行诊 ...

  6. UserNotifications ios10 通知使用

    通知在ios10 中推荐使用 导入  import UserNotifications  头文件 if #available(iOS 10.0, *) { UNUserNotificationCent ...

  7. web前端识别文字转语音

    const msg = new SpeechSynthesisUtterance("hello world"); window.speechSynthesis.speak(msg) ...

  8. nginx之请求限制与连接限制

    模块limit_conn_module与limit_req_module limit_conn_module语法 limit_req_module语法 附: http长连接与短连接

  9. ionic3 title 不居中问题

    <ion-navbar> <ion-title style="text-align: center;">{{naveTitle}}<button io ...

  10. easyUI详解

    1.EasyUI 是前端框架,封装大量 css和封装大量 JS 2.使用前端框架时,给标签定义class 属性,就会有样式和脚本功能了 3.data-options 属性是定义 easyui 属性的, ...