SpringBoot 打包为Docker进行
可以有两种方式:
1、dockerfile
2、maven docker
第一种方式:通过dockerfile打包Docker镜像
1、将dockerfile和 springboot打包的jar文件放在一起
[root@docker springboot-docker]# ls dockerfile hot-0.0.1.jar
2、dockerfile文件内容
FROM docker.io/ascdc/jdk8 # jdk8 基础镜像 ENV HOME_PATH /spring # 定义目录变量 RUN ["mkdir", "-p", "/spring"] # 在基础镜像中创建目录 ADD hot-0.0.1.jar $HOME_PATH/app.jar # 将本地的jar目录复制到docker基础镜像中 WORKDIR $HOME_PATH # 设置镜像工作目录 ENTRYPOINT ["java", "-jar", "app.jar"] # 镜像启动命令
3、执行镜像生成命令
[root@docker springboot-docker]# docker build -t springboot-hot:v1 . Sending build context to Docker daemon 18.23 MB Step 1/6 : FROM docker.io/ascdc/jdk8 ---> 9f04bbc44406 Step 2/6 : ENV HOME_PATH /spring ---> Using cache ---> 63ce1b9faf62 Step 3/6 : RUN mkdir -p /spring ---> Running in dfb2354ab51f ---> b52f3904f762 Removing intermediate container dfb2354ab51f Step 4/6 : ADD hot-0.0.1.jar $HOME_PATH/app.jar ---> ed41c3a0104e Removing intermediate container c5ccbb211747 Step 5/6 : WORKDIR $HOME_PATH ---> 421f5b686e0f Removing intermediate container 8cbb8cbe870c Step 6/6 : ENTRYPOINT java -jar app.jar ---> Running in 2df6bc912185 ---> f89df931edfe Removing intermediate container 2df6bc912185 Successfully built f89df931edfe
4、查看本地镜像
[root@docker springboot-docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE springboot-hot v1 f89df931edfe 8 minutes ago 662 MB docker.io/ascdc/jdk8 latest 9f04bbc44406 10 months ago 644 MB
5、运行springboot镜像
[root@docker springboot-docker]# docker run -d -p 8080:8080 springboot-hot:v1 78fb24c74e21f57fa3d1f543dd9c60537db65c03fb1e617819831d1c13c1b96e [root@docker springboot-docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 78fb24c74e21 springboot-hot:v1 "java -jar app.jar" 2 seconds ago Up 1 second 0.0.0.0:8080->8080/tcp flamboyant_blackwell
6、浏览器中测试
PS:将war包打包到tomcat中