springboot项目生成镜像、推送镜像
2024-11-19
1. 配置DOCKER_HOST 环境变量
tcp://devxmate.me:2375
2. pom.xml文件添加如下内容:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.3.5</version>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<configuration>
<repository>devxmate/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<jarFile>${project.build.finalName}.jar</jarFile>
</buildArgs>
</configuration>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
3. 新建Dockerfile
FROM amazoncorretto:17-alpine as corretto-deps
COPY ./target/app.jar /app/
RUN unzip /app/app.jar -d temp && \
jdeps \
--print-module-deps \
--ignore-missing-deps \
--recursive \
--multi-release 17 \
--class-path="./temp/BOOT-INF/lib/*" \
--module-path="./temp/BOOT-INF/lib/*" \
/app/app.jar > /modules.txt
FROM amazoncorretto:17-alpine as corretto-jdk
COPY --from=corretto-deps /modules.txt /modules.txt
RUN apk add --no-cache binutils && \
jlink \
--verbose \
--add-modules "$(cat /modules.txt),jdk.crypto.ec,jdk.crypto.cryptoki" \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=2 \
--output /jre17
FROM alpine:latest
ARG JDK_HOME_DIR=/opt/java/jdk17
COPY --from=corretto-jdk /jre17 ${JDK_HOME_DIR}
ENV JAVA_HOME=${JDK_HOME_DIR}
ENV PATH=$JAVA_HOME/bin:$PATH
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENV JAVA_OPTS="-server \
-Xms1024m \
-Xmx1024m \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/opt/apps/logs \
-Dfile.encoding=utf-8 \
-Duser.timezone=GMT+08 \
-Dspring.profiles.active=test"
RUN addgroup --system spring && adduser --system spring --ingroup spring
RUN mkdir -p /opt/apps && mkdir -p /opt/apps/logs && mkdir -p /opt/apps/config
COPY target/app.jar /opt/apps/app.jar
COPY run.sh /opt/apps/run.sh
COPY src/main/resources/application-test.properties /opt/apps/config/application-test.properties
RUN chmod +x /opt/apps/run.sh && \
chown -R spring:spring /opt/apps
WORKDIR /opt/apps
USER spring
EXPOSE 8080
RUN chmod u+x /opt/apps/run.sh
ENV LANG C.UTF-8
CMD ["sh", "/opt/apps/run.sh", "start"]
4. 构建镜像并推送
mvn clean package
5. 检查是否推送成功
在DOCK_HOST所指向的服务器中运行以下命令查看是否有你刚才创建的镜像
docker images