赞
踩
设置临时变量
- # ===> 临时变量
- container_name=mytest
- jar_name=mytest-1.0-SNAPSHOT.jar
- port=8080
- # ===> 临时变量
创建shell
- # java_image_dockerfile
- cat > java_image_dockerfile <<'EOF'
- FROM openjdk:8-jdk
-
- RUN ln -sf /usr/share/zoneinfo/Asia/ShangHai /etc/localtime \
- && echo Asia/Shanghai > /etc/timezone \
- && dpkg-reconfigure -f noninteractive tzdata
- CMD ["sh","/home/startup.sh"]
- EOF
- docker build -t java_image:1.0 -f java_image_dockerfile .
-
- # startup.sh
- cat > startup.sh <<'EOF'
- jar=jar_name
- cd /home && java -jar *.jar --spring.profiles.active=prod
- while true
- do
- ps=`ps aux|grep $jar |grep -v color | wc -l`
- if [ $ps -eq 0 ]; then
- exit
- fi
- sleep 30s
- done
- EOF
- sed -i "s#jar_name#$jar_name#g" startup.sh
-
- # run.sh
- cat > run.sh <<'EOF'
- path="$( cd "$( dirname "$0" )" && pwd )"
- docker run -d --name=container_name --log-opt max-size=10m --log-opt max-file=3 \
- --restart=always \
- -v $path:/home \
- -v $path/log:/logs \
- -p port:port java_image:1.0
- EOF
- sed -i "s#container_name#$container_name#g" run.sh
- sed -i "s#port#$port#g" run.sh
-
-
- cat > bin.sh <<'EOF'
- case "$1" in
- start)
- docker start container_name
- ;;
- stop)
- docker stop container_name
- ;;
- restart)
- docker restart container_name
- ;;
- exec)
- docker exec -it container_name bash
- ;;
- rm)
- docker rm -f container_name
- ;;
- log)
- docker logs -f --tail 100 container_name
- ;;
- *)
- echo "start|stop|restart|log"
- ;;
- esac
- EOF
- sed -i "s#container_name#$container_name#g" bin.sh
-
- chmod +x run.sh bin.sh
创建容器
./run.sh
其他命令
- # 启动
- ./bin.sh
- # 停止
- ./bin.sh stop
- # 重启
- ./bin.sh restart
- # 查看最新日志
- ./bin.sh log
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。