1
13693261870
2024-12-08 864536db862bcde4ac0f281cff54cd4940380976
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package com.se.docker.utils;
 
import cn.hutool.core.util.ObjectUtil;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@SuppressWarnings("ALL")
public class DockerUtils {
    private static volatile DockerClient dockerClient;
 
    private static final Logger log = LoggerFactory.getLogger(DockerUtils.class);
 
    /**
     * vim /lib/systemd/system/docker.service
     * <p>
     * # 配置普通模式,-H参数指定docker应用程序监听方式,或者采用下面的安全连接方式2选1
     * ExecStart=/usr/bin/dockerd -H unix://var/run/docker.sock -H tcp://0.0.0.0:2375
     * <p>
     * # 配置安全连接,注意这里的/home/user/certs/ 根据自己情况替换为实际的的密钥存放路径
     * ExecStart=/usr/bin/dockerd -D --tlsverify=true --tlscert=/home/user/certs/server-cert.pem --tlskey=/home/user/certs/server-key.pem --tlscacert=/home/user/certs/ca.pem -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
     * <p>
     * # 重载配置,重启服务
     * systemctl daemon-reload
     * systemctl restart docker
     * <p>
     * # 查看端口监听
     * netstat -nlp |grep 2375
     * <p>
     * vi /etc/docker/daemon.json
     * # 配置insecure-registries
     * {
     * "insecure-registries": ["122.12.12.12:5000"],
     * "registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
     * }
     * # 重载配置,重启服务
     * systemctl daemon-reload
     * systemctl restart docker
     */
    private DockerUtils() {
    }
 
    private DockerUtils(String dockerHost, String dockerApiVersion, String dockerCertPath) {
        Objects.requireNonNull(dockerHost, "Docker 主机地址不能为空.");
        Objects.requireNonNull(dockerApiVersion, "Docker API 版本不能为空.");
 
        // 使用双重校验锁实现 Docker 客户端单例
        if (dockerClient == null) {
            synchronized (DockerUtils.class) {
                if (dockerClient == null) {
                    dockerClient = createDockerClient(dockerHost, dockerApiVersion, dockerCertPath);
                }
            }
        }
    }
 
    private DockerClient createDockerClient(String dockerHost, String dockerApiVersion, String dockerCertPath) {
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withApiVersion(dockerApiVersion)
                .withDockerHost(dockerHost)
                //如果开启安全连接,需要配置这行
                .withDockerTlsVerify(true).withDockerCertPath(dockerCertPath)
                .build();
 
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .sslConfig(config.getSSLConfig())
                .maxConnections(1000)
                .connectionTimeout(Duration.ofSeconds(60))
                .responseTimeout(Duration.ofMinutes(30))
                .build();
 
        return DockerClientImpl.getInstance(config, httpClient);
    }
 
    /**
     * 登录 Docker 镜像仓库
     *
     * @param authConfig 登录所需的认证信息
     * @throws RuntimeException 登录失败时抛出异常
     */
    public void login(AuthConfig authConfig) {
        try {
            Objects.requireNonNull(authConfig, "认证信息不能为空.");
            log.info("开始登录镜像仓库:{};username:{};password:{}", authConfig.getRegistryAddress(), authConfig.getUsername(), authConfig.getPassword());
 
            dockerClient.authCmd()
                    .withAuthConfig(authConfig)
                    .exec();
            log.info("镜像仓库登录成功:{}", authConfig.getRegistryAddress());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("镜像仓库登录失败:" + e.getMessage());
        }
    }
 
    /**
     * 从registry拉取Docker镜像
     *
     * @param tag 镜像名称
     * @return true表示拉取成功,false表示拉取失败
     */
    public void pullImage(AuthConfig authConfig, String tag) {
        Objects.requireNonNull(authConfig, "认证信息不能为空.");
        if (ObjectUtil.isEmpty(tag)) {
            throw new RuntimeException("镜像信息不能为空");
        }
        log.info("开始拉取 Docker 镜像: {}", tag);
        try {
            PullImageResultCallback exec = new PullImageResultCallback() {
                @Override
                public void onNext(PullResponseItem item) {
                    System.out.println(item.getStatus());
                }
            };
            PullImageCmd pullImageCmd = dockerClient.pullImageCmd(tag);
            pullImageCmd.withAuthConfig(authConfig)
                    .exec(exec)
                    .awaitCompletion(30, TimeUnit.MINUTES);
            exec.close();
            log.info("镜像拉取成功:{};", tag);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("镜像拉取失败:{}" + e.getMessage());
        }
    }
 
