java实现docker镜像上传到harbor仓库

1. 前 言

在推送镜像文件到镜像仓库时,我们往往是在镜像文件所在的那个主机上,以 root 用户的权限,执行 docker push 命令,完成镜像推送的工作。

但有这么一种令人匪夷所思的人,他直接打一个离线的镜像包(docker save tomcat:latest > tomcat-892148dsadg-v1.tar)出来,比如 tomcat-892148dsadg-v1.tar ,然后通过邮件或者其它通讯工具发给你,让你推送到镜像仓库。

这时,你怎么搞?直接通过 shell 指令操作,可行吗?当然可以,只要在有 docker 环境的服务器上,以 root 权限的用户执行下面 5 步就能完成:

  1. rz 命令,上传 tar 包到服务器上
  2. docker load -i xxxx.tar ,加载镜像文件
  3. docker login harbor仓库地址 -u 用户名 -p 密码 ,登录 harbor 仓库
  4. docker tag xxxx.tar harbor仓库地址/命名空间/xxxxx.tar,重新打标签
  5. docker push harbor仓库地址/命名空间/xxxxx.tar,推送镜像到 harbor 仓库

上面的方式,看着是很清晰地解决了问题,但一般来说,开发人员很少能直接操控服务器,更别提拿到 root 用户,在一些权限管控严格的企业,这种方式想想就好了,或者让运维来。

那还有其它方式吗?当然!那就直接用 java 操作 docker 命令实现离线的 tar包 推送至 harbor 仓库吧!

2. 编写工具类

2.1 引入依赖包

在 pom.xml 文件中引入下面的两个依赖

<dependency>
    <groupId>com.github.docker-javagroupId>
    <artifactId>docker-javaartifactId>
    <version>3.2.13version>
dependency>
<dependency>
    <groupId>com.github.docker-javagroupId>
    <artifactId>docker-java-transport-httpclient5artifactId>
    <version>3.2.13version>
dependency>
2.2 使用当前服务器的docker环境推送镜像

application.properties 配置文件

harbor.username=harbor
harbor.password=Harbor123!

工具类

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.List;

/**
 * @author yuhuofei
 * @version 1.0
 * @description 镜像上传工具类
 * @date 2022/6/13 21:19
 */
@Service
@Slf4j
public class DockerUtils {

    @Value("${harbor.username}")
    private String harborUserName;

    @Value("${harbor.password}")
    private String harborPassWord;

    /**
     * 获取dockerClient对象
     */
    public DockerClient getDockerClient() {
    
        //创建DefaultDockerClientConfig(当前服务器的默认配置)
        DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().build();

        //创建DockerHttpClient
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .maxConnections(100)
                .connectionTimeout(Duration.ofSeconds(30))
                .responseTimeout(Duration.ofSeconds(45))
                .build();

        //创建DockerClient
        return DockerClientImpl.getInstance(config, httpClient);

    }

    /**
     * @param file      镜像文件
     * @param imageName 镜像名称,格式最好是:镜像名-镜像id-版本号.tar
     * @param harborLoginUrl harbor仓库地址
     * @param imageId 镜像id
     * @param tag 版本号
     */
    public void uploadImage(File file, String imageName, String harborLoginUrl, String imageId, String tag) {
        log.info("上传镜像文件时,传递的参数:{},{},{},{},{}", file.getAbsolutePath(), imageName, harborLoginUrl, imageId, tag);
        try (InputStream inputStream = new FileInputStream(file)) {
            DockerClient dockerClient = getDockerClient();
            //Harbor登录信息
            AuthConfig autoConfig = new AuthConfig().withRegistryAddress(harborLoginUrl).withUsername(harborUserName).withPassword(harborPassWord);

            //加载镜像
            log.info("====开始加载镜像====,{}", inputStream);
            dockerClient.loadImageCmd(inputStream).exec();
            
            //获取加载镜像的名称,如果根据镜像名称去匹配,有可能重复,所以用镜像id去匹配
            String uploadImageName = "";
            List<Image> list = dockerClient.listImagesCmd().exec();
            for (Image image : list) {
                if (image.getId().contains(imageId)) {
                    uploadImageName = image.getRepoTags()[0];
                }
            }

            //给镜像打上tag
            log.info("原始镜像名:{},要修改的镜像名:{}", uploadImageName, imageName);
            dockerClient.tagImageCmd(uploadImageName, imageName, tag).exec();

            //推送镜像至镜像仓库
            dockerClient.pushImageCmd(imageName).withAuthConfig(autoConfig).start().awaitCompletion();
            //push成功后,删除本地加载的镜像
            dockerClient.removeImageCmd(imageName).exec();
            dockerClient.removeImageCmd(uploadImageName).exec();
        } catch (InterruptedException exception) {
            throw new BaseException("推送镜像到镜像仓库异常:{}", exception);
        } catch (IOException e) {
            throw new BaseException("镜像文件流处理异常");
        }
    }
}

