目录

Life in Flow

知不知,尚矣;不知知,病矣。
不知不知,殆矣。

X

微服务本地镜像打包上传阿里云私有仓库

配置 JDK11

参考

微服务 Docker 镜像打包

父项目(xdclass-cloud)添加 SpringBoot 版本依赖

1 <properties>
2        <java.version>11</java.version>
3        <maven.compiler.source>11</maven.compiler.source>
4        <maven.compiler.target>11</maven.compiler.target>
5        <spring.boot.version>2.3.3.RELEASE</spring.boot.version>
6  </properties>

每个子模块项目添加依赖(xdclass-api-gateway)

 1//配置文件增加
 2    <properties>
 3        <docker.image.prefix>xdclass-cloud</docker.image.prefix>
 4    </properties>
 5
 6
 7<build>
 8        <finalName>alibaba-cloud-gateway</finalName>
 9        <plugins>
10            <plugin>
11                <groupId>org.springframework.boot</groupId>
12                <artifactId>spring-boot-maven-plugin</artifactId>
13		<executions>
14                    <execution>
15                        <goals>
16                            <goal>repackage</goal>
17                        </goals>
18                    </execution>
19                </executions>
20                <configuration>
21                    <fork>true</fork>
22                    <addResources>true</addResources>
23                </configuration>
24            </plugin>
25
26            <plugin>
27                <groupId>com.spotify</groupId>
28                <artifactId>dockerfile-maven-plugin</artifactId>
29                <version>1.4.10</version>
30                <configuration>
31                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
32                    <buildArgs>
33                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
34                    </buildArgs>
35                </configuration>
36            </plugin>
37        </plugins>
38    </build>
39
  • Spotify 的 docker-maven-plugin 插件是用 maven 插件方式构建 docker 镜像的。
1${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}

在每个子项目(xdclass-api-gateway)的根目录下创建文件 Dockerfile

1FROM  adoptopenjdk/openjdk11:ubi
2VOLUME /tmp
3ARG JAR_FILE
4COPY ${JAR_FILE} app.jar
5ENTRYPOINT ["java","-jar","/app.jar"]

构建镜像( 去到子模块 pom 文件下)执行命令

1mvn install -Dmaven.test.skip=true dockerfile:build

等待执行成功

1C:\Users\Ryzen-7-3800X>docker images
2REPOSITORY                          TAG       IMAGE ID       CREATED         SIZE
3xdclass-cloud/xdclass-api-gateway   latest    a8722afafb45   2 seconds ago   629MB
4adoptopenjdk/openjdk11              ubi       014786455e14   2 weeks ago     587MB

本地镜像上传阿里云私有仓库

 11. 登录阿里云Docker Registry
 2sudo docker login --username=soulboy1990116 registry.cn-shanghai.aliyuncs.com
 3
 4
 52. 将镜像推送到Registry
 6本地镜像打标签
 7$ sudo docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:[镜像版本号]
 8$ sudo docker tag a8722afafb45 registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:v1.0
 9
10将本地镜像推送至阿里云私有仓库
11$ sudo docker push registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:[镜像版本号]
12$ sudo docker push registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:v1.0
13

从阿里云私有仓库拉去镜像到阿里云 ECS 部署服务

11. 从Registry中拉取镜像
2sudo docker pull registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:v1.0
3
42.网关容器启动
5docker run  --name xdclass-gateway -d -p 8888:8888 [镜像id]

作者:Soulboy