一、背景
之前写了一篇文章:docker-compose部署java项目,每次使用之前需要在本地ide环境打包成jar包,然后上传到linux环境,进入到指定目录,执行docker-compose build
,完毕后执行docker-compose up -d
,这一路下来麻烦至极,于是想着为啥不能用脚本来执行打包jar、生成并运行docker镜像,于是这篇一键式部署由此而生...
二、环境
# 1. jdk 1.8(不是jre 1.8) [root@192 ~]# java -version java version "1.8.0_202" Java(TM) SE Runtime Environment (build 1.8.0_202-b08) Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode) # 2. maven [root@192 ~]# mvn --version Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00) Maven home: /root/apache-maven-3.5.4 Java version: 1.8.0_202, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_202-amd64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-1127.18.2.el7.x86_64", arch: "amd64", family: "unix" # 3. git [root@192 ~]# git --version git version 1.8.3.1 # 4. docker-compose [root@192 ~]# docker-compose -v docker-compose version 1.24.1, build 4667896b
三、具体实现
- 配置ssh,方便拉取仓库中的代码,参考超详细!linux环境git clone探坑录
- 编写dockerFile,参考Docker(四) 通过DockerFile自定义镜像
- 编写docker-compose.yml,参考docker-compose部署java项目
- 编写部署脚本
deployment.sh
#!/usr/bin/env bash # 清理容器 docker system prune -f docker network prune -f # 打包 mvn clean install -Dmaven.test.skip=true # 生成并运行docker images docker-compose -f docker-compose.yml up --build -d
- cd指定目录,拉取代码
git clone xxxxxx.git
- cd进入到脚本目录,并赋权限,不然无法执行
[root@192 ~]# cd /root/test/car_wash_api/scripts/ [root@192 scripts]# ll total 4 -rwxrwxrwx. 1 root root 230 Dec 10 14:31 deployment.sh [root@192 scripts]# chmod 777 deployment.sh
- cd到项目根目录,执行脚本
[root@192 car_wash_api]# ./scripts/deployment.sh java_test Untagged: java/test:8.0.0 Deleted: sha256:fbe38eaedd94b14bc73df95ca2e2aa3964da176413f79b9a074c415ac081aa4a Deleted: sha256:af64b61cb4ee12414de33acefe59de973dab654467d86f5ebe10fa7cd33ba120 Deleted: sha256:6a603856139b6f6bbcdc5d0afaa71a782417949e17951b1c9ead3d54efab5ebd [WARNING] [WARNING] Some problems were encountered while building the effective settings [WARNING] expected START_TAG or END_TAG not TEXT (position: TEXT seen ...</mirror>\n -->\n <mirror>\n\n\ua0<i... @164:4) @ /root/apache-maven-3.5.4/conf/settings.xml, line 164, column 4 [WARNING] [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.example:docker_images >---------------------- [INFO] Building docker_images 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ docker_images --- [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ docker_images --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ docker_images --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 3 source files to /root/test/car_wash_api/target/classes [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ docker_images --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ docker_images --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ docker_images --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ docker_images --- [INFO] Building jar: /root/test/car_wash_api/target/docker_images-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.3.5.RELEASE:repackage (repackage) @ docker_images --- [INFO] Replacing main artifact with repackaged archive [INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ docker_images --- [INFO] Installing /root/test/car_wash_api/target/docker_images-0.0.1-SNAPSHOT.jar to /root/.m2/repository/com/example/docker_images/0.0.1-SNAPSHOT/docker_images-0.0.1-SNAPSHOT.jar [INFO] Installing /root/test/car_wash_api/pom.xml to /root/.m2/repository/com/example/docker_images/0.0.1-SNAPSHOT/docker_images-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.560 s [INFO] Finished at: 2020-12-10T14:03:40-05:00 [INFO] ------------------------------------------------------------------------ [WARNING] The requested profile "local" could not be activated because it does not exist. Building test Step 1/5 : FROM openjdk:8 ---> 192ceee8f2fd Step 2/5 : LABEL description "Java 8" ---> Using cache ---> 64211c0ae4ae Step 3/5 : EXPOSE 8088 ---> Using cache ---> 3108e3140265 Step 4/5 : ADD /target/docker_images-0.0.1-SNAPSHOT.jar docker_images-0.0.1-SNAPSHOT.jar ---> 15a61a7a9a78 Removing intermediate container d7e4d78a6e49 Step 5/5 : ENTRYPOINT java -jar docker_images-0.0.1-SNAPSHOT.jar ---> Running in 0c291157db5f ---> c670624febf2 Removing intermediate container 0c291157db5f Successfully built c670624febf2 Creating java_test ... done
- 查看项目是否启动,是否可以正常访问
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c92658ee7f3b java/test:8.0.0 "java -jar docker_..." 6 seconds ago Up 5 seconds 0.0.0.0:8088->8088/tcp java_test
image
四、补充说明
- 安装环境的时候可能会碰到很多的坑,放心,我已经帮您趟过了,有问题可以参考这篇centos 7 yum安装失败(HTTP Error 404 - Not Found)的解决方法。
- 如果有需要,可以访问源码地址