Docker registry本地镜像仓库搭建 + SpringBoot项目构建后推送至镜像仓库

# 拉取registry镜像
docker pull registry
# 启动并挂载和映射端口
docker run --name registry -p 5000:5000 -v /var/lib/registry:/var/lib/registry --restart=always --privileged=true -d registry
# 修改客户机的docker镜像地址为 "insecure-registries": "对应私服地址ip:5000"
vim /etc/docker/daemon.json
# { "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"], "insecure-registries":["私服ip:5000"] } # 重新读取配置生效 并 重启docker systemctl daemon
-reload systemctl restart docker # 进入有Dockerfile的路径,并构建镜像 docker build --build-arg JAR_NAME=xxxx-0.0.1-SNAPSHOT.jar -t app . # 私服镜像管理命令 # 将镜像的tag(docker images 命令查看image_id)推送至私服 docker tag $image_id 私服ip:5000/xxxx # 推送镜像 docker push 私服ip:5000/xxxx # 查询私服仓库,返回镜像仓库信息,数组的格式 curl -X GET http://127.0.0.1:5000/v2/_catalog # 删除私服中镜像 docker rmi 私服ip:5000/xxxx xxxx
curl http://127.0.0.1:5000/v2/maven/tags/list
curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://127.0.0.1:5000/v2/tico/docker/manifests/local | grep Docker-Content-Digest | sed -e 's/Docker-Content-Digest: //g'
curl -I -X DELETE http://127.0.0.1:5000/v2/tico/docker/manifests/sha256:5cc709f79b1d3ee4635566260f5ae6b0237da04a35ef3f222c2110331cf318e4
docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml
docker exec registry rm -rf /var/lib/registry/docker/registry/v2/repositories/<镜像名>

 常用源:

{ 
    "registry-mirrors": [
        "https://kfwkfulq.mirror.aliyuncs.com",
        "https://2lqq34jg.mirror.aliyuncs.com",
        "https://pee6w651.mirror.aliyuncs.com",
        "https://registry.docker-cn.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn"
    ], 
    "insecure-registries":["私服ip:5000"] 
}

 SpringBoot 项目构建完成后推送至registry

先在pom配置插件

<properties>
<java.version>1.8</java.version>
<!--maven-docker插件版本号 -->
<dockerfile.plugin.version>1.4.10</dockerfile.plugin.version>
<!--oms docker私有镜像仓库地址-->
<docker.image.prefix>10.213.30.206:5000</docker.image.prefix>
<!--镜像Tag,默认是latest-->
<tag.version></tag.version>
</properties>
<build>
    <plugins>
        <!--使用Maven插件直接将应用打包为一个Docker镜像-->
        <!--跳过dockerfile打包为镜像 mvn install -Ddockerfile.skip=true-->
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>${dockerfile.plugin.version}</version>
            <configuration>
                <!-- 镜像仓库地址docker.image.prefix,一般是docker.image.prefix = 'ip:5000'(docker registry的端口号),项目名project.artifactId,比如这里就是 project.artifactId= 'manage' -->
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
                <!-- 标签版本号,一般在mvn命令中:mvn clean package -Dtag.version=$tag -->
                <tag>${tag.version}</tag>
                <buildArgs>
                    <!-- 构建Dockerfile时,作为参数传入,这里让docker构建时找得到jar包 -->
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
            <!--镜像构建完毕之后自动推送到仓库-->
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>build</goal>
                        <goal>push</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    <plugins>
</build>

打包命令

mvn clean package -Dmaven.test.skip=true -Dtag.version=xxx.latest

mvn dockerfile:push -Dtag.version=xxx.latest

 

posted on 2022-04-20 21:18  lyjlyjlyj  阅读(303)  评论(0编辑  收藏  举报

导航