当前位置:   article > 正文

docker(20) : 构建jar运行容器,挂载jar包运行_docker 挂载jar包

docker 挂载jar包

 设置临时变量

  1. # ===> 临时变量
  2. container_name=mytest
  3. jar_name=mytest-1.0-SNAPSHOT.jar
  4. port=8080
  5. # ===> 临时变量

创建shell

  1. # java_image_dockerfile
  2. cat > java_image_dockerfile <<'EOF'
  3. FROM openjdk:8-jdk
  4. RUN ln -sf /usr/share/zoneinfo/Asia/ShangHai /etc/localtime \
  5. && echo Asia/Shanghai > /etc/timezone \
  6. && dpkg-reconfigure -f noninteractive tzdata
  7. CMD ["sh","/home/startup.sh"]
  8. EOF
  9. docker build -t java_image:1.0 -f java_image_dockerfile .
  10. # startup.sh
  11. cat > startup.sh <<'EOF'
  12. jar=jar_name
  13. cd /home && java -jar *.jar --spring.profiles.active=prod
  14. while true
  15. do
  16. ps=`ps aux|grep $jar |grep -v color | wc -l`
  17. if [ $ps -eq 0 ]; then
  18. exit
  19. fi
  20. sleep 30s
  21. done
  22. EOF
  23. sed -i "s#jar_name#$jar_name#g" startup.sh
  24. # run.sh
  25. cat > run.sh <<'EOF'
  26. path="$( cd "$( dirname "$0" )" && pwd )"
  27. docker run -d --name=container_name --log-opt max-size=10m --log-opt max-file=3 \
  28. --restart=always \
  29. -v $path:/home \
  30. -v $path/log:/logs \
  31. -p port:port java_image:1.0
  32. EOF
  33. sed -i "s#container_name#$container_name#g" run.sh
  34. sed -i "s#port#$port#g" run.sh
  35. cat > bin.sh <<'EOF'
  36. case "$1" in
  37. start)
  38. docker start container_name
  39. ;;
  40. stop)
  41. docker stop container_name
  42. ;;
  43. restart)
  44. docker restart container_name
  45. ;;
  46. exec)
  47. docker exec -it container_name bash
  48. ;;
  49. rm)
  50. docker rm -f container_name
  51. ;;
  52. log)
  53. docker logs -f --tail 100 container_name
  54. ;;
  55. *)
  56. echo "start|stop|restart|log"
  57. ;;
  58. esac
  59. EOF
  60. sed -i "s#container_name#$container_name#g" bin.sh
  61. chmod +x run.sh bin.sh

创建容器

./run.sh

其他命令

  1. # 启动
  2. ./bin.sh
  3. # 停止
  4. ./bin.sh stop
  5. # 重启
  6. ./bin.sh restart
  7. # 查看最新日志
  8. ./bin.sh log

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号