    /**
     * 保存Docker镜像
     *
     * @param imageId  镜像Id
     * @param filePath 保存文件名
     * @return true表示保存成功,false表示保存失败
     */
    public void saveImage(java.lang.String imageId, String filePath) {
 
        if (ObjectUtil.isEmpty(filePath)) {
            throw new RuntimeException("参数错误:保存路径不能为空");
        }
        log.info("开始保存镜像:{}", imageId);
        try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath));
             InputStream inputStream = dockerClient.saveImageCmd(imageId)
                     .exec()) {
            if (null == inputStream) {
                throw new RuntimeException("无法获取镜像");
            }
            byte[] bytesArray = new byte[4096];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(bytesArray)) != -1) {
                outputStream.write(bytesArray, 0, bytesRead);
            }
            log.info("镜像保存成功:{}", imageId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("保存镜像异常:" + e.getMessage());
        }
    }
 
    /**
     * 删除Docker镜像
     *
     * @param imageId 镜像标签
     * @return true表示删除成功,false表示删除失败
     */
    public boolean removeImage(String imageId) {
        Objects.requireNonNull(imageId, "镜像 ID 不能为空.");
        log.info("开始删除 Docker 镜像: {}", imageId);
        try {
            // 如果镜像当前有容器在运行,则不进行删除操作
            if (isRunContainer(imageId)) {
                log.warn("Docker 镜像正在使用中,无法删除: {}", imageId);
                return false;
            }
            RemoveImageCmd removeImageCmd = dockerClient.removeImageCmd(imageId);
            removeImageCmd.exec();
            log.info("Docker 镜像删除成功: {}", imageId);
            return true;
        } catch (Exception e) {
            log.error("Docker 镜像删除失败: {};{}", imageId, e.getMessage());
            return false;
        }
    }
 
    /**
     * 获取所有 Docker 容器的信息
     *
     * @return 所有 Docker 容器的信息列表
     */
    public List<Container> listContainers() {
        log.info("开始获取所有 Docker 容器信息.");
        try {
            ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
            return listContainersCmd.exec();
        } catch (Exception e) {
            log.error("获取所有 Docker 容器信息失败: {}", e.getMessage());
            throw new RuntimeException("获取所有 Docker 容器信息失败: " + e.getMessage());
        }
    }
 
    /**
     * 是否有在运行的容器
     *
     * @param imageId
     * @return
     */
    public boolean isRunContainer(String imageId) {
        Objects.requireNonNull(imageId, "镜像 ID 不能为空.");
        log.info("检查 Docker 镜像是否正在使用中: {}", imageId);
        try {
            List<Container> containers = listContainers();
            List<String> containerNames = containers.stream()
                    .map(Container::getImageId)
                    .collect(Collectors.toList());
            log.info("列出所有容器成功,数量:{}", containerNames.size());
            if (ObjectUtil.isNotEmpty(containerNames) && containerNames.contains(imageId)) {
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("检查 Docker 镜像是否正在使用中失败: {}", e.getMessage());
            throw new RuntimeException("检查 Docker 镜像是否正在使用中失败: " + e.getMessage());
        }
 
    }
 
    /**
     * 推送镜像
     *
     * @param authConfig
     * @param tag
     */
    public static void pushImage(AuthConfig authConfig, String tag) {
        Objects.requireNonNull(authConfig, "认证信息不能为空.");
        Objects.requireNonNull(tag, "镜像信息不能为空.");
        log.info("开始推送 Docker 镜像: {}", tag);
        try {
            PushImageCmd pushImageCmd = dockerClient.pushImageCmd(tag);
            pushImageCmd.withAuthConfig(authConfig)
                    .start()
                    .awaitCompletion(30, TimeUnit.SECONDS);
            log.info("镜像push成功:{}", tag);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("镜像push失败:{}" + e.getMessage());
        }
    }
 
    /**
     * 获取镜像Id
     *
     * @param tag
     * @return
     */
    public String getImageId(String tag) {
        try {
            InspectImageCmd inspectImageCmd = dockerClient.inspectImageCmd(tag);
            InspectImageResponse image = inspectImageCmd.exec();
            if (null == image) {
                throw new RuntimeException("未获取到镜像信息:");
            }
            return image.getId();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("无法获取镜像信息:" + e.getMessage());
        }
 
    }
 
    // 使用 Builder 模式构建 DockerUtil 对象
    public static class Builder {
 
        private String dockerHost;
        private String dockerApiVersion;
        private String dockerCertPath;
 
        public Builder withDockerHost(String dockerHost) {
            this.dockerHost = dockerHost;
            return this;
        }
 
        public Builder withDockerApiVersion(String dockerApiVersion) {
            this.dockerApiVersion = dockerApiVersion;
            return this;
        }
 
        public Builder withDockerCertPath(String dockerCertPath) {
            this.dockerCertPath = dockerCertPath;
            return this;
        }
 
        public DockerUtils build() {
            return new DockerUtils(dockerHost, dockerApiVersion, dockerCertPath);
        }
    }
 
    public static void main2(String[] args) throws InterruptedException {
        String tag = "10.20.152.16:18080/test/redis:t1";
        AuthConfig authConfig = new AuthConfig()
                .withRegistryAddress("10.20.152.16:18080")
                .withUsername("admin")
                .withPassword("admin123");
 
        DockerUtils dockerUtil = new DockerUtils.Builder()
                //服务器ip
                .withDockerHost("tcp://10.50.80.165:2375")
                //API版本 可通过在服务器 docker version 命令查看
                .withDockerApiVersion("1.41")
                //安全连接密钥文件存放路径
                .withDockerCertPath("/home/user/certs/")
                .build();
        //登录
        dockerUtil.login(authConfig);
 
        /* DockerUtil.pushImage(authConfig , tag); */
        //拉取镜像
        dockerUtil.pullImage(authConfig, tag);
        String imageId1 = dockerUtil.getImageId(tag);
 
        //保存镜像
        dockerUtil.saveImage(imageId1, "E:\\pdfTest\\docker\\redis.tar");
        //删除镜像
        dockerUtil.removeImage(imageId1);
    }
}