From 864536db862bcde4ac0f281cff54cd4940380976 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期日, 08 十二月 2024 20:05:35 +0800 Subject: [PATCH] 1 --- se-modules/se-docker/pom.xml | 19 ++ se-modules/se-docker/src/main/java/com/se/docker/utils/DockerUtils.java | 343 +++++++++++++++++++++++++++++++++++++++++++++++++ 说明.txt | 6 se-modules/se-docker/src/main/java/com/se/docker/service/SysDockerService.java | 39 +++++ 4 files changed, 407 insertions(+), 0 deletions(-) diff --git a/se-modules/se-docker/pom.xml b/se-modules/se-docker/pom.xml index 2f110d4..4c72ddb 100644 --- a/se-modules/se-docker/pom.xml +++ b/se-modules/se-docker/pom.xml @@ -51,6 +51,25 @@ <groupId>com.se</groupId> <artifactId>se-common-swagger</artifactId> </dependency> + + <!-- docker --> + <dependency> + <groupId>com.github.docker-java</groupId> + <artifactId>docker-java</artifactId> + <version>3.4.0</version> + </dependency> + <dependency> + <groupId>com.github.docker-java</groupId> + <artifactId>docker-java-transport-httpclient5</artifactId> + <version>3.4.0</version> + </dependency> + + <!-- hutool --> + <dependency> + <groupId>cn.hutool</groupId> + <artifactId>hutool-all</artifactId> + <version>5.8.34</version> + </dependency> </dependencies> <build> diff --git a/se-modules/se-docker/src/main/java/com/se/docker/service/SysDockerService.java b/se-modules/se-docker/src/main/java/com/se/docker/service/SysDockerService.java index 4de57df..b5bfd93 100644 --- a/se-modules/se-docker/src/main/java/com/se/docker/service/SysDockerService.java +++ b/se-modules/se-docker/src/main/java/com/se/docker/service/SysDockerService.java @@ -1,5 +1,11 @@ package com.se.docker.service; +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.CreateContainerResponse; +import com.github.dockerjava.api.command.InspectContainerResponse; +import com.github.dockerjava.core.DockerClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -9,7 +15,40 @@ //@Value("${docker.prefix}") public String localFilePrefix; + private static final Logger log = LoggerFactory.getLogger(SysDockerService.class); + public String test() throws Exception { return "docker: " + System.currentTimeMillis(); } + + /** + * java DockerClient鎿嶄綔 + * https://blog.csdn.net/weixin_45198228/article/details/130060333 + */ + public void t() { + try { + // 鍒涘缓Docker瀹㈡埛绔疄渚� + DockerClient dockerClient = DockerClientBuilder.getInstance("unix:///var/run/docker.sock").build(); + + // 鍒涘缓骞跺惎鍔ㄤ竴涓柊鐨勫鍣� + CreateContainerResponse container = dockerClient.createContainerCmd("busybox") + .withCmd("echo", "Hello Docker!") + .exec(); + + // 鍚姩瀹瑰櫒 + dockerClient.startContainerCmd(container.getId()).exec(); + + // 妫�鏌ュ鍣ㄧ姸鎬� + InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); + System.out.println(inspectContainerResponse.toString()); + + // 鍋滄骞剁Щ闄ゅ鍣� + dockerClient.removeContainerCmd(container.getId()).withRemoveVolumes(true).exec(); + + // 鍏抽棴Docker瀹㈡埛绔� + dockerClient.close(); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } } diff --git a/se-modules/se-docker/src/main/java/com/se/docker/utils/DockerUtils.java b/se-modules/se-docker/src/main/java/com/se/docker/utils/DockerUtils.java new file mode 100644 index 0000000..9530b38 --- /dev/null +++ b/se-modules/se-docker/src/main/java/com/se/docker/utils/DockerUtils.java @@ -0,0 +1,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()); + } + } + + /** + * 浠巖egistry鎷夊彇Docker闀滃儚 + * + * @param tag 闀滃儚鍚嶇О + * @return true琛ㄧず鎷夊彇鎴愬姛锛宖alse琛ㄧず鎷夊彇澶辫触 + */ + 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琛ㄧず淇濆瓨鎴愬姛锛宖alse琛ㄧず淇濆瓨澶辫触 + */ + 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琛ㄧず鍒犻櫎鎴愬姛锛宖alse琛ㄧず鍒犻櫎澶辫触 + */ + public boolean removeImage(String imageId) { + Objects.requireNonNull(imageId, "闀滃儚 ID 涓嶈兘涓虹┖."); + log.info("寮�濮嬪垹闄� Docker 闀滃儚: {}", imageId); + try { + // 濡傛灉闀滃儚褰撳墠鏈夊鍣ㄥ湪杩愯锛屽垯涓嶈繘琛屽垹闄ゆ搷浣� + if (isRunContainer(imageId)) { + log.warn("Docker 闀滃儚姝e湪浣跨敤涓紝鏃犳硶鍒犻櫎: {}", 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 闀滃儚鏄惁姝e湪浣跨敤涓�: {}", 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湪浣跨敤涓け璐�: {}", e.getMessage()); + throw new RuntimeException("妫�鏌� Docker 闀滃儚鏄惁姝e湪浣跨敤涓け璐�: " + 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() + //鏈嶅姟鍣╥p + .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); + } +} diff --git "a/\350\257\264\346\230\216.txt" "b/\350\257\264\346\230\216.txt" index 8c5faf3..5449bef 100644 --- "a/\350\257\264\346\230\216.txt" +++ "b/\350\257\264\346\230\216.txt" @@ -55,6 +55,12 @@ uri: lb://se-job 鈫� uri: lb://se-system + # docker鏈嶅姟 + - id: se-docker + uri: lb://se-docker + predicates: + - Path=/docker/** + # 涓嶆牎楠岀櫧鍚嶅崟 ignore: whites: -- Gitblit v1.9.3