SpringBoot项目打包Docker镜像

目的:

将项目打包成docker镜像发布到服务器

将SpringBoot项目进行打包

SpringBoot项目打包Docker镜像_第1张图片

创建Dockerfile文件

SpringBoot项目打包Docker镜像_第2张图片

jar包本地运行没问题之后,将jar包和Dockerfile文件上传至linux服务器SpringBoot项目打包Docker镜像_第3张图片

我这里先启动了openjdk的镜像

将项目打成docker镜像,命令后面有个 .

docker build -t yutao666 .
[root@localhost test]# docker build -t yutao666 .
Sending build context to Docker daemon   69.4MB
Step 1/5 : FROM openjdk
 ---> 5e28ba2b4cdb
Step 2/5 : COPY *.jar /app.jar
 ---> 4a87bb06da90
Step 3/5 : CMD ["--server.prot=8080"]
 ---> Running in 88a0e5e23b5d
Removing intermediate container 88a0e5e23b5d
 ---> 6a1b6abead85
Step 4/5 : EXPOSE 8080
 ---> Running in d1f514b5e6ae
Removing intermediate container d1f514b5e6ae
 ---> 0539168cd6cc
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in c3b46aba88d0
Removing intermediate container c3b46aba88d0
 ---> ac6735e9a7a2
Successfully built ac6735e9a7a2
Successfully tagged yutao666:latest

查看镜像

[root@localhost test]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
yutao666     latest    ac6735e9a7a2   7 seconds ago   541MB
tomcat       latest    5eb506608219   4 hours ago     685MB
redis        latest    1ca2c2a1b554   6 days ago      117MB
openjdk      8         c43167b660ad   7 days ago      526MB
mysql        5.7       8aa4b5ffb001   2 weeks ago     462MB
openjdk      latest    5e28ba2b4cdb   2 weeks ago     471MB
nginx        latest    fa5269854a5e   3 weeks ago     142MB
centos       latest    5d0da3dc9764   8 months ago    231MB

 直接运行镜像

[root@localhost test]# docker run -d -it -p 8080:8080 yutao888 
ad4d6e63611c456c33444362767ae54a1d3f2831652e2ade6dc285f142feb230

 查看镜像是否启动成功

[root@localhost test]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
ad4d6e63611c   yutao666   "java -jar /app.jar …"   3 seconds ago    Up 2 seconds    8080/tcp                                    great_booth
e8a75344ac62   openjdk    "/bin/bash"              24 minutes ago   Up 24 minutes                                               exciting_euler

 访问成功!

SpringBoot项目打包Docker镜像_第4张图片

PS:我这里写的是一个小demo,主要是打包流程,真正打包项目的时候将项目镜像交给运维或者测试,或者直接上传至dockerhub,别人直接拉下来,docker run 就ok了!

你可能感兴趣的:(spring,boot,docker,java)