springBoot微服务打包成docker镜像
使用docker跑服务非常方便,不需要在服务实例上繁琐的安装运行环境,通过编写dockefile快速构建镜像,能够快速运行应用,开发首选
1、准备jar包
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-flBRkpY9-1624332371447)(C:\Users\Meet\AppData\Roaming\Typora\typora-user-images\image-20210622102829673.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-flBRkpY9-1624332371447)(C:\Users\Meet\AppData\Roaming\Typora\typora-user-images\image-20210622102829673.png)]](https://ucc.alicdn.com/gyexla2gtwvm4/developer-article1138085/20241026/2e2ed579e8a547549f6e49a3dbad1f56.png?x-oss-process=image/resize,w_1400/format,webp)
2、编写DockerFile
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bWuubFFB-1624332371451)(C:\Users\Meet\AppData\Roaming\Typora\typora-user-images\image-20210622102919722.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bWuubFFB-1624332371451)(C:\Users\Meet\AppData\Roaming\Typora\typora-user-images\image-20210622102919722.png)]](https://ucc.alicdn.com/gyexla2gtwvm4/developer-article1138085/20241026/98ef72f34824450f965889a8df4b65f4.png?x-oss-process=image/resize,w_1400/format,webp)
3、上传,build生成镜像
[root@VM-8-9-centos test]# ls
Dockerfile test-0.0.1-SNAPSHOT.jar
[root@VM-8-9-centos test]# docker build -f Dockerfile -t springboot-image
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
[root@VM-8-9-centos test]# docker build -f Dockerfile -t springboot-image .
Sending build context to Docker daemon 17.6MB
Step 1/5 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
---> 3a53cad9ae39
Step 3/5 : CMD ["--server.port=8080"]
---> Running in b53ca055dafd
Removing intermediate container b53ca055dafd
---> 3aa1b8767fea
Step 4/5 : EXPOSE 8080
---> Running in 20c50a75f15c
Removing intermediate container 20c50a75f15c
---> b8e7072c5ae8
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
---> Running in 5edbe3e7a251
Removing intermediate container 5edbe3e7a251
---> d99e96e3c5dd
Successfully built d99e96e3c5dd
Successfully tagged springboot-image:latest
4、运行容器,访问
[root@VM-8-9-centos test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot-image latest d99e96e3c5dd About a minute ago 661MB
tomcat-image 1.0 271418e6337f 36 hours ago 638MB
tomcat 9.0.48-jdk8-adoptopenjdk-openj9 d716ed54947b 5 days ago 386MB
redis latest fad0ee7e917a 2 weeks ago 105MB
nginx latest d1a364dc548d 3 weeks ago 133MB
mysql 8.0.25 c0cdc95609f1 5 weeks ago 556MB
centos latest 300e315adb2f 6 months ago 209MB
java 8 d23bdf5b1b1b 4 years ago 643MB
[root@VM-8-9-centos test]# docker run -d -p 9999:8080 --name springboot-test springboot-image
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xIOTFeKg-1624332371453)(C:\Users\Meet\AppData\Roaming\Typora\typora-user-images\image-20210622103520607.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xIOTFeKg-1624332371453)(C:\Users\Meet\AppData\Roaming\Typora\typora-user-images\image-20210622103520607.png)]](https://ucc.alicdn.com/gyexla2gtwvm4/developer-article1138085/20241026/192861889e484e4ea0f7f105af147e81.png?x-oss-process=image/resize,w_1400/format,webp)