目录
Dockerfile
Java版:
build_image.sh
run.sh
一般需要添加 Dockerfile,build_image.sh和run.sh三个文件。
Dockerfile
Java版:
# stage1 build java run image
FROM swr.ap-southeast-3.myhuaweicloud.com/it-common/java-base:oracle-jdk-1.8.0_202-with-awscli-obsutil5.1
#从build_image.sh传进来工程目录
ARG PROJECT_DIR
# stage2 copy run jar file
COPY $PROJECT_DIR/target/*.jar /data/code/
COPY $PROJECT_DIR/run.sh /data/code/run.sh
RUN sudo chmod o+x /data/code/run.sh && \
sudo chown -R ${AppUser}:${AppGroup} /data
# set workdir
WORKDIR /data
# stage2
CMD ["/bin/bash","-c","bash /data/code/run.sh $Env"]
Python版:
FROM swr.ap-southeast-3.myhuaweicloud.com/it-common/python-base:sbos-3.6.1-v2.0
#从build_image.sh传进来工程目录
ARG PROJECT_DIR
# copy python file
COPY . /data/code/
COPY $PROJECT_DIR/run.sh /data/code/run.sh
# set workdir
WORKDIR /data
CMD ["/bin/bash","-c","bash /data/code/run.sh $Env]
说明:
- stage1 为程序运行的环境基础镜像,如java项目使用 swr.ap-southeast-3.myhuaweicloud.com/it-common/java-base:oracle-jdk-1.8.0_202-with-awscli-obsutil5.1 ,python项目使用 swr.ap-southeast-3.myhuaweicloud.com/it-sgt/python-base:sbos-3.6.1, 各团队可使用各自不同的自定义镜像;
- stage2 将build产生的jar包或python代码和run.sh脚本拷贝至新镜像
- stage3 dev/test/prod等环境使用同一份docker镜像,通过传入不同的Env参数加载不同的环境配置,支持传入多个参数。如python项目通过传入的Project参数来启动不同的模块;
build_image.sh
- Java
1.存放路径:各项目根路径下;
2.build_image.sh内容
#!/bin/bash
VERSION=$1
PROJECT=$2
BUILD_CMD=$3
PROJECT_DIR=$4
IMAGE_HUB='swr.ap-southeast-3.myhuaweicloud.com/it-sbos'
if [ ! -n "$VERSION" ]; then
exit 2
fi
if [ ! -n "$BUILD_CMD" ]; then
BUILD_CMD="mvn clean install package -Dmaven.test.skip=true"
fi
$BUILD_CMD
echo -------------------------------------------
echo begin build docker images $PROJECT:$VERSION
echo -------------------------------------------
docker build --no-cache --build-arg PROJECT_DIR=$PROJECT_DIR -t $PROJECT:$VERSION .
docker tag $PROJECT:$VERSION $IMAGE_HUB/$PROJECT:$VERSION
docker push $IMAGE_HUB/$PROJECT:$VERSION
- Python
1.存放路径:各项目根路径下;
2.build_image.sh内容
#!/bin/bash
VERSION=$1
PROJECT=$2
PROJECT_DIR=$3
IMAGE_HUB='swr.ap-southeast-3.myhuaweicloud.com/it-sbos'
if [ ! -n "$VERSION" ]; then
exit 2
fi
if [ ! -n "$PROJECT" ]; then
exit 1
fi
echo -------------------------------------------
echo begin build docker images $PROJECT:$VERSION
echo -------------------------------------------
docker build --no-cache --build-arg PROJECT_DIR=$PROJECT_DIR -t $PROJECT:$VERSION .
docker tag $PROJECT:$VERSION $IMAGE_HUB/$PROJECT:$VERSION
docker push $IMAGE_HUB/$PROJECT:$VERSION
run.sh
#!/bin/bash
Env=$1
ProjectName=trans
[ -z $Env ] && echo 'Please input environment argument' && exit 1
#额外执行命令
if [ ! -z ${Ext_CMD} ];then
${Ext_CMD}
fi
# 启动服务
AppDir=$(cd $(dirname $0); pwd)
if [ $Env != "prod" ];
then
# 对应dev/test等非prod环境
ExeArgs="-Xmx1024M -Xms1024M -XX:MaxMetaspaceSize=512M -XX:MetaspaceSize=512M -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:+ParallelRefProcEnabled -Dname=$ProjectName"
else
# 对应prod环境
ExeArgs="-Xmx4096M -Xms4096M -XX:MaxMetaspaceSize=512M -XX:MetaspaceSize=512M -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:+ParallelRefProcEnabled -Dname=$ProjectName"
fi
ExeArgs="$ExeArgs --spring.profiles.active=$Env"
JarFile=$(find $AppDir/ -maxdepth 1 -name "*.jar")
# 以对应用启动时,1)部分文件没有权限;2)/data/logs 挂载到容器后普通用户无写权限
sudo chown -R ${AppUser}:${AppGroup} /data
exec java -jar $JVMArgs $JarFile $ExeArgs &>/dev/null
此文件由各项目负责人自行维护,ExeArgs启动命令由研发自行修改;