关联文章链接:
- Java引入docker-api(一),以代码形式对docker执行常用的操作
- Java引入docker-api(二),进阶操作之redis创建
- Java引入docker-api(三),进阶操作之xxl-job创建
环境
开发系统:mac
步骤
1.准备连接配置,多种方式配置相关信息,可以使用properties文件的形式:
DOCKER_TLS_VERIFY=1 //使用TLS连接
DOCKER_CERT_PATH=/Users/tomcat/.docker // docker所安装目录
DOCKER_CONFIG=/home/user/.docker
api.version=1.41 //使用docker命令可以查看版本
registry.url=https://index.docker.io/v1/
registry.username=dockeruser //注册用户名
registry.password=password //注册密码
registry.email=dockeruser@github.com //注册邮箱
我这边是以测试方便出发,直接写到代码里的:
/**
* 创建连接
* @param
* @return
*/
public DockerClient geiClient() {
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerTlsVerify(true)
.withDockerCertPath("/Users/tomcat/.docker")
.withDockerHost("tcp://localhost:2375")
.withApiVersion("1.41")
.withRegistryUrl("https://index.docker.io/v1/")
.withRegistryUsername("你的用户名")
.withRegistryPassword("你的密码")
.withRegistryEmail("你的邮箱@qq.com")
.build();
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
.dockerHost(config.getDockerHost())
.sslConfig(config.getSSLConfig())
.maxConnections(100)
.connectionTimeout(Duration.ofSeconds(30))
.responseTimeout(Duration.ofSeconds(45))
.build();
// 连接
DockerClient dockerClient = DockerClientImpl.getInstance(config, httpClient);
Info info = dockerClient.infoCmd().exec();
JSON json = JSONUtil.parse(info);
System.out.println("docker的环境信息如下:");
System.out.println(json.toString());
return dockerClient;
}
如果创建连接失败,提示docker拒绝连接,是因为docker服务没有打开远程服务监听2379端口,所以需要对docker服务添加参数,监听2379端口。
mac环境需要进行如下操作:
如果没有安装brew先执行安装home brew,已经替换为国内连接,brew的使用请自行学习,已安装跳过该步骤
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
通过brew安装socat
brew install socat
开启远程 TCP 2375端口访问Docker
socat TCP-LISTEN:2375,reuseaddr,fork UNIX-CONNECT:/var/run/docker.sock &
配置完成,就可以通过 tcp://ip地址:2375 的方式访问Mac上运行的Docker了。
地址栏输入http://localhost:2375/info,出现数据,表示可以远程访问了。
2.测试操作方法
/**
* 创建容器
* @param client,imageName
* @return
*/
public CreateContainerResponse createContainers(DockerClient client){
//镜像的名字需要:名字 + 版本号
CreateContainerResponse container = client.createContainerCmd("redis:6.2")
//容器名称
.withName("testredis")
//端口绑定 宿主机端口 -> 容器端口 不等长参数,可绑定多个暴露端口
.withHostConfig(HostConfig.newHostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(6378), ExposedPort.tcp(6379))))
.exec();
return container;
}
@Test
public void CreateContainer() {
DockerClient client = geiClient();
// 创建容器
CreateContainerResponse containers = createContainers(client, "redis:6.2");
// 开始容器
client.startContainerCmd(containers.getId()).exec();
}
/**
* 启动容器
* @param
* @param
*/
@Test
public void startContainer(){
try {
DockerClient client = geiClient();
client.startContainerCmd("你的容器ID").exec();
} catch (NotFoundException e) {
System.out.println("未找到id为: 的容器");
}
}
/**
* 关闭容器
* @param
* @param
*/
@Test
public void stopContainer(){
DockerClient client = geiClient();
client.stopContainerCmd("你的容器id").exec();
}
/**
* 删除容器
* @param
* @param
*/
@Test
public void removeContainer(){
DockerClient client = geiClient();
client.removeContainerCmd("你的容器id").exec();
}
/**
* 删除镜像
* @param
* @return
*/
@Test
public void removeImage(){
DockerClient client = geiClient();
client.removeImageCmd("镜像id").exec();
}
这里以创建容器来测试,我拉取的有redis6.2版本的景象,下面执行下创建容器方法:
运行成功,dd078d98b8bb5cc49467d8c8ab9795b05d6be304b1f3cd25e2188bd638d1029f为容器ID,下面去desktop docker里去看
由于我运行的有一个redis容器,端口我这里指定的是6378,下面使用redis连接工具测试下:
连接成功