项目结构如下
Dockerfile
FROM centos:7
MAINTAINER Tony Chen <chenming7@ruijie.com.cn>
ENV JAVA_HOME="/opt/jdk_8"
ENV PATH="${JAVA_HOME}/bin:${PATH}"
ENV MAVEN_HOME="/opt/maven_3.9.2"
ENV PATH="${MAVEN_HOME}/bin:${PATH}"
# 添加jdk和 maven
COPY jdk-8u192-linux-x64.tar.gz /opt/jdk-8u192-linux-x64.tar.gz
COPY maven_3.9.2.tar.gz /opt/maven_3.9.2_11.tar.gz
RUN cd /opt && tar zxvf /opt/jdk-8u192-linux-x64.tar.gz
RUN mv /opt/jdk1.8.0_192 /opt/jdk_8
RUN cd /opt && tar zxvf /opt/maven_3.9.2_11.tar.gz
build.sh
maven_3.9.2.tar.gz
和jdk-8u192-linux-x64.tar.gz
是自己准备好的maven压缩包(setting.xml我已经修改好了国内源和本地仓库),以及JDK8
bash build.sh
构建镜像,镜像内就会有JAVA和MAVEN环境
# 清除构建缓存
docker builder prune -a -f
image_name="maven_jdk8"
version="latest"
if [ `docker ps -a --format "{{.Image}}" | grep ${image_name}:${version} | wc -l` -gt 1 ]; then
echo "当前存在容器占用镜像,请先清除容器后再构建镜像"
exit 1
fi
if [ `docker images --format "{{.Repository}}:{{.Tag}}" | grep ${image_name}:${version} | wc -l` -gt 0 ]; then
echo "存在残留同名镜像,正在清除"
docker rmi -f ${image_name}:${version}
fi
echo "开始构建镜像"
docker build --no-cache -t $image_name .
编译Jar包
这个是我在Jenkins上写的,把源码挂载到镜像中。以及挂载宿主机的本地maven仓库(setting.xml本地仓库配置的就是/opt/repo/maven
),以后构建就不用下载依赖了
docker run \
--rm \
-v /opt/jenkins/data/workspace/rycem_prod:/rycem/source \
-v /opt/repo/maven:/opt/repo/maven \
--name maven_jdk8_package \
maven_jdk8 \
sh -c "cd /rycem/source && mvn package -Dmaven.test.skip=true"
评论区