jar包打包成镜像文件

jar包打包成镜像文件

目录结构
jar包打包成镜像文件_第1张图片
在这里插入图片描述
jar包打包成镜像文件_第2张图片jar包打包成镜像文件_第3张图片

build.sh

#!/bin/bash
# set -x
echo "---------------------------------------------"
echo "1、打包的文件夹与镜像名保持一致"
echo "2、打包的文件夹中必须包含Dockerfile与jar包"
echo "3、Dockerfile中定义的jar包名称与提供的jar包名一致"
echo "---------------------------------------------"
image_name=$1
if [ "$image_name" == "" ]; then
    echo "请输入打包的镜像名:"
    read input
    if [ "$input" == "" ]; then
        echo "镜像名不能为空"
        exit 1
    fi
    image_name=$input
fi
shares=()
function read_dir(){
    for file in `ls $1`
    do
    cp -r $1"/"$file $image_name
    shares[${#shares[@]}]=$file
    done
}
function mv_bak(){
    if [ ! -d "$image_name/bak" ]; then
        mkdir $image_name"/bak"
    fi
    for file in ${shares[*]}
    do
        if [ -d "$image_name/bak/$file" ]; then
           rm -rf "$image_name/bak/$file"
        fi
        mv $image_name"/"$file"/" $image_name"/bak/"
    done
}
read_dir share
docker build -t $image_name $image_name
mv_bak
input="n"
echo "是否需要导出? (y/n) "
read input
if echo $input | grep -qi "y"; then
    bash export.sh $image_name $image_name/${image_name}.tar.gz
else
    exit 1
fi

Dockerfile

FROM openjdk:8
WORKDIR /app/etc/
LABEL USER=HULAN

#名称为你打包的jar
ENV JARNAME=datalook-wechat-ui-2.1.4.RELEASE.jar

COPY ${JARNAME} /app/lib/${JARNAME}
COPY startup.sh /app/bin/startup.sh

RUN chmod +x /app/bin/startup.sh
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENTRYPOINT ["sh","/app/bin/startup.sh"]

startup.sh

set -x
#如果未配置xmx和xms则使用感知功能
if [ ! -z "${XMX}" ] && [ ! -z "${XMS}" ]; then
    JAVA_OPT="${JAVA_OPT} -Xmx${XMX} -Xms${XMS}"
fi

JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError"
JAVA_OPT="${JAVA_OPT} -XX:HeapDumpPath=/app/etc"
JAVA_OPT="${JAVA_OPT} -XX:+ExitOnOutOfMemoryError"
JAVA_OPT="${JAVA_OPT} -Duser.timezone=GMT+08"
JAVA_OPT="${JAVA_OPT} -Dspring.config.location=/app/etc/"
JAVA_OPT="${JAVA_OPT} /app/lib/${JARNAME}"

java -jar ${JAVA_OPT}

打包的镜像包会复制到本地文件夹里面

你可能感兴趣的:(笔记,jar,java,log4j,运维,linux)