2.2 使用指定服务器的docker环境推送镜像

application.properties 文件的配置

docker.server.url=tcp://xxxxxx:2376
docker.cert.path=/home/user/.docker/certs
api.version=1.41
registry.url=https://index.docker.io/v1/
registry.username=docker
registry.password=Docker123!
registry.email=xxxxxxx@qq.com

harbor.username=harbor
harbor.password=Harbor123!

工具类

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.List;

/**
 * @author yuhuofei
 * @version 1.0
 * @description 镜像上传工具类
 * @date 2022/6/13 21:19
 */
@Service
@Slf4j
public class DockerUtils {

    @Value("${docker.server.url}")
    private String dockerServerUrl;
    
    @Value("${docker.cert.path}")
    private String dcokerCertPath;

    @Value("${registry.user}")
    private String registryUser;
    
    @Value("${registry.pass}")
    private String registryPass;
    
    @Value("${registry.mail}")
    private String registryMail;
    
    @Value("${registry.url}")
    private String registryUrl;

	//harbor仓库登录用户名
    @Value("${harbor.username}")
    private String harborUserName;
	//harbor仓库登录密码
    @Value("${harbor.password}")
    private String harborPassWord;

    /**
     * 获取dockerClient对象
     */
    public DockerClient getDockerClient() {
    
        //创建DefaultDockerClientConfig(指定docker服务器的配置)
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
            .withDockerHost(dockerServerUrl)
    		.withDockerTlsVerify(true)
    		.withDockerCertPath(dcokerCertPath)
    		.withRegistryUsername(registryUser)
    		.withRegistryPassword(registryPass)
    		.withRegistryEmail(registryMail)
    		.withRegistryUrl(registryUrl)
    		.build();

        //创建DockerHttpClient
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
                .dockerHost(config.getDockerHost())
                .maxConnections(100)
                .connectionTimeout(Duration.ofSeconds(30))
                .responseTimeout(Duration.ofSeconds(45))
                .build();

        //创建DockerClient
        return DockerClientImpl.getInstance(config, httpClient);

    }

    /**
     * @param file      镜像文件
     * @param imageName 镜像名称,格式最好是:镜像名-镜像id-版本号.tar
     * @param harborLoginUrl harbor仓库地址
     * @param imageId 镜像id
     * @param tag 版本号
     */
    public void uploadImage(File file, String imageName, String harborLoginUrl, String imageId, String tag) {
        log.info("上传镜像文件时,传递的参数:{},{},{},{},{}", file.getAbsolutePath(), imageName, harborLoginUrl, imageId, tag);
        try (InputStream inputStream = new FileInputStream(file)) {
            DockerClient dockerClient = getDockerClient();
            //Harbor登录信息
            AuthConfig autoConfig = new AuthConfig().withRegistryAddress(harborLoginUrl).withUsername(harborUserName).withPassword(harborPassWord);

            //加载镜像
            log.info("====开始加载镜像====,{}", inputStream);
            dockerClient.loadImageCmd(inputStream).exec();
            
            //获取加载镜像的名称,如果根据镜像名称去匹配,有可能重复,所以用镜像id去匹配
            String uploadImageName = "";
            List<Image> list = dockerClient.listImagesCmd().exec();
            for (Image image : list) {
                if (image.getId().contains(imageId)) {
                    uploadImageName = image.getRepoTags()[0];
                }
            }

            //给镜像打上tag
            log.info("原始镜像名:{},要修改的镜像名:{}", uploadImageName, imageName);
            dockerClient.tagImageCmd(uploadImageName, imageName, tag).exec();

            //推送镜像至镜像仓库
            dockerClient.pushImageCmd(imageName).withAuthConfig(autoConfig).start().awaitCompletion();
            //push成功后,删除本地加载的镜像
            dockerClient.removeImageCmd(imageName).exec();
            dockerClient.removeImageCmd(uploadImageName).exec();
        } catch (InterruptedException exception) {
            throw new Exception("推送镜像到镜像仓库异常:{}", exception);
        } catch (IOException e) {
            throw new Exception("镜像文件流处理异常");
        }
    }
}

3. 说 明

  1. 上述的 java 服务,不能部署在 docker 环境的容器内,需要部署在 docker 主机上,不然在容器中运行的 java 服务,是没法操作到主机上的 docker 命令的
  2. 上传 tar 包文件的前端内容省略
  3. 这里上传的 tar 包命名格式最好是:镜像名-镜像id-版本号.tar
  4. 打出来的 tar 包,大小最好不要超过 20 G

你可能感兴趣的:(Java,Docker,docker,java,容器)