微服务本地镜像打包上传阿里云私有仓库
配置JDK11
微服务Docker镜像打包
父项目(xdclass-cloud)添加springboot版本依赖
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring.boot.version>2.3.3.RELEASE</spring.boot.version>
</properties>
每个子模块项目添加依赖(xdclass-api-gateway)
//配置文件增加
<properties>
<docker.image.prefix>xdclass-cloud</docker.image.prefix>
</properties>
<build>
<finalName>alibaba-cloud-gateway</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
- Spotify 的 docker-maven-plugin 插件是用maven插件方式构建docker镜像的。
${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
在每个子项目(xdclass-api-gateway)的根目录下创建文件Dockerfile
FROM adoptopenjdk/openjdk11:ubi
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
构建镜像( 去到子模块pom文件下)执行命令
mvn install -Dmaven.test.skip=true dockerfile:build
等待执行成功
C:\Users\Ryzen-7-3800X>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xdclass-cloud/xdclass-api-gateway latest a8722afafb45 2 seconds ago 629MB
adoptopenjdk/openjdk11 ubi 014786455e14 2 weeks ago 587MB
本地镜像上传阿里云私有仓库
1. 登录阿里云Docker Registry
sudo docker login --username=soulboy1990116 registry.cn-shanghai.aliyuncs.com
2. 将镜像推送到Registry
本地镜像打标签
$ sudo docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:[镜像版本号]
$ sudo docker tag a8722afafb45 registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:v1.0
将本地镜像推送至阿里云私有仓库
$ sudo docker push registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:[镜像版本号]
$ sudo docker push registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:v1.0
从阿里云私有仓库拉去镜像到阿里云ECS部署服务
1. 从Registry中拉取镜像
sudo docker pull registry.cn-shanghai.aliyuncs.com/abc1024/cloud-gateway:v1.0
2.网关容器启动
docker run --name xdclass-gateway -d -p 8888:8888 [镜像id]