Dockerfile的多级构建springboot项目及运行(maven)
本文章是基于maven的,如果想要gradle的方式,可以参考https://blog.csdn.net/Xavider/article/details/94548266
在Docker 17.05多阶段构建推出之后,我们只需要维护一个Dockerfile文件即可:
在项目的根目录下,创建如下的Dockerfile文件:
# First stage: complete build environment
#FROM maven:3.5.0-jdk-8-alpine AS builder
FROM maven:3.9.6-amazoncorretto-8-debian AS builder
WORKDIR /app
COPY . /app
# add pom.xml and source code
#ADD ./pom.xml pom.xml
#ADD ./src src/
# package jar
#COPY settings.xml /usr/share/maven/conf/
RUN mvn package -Dmaven.test.skip=true
# Second stage: minimal runtime environment
#FROM openjdk:8-jre
#FROM openjdk:8-jre-alpine
#FROM openjdk:8-jdk
#FROM openjdk:8-jdk-alpine
FROM amazoncorretto:8-alpine-jdk
## Change TimeZone
##-----------------------------------alpine------------------------------------------------------
## 安装 tzdata,复制里面的时区文件后删除 tzdata,保持精简
#RUN apk add --no-cache tzdata && \
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
# apk del tzdata && \
# echo "Asia/Shanghai" > /etc/timezone
##-----------------------------------ubuntu------------------------------------------------------
## 安装 tzdata,复制里面的时区文件后删除 tzdata,保持精简
#RUN apt update -y && \
# DEBIAN_FRONTEND="noninteractive" apt -y install tzdata
#RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
# dpkg-reconfigure -f noninteractive tzdata
##-----------------------------------centos------------------------------------------------------
## 安装 tzdata,复制里面的时区文件后删除 tzdata,保持精简
#RUN rm -f /etc/localtime \
# && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
# && echo "Asia/Shanghai" > /etc/timezone
# 设置 LABEL
LABEL maintainer="bydongxing@gmail.com"
# copy jar from the first stage
COPY --from=builder /app/target/springboot-redis-*.jar /app.jar
ENV JAVA_OPTS=""
ENV PARAMS=""
# health check, 每 15 秒检查一次,健康检查命令超过 3 秒没响应,并且重试 3 次都没响应就视为失败
HEALTHCHECK --interval=15s --timeout=3s --retries=3 \
CMD curl -fs http://localhost:8080/actuator/health || exit 1
# run jar
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar $PARAMS" ]
备注:
1、解决下载依赖慢的问题:可以通过将本地的settings.xml 复制到maven中或者可以在pom文件中指定仓库的地址也可以(二种方式二选一)
方式一:
settings.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/usr/share/maven/ref/repository</localRepository>
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<mirror>
<id>CN</id>
<name>OSChina Central</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
方式二:
pom.xml
<!-- nexus私服配置 -->
<repositories>
<repository>
<id>aliyun</id>
<name>Nexus Snapshot Repository</name>
<url>https://maven.aliyun.com/repository/public</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<!--snapshots默认是关闭的,需要开启 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<name>Nexus Snapshot Repository</name>
<url>https://maven.aliyun.com/repository/public</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
2、启动命令
jvm层级:docker run -p 宿主机端口号:镜像端口号 -e JAVA_OPTS="-Dspring.profiles.active=dev -Dspring.redis.host=host.docker.internal" +镜像名称
Pagram层级:docker run -p 宿主机端口号:镜像端口号 -e PARAMS="--spring.profiles.active=dev --spring.redis.host=host.docker.internal" +镜像名称
其中host.docker.internal是容器访问宿主机的地址,也可以通过docker0的ip地址访问
docker run -d --name=myweb --health-cmd="curl -fs http://localhost:8080/actuator/health || exit 1" --health-interval=5s --health-retries=12 --health-timeout=2s 镜像名称
--health-cmd string
:运行检查健康状况的命令--health-interval duration
:运行间隔时间(ms|s|m|h)(缺省为 0s)--health-retries int
:需要报告不健康的连续失败次数--health-start-period duration
:容器在开始健康重试倒计时之前初始化的起始周期(ms|s|m|h)(默认 0)--health-timeout duration
:允许一次检查运行的最大时间(ms|s|m|h)(默认为 0s)--no-healthcheck
:禁用任何容器指定的HEALTHCHECK,会使得 Dockerfile 构建出来的HEALTHCHECK功能失效
如果是构建maven多模块的项目
在项目父工程
的根目录
下,创建如下的Dockerfile文件:
附上Dockerfile文件:
# First stage: complete build environment
#FROM maven:3.5.0-jdk-8-alpine AS builder
FROM maven:3.9.6-amazoncorretto-8-debian AS builder
WORKDIR /app
ADD ./ /app
# package jar
#COPY settings.xml /usr/share/maven/conf/
RUN mvn install -Dmaven.test.skip=true
# Second stage: minimal runtime environment
#FROM openjdk:8-jdk
FROM amazoncorretto:8-alpine-jdk
## Change TimeZone
##-----------------------------------alpine------------------------------------------------------
## 安装 tzdata,复制里面的时区文件后删除 tzdata,保持精简
#RUN apk add --no-cache tzdata && \
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
# apk del tzdata && \
# echo "Asia/Shanghai" > /etc/timezone
##-----------------------------------ubuntu------------------------------------------------------
## 安装 tzdata,复制里面的时区文件后删除 tzdata,保持精简
#RUN apt update -y && \
# DEBIAN_FRONTEND="noninteractive" apt -y install tzdata
#RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
# dpkg-reconfigure -f noninteractive tzdata
##-----------------------------------centos------------------------------------------------------
## 安装 tzdata,复制里面的时区文件后删除 tzdata,保持精简
#RUN rm -f /etc/localtime \
# && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
# && echo "Asia/Shanghai" > /etc/timezone
# 设置 LABEL
LABEL maintainer="bydongxing@gmail.com"
# copy jar from the first stage
COPY --from=builder /app/springboot-redis/target/springboot-redis-*.jar /app.jar
ENV JAVA_OPTS=""
ENV PARAMS=""
# health check, 每 15 秒检查一次,健康检查命令超过 3 秒没响应,并且重试 3 次都没响应就视为失败
HEALTHCHECK --interval=15s --timeout=3s --retries=3 \
CMD curl -fs http://localhost:8080/actuator/health || exit 1
# run jar
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar $PARAMS" ]