160_SpringBoot微服务打包Docker镜像


构建springboot项目

image.png

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "hello,world!";
    }
}

image.png

打包测试

image.png
image.png
image.png

idea下载Docker插件

image.png

编写Dockerfile文件

image.png

FROM java:8

COPY *.jar /app.jar

CMD ["--server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

构建镜像

:::info
创建目录/home/springboot,将Dockerfile和demo.jar文件移入
:::

[root@ecs-214025 /]# cd /home/
[root@ecs-214025 home]# ls
admin  ceshi  dockerfile  docker-test-volume  eqroot  mysql  ruoyi  tomcat
[root@ecs-214025 home]# mkdir springboot
[root@ecs-214025 home]# ll
total 36
drwx------ 2 admin admin 4096 Jun 26 17:01 admin
drwxr-xr-x 2 root  root  4096 Jul  3 15:35 ceshi
drwxr-xr-x 2 root  root  4096 Jul  6 00:07 dockerfile
drwxr-xr-x 2 root  root  4096 Jul  3 16:55 docker-test-volume
drwx------ 2  1001  1001 4096 Apr 16 18:20 eqroot
drwxr-xr-x 4 root  root  4096 Jul  3 15:55 mysql
drwxr-xr-x 3 root  root  4096 May  1 16:29 ruoyi
drwxr-xr-x 2 root  root  4096 Jul 11 16:34 springboot
drwxrwxrwx 4 root  root  4096 Jul  7 22:55 tomcat
[root@ecs-214025 home]# chmod 777 springboot/
[root@ecs-214025 home]# ll
total 36
drwx------ 2 admin admin 4096 Jun 26 17:01 admin
drwxr-xr-x 2 root  root  4096 Jul  3 15:35 ceshi
drwxr-xr-x 2 root  root  4096 Jul  6 00:07 dockerfile
drwxr-xr-x 2 root  root  4096 Jul  3 16:55 docker-test-volume
drwx------ 2  1001  1001 4096 Apr 16 18:20 eqroot
drwxr-xr-x 4 root  root  4096 Jul  3 15:55 mysql
drwxr-xr-x 3 root  root  4096 May  1 16:29 ruoyi
drwxrwxrwx 2 root  root  4096 Jul 11 16:34 springboot
drwxrwxrwx 4 root  root  4096 Jul  7 22:55 tomcat
[root@ecs-214025 home]# cd springboot/
[root@ecs-214025 springboot]# ll
total 0
[root@ecs-214025 springboot]# ll
total 16188
-rw-rw-r-- 1 admin admin 16568613 Jul 11 16:36 demo-0.0.1-SNAPSHOT.jar
-rw-rw-r-- 1 admin admin      120 Jul 11 16:35 Dockerfile
[root@ecs-214025 springboot]# 

:::info
构建镜像
注意:末尾的“.”
:::

[root@ecs-214025 springboot]# docker build -t demo .
Sending build context to Docker daemon  16.57MB
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
 ---> bb1eab1a87b2
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in cc7a20d3b9bb
Removing intermediate container cc7a20d3b9bb
 ---> 763abaa27118
Step 4/5 : EXPOSE 8080
 ---> Running in c83f9fc1c8b3
Removing intermediate container c83f9fc1c8b3
 ---> 35e93ab649a8
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 1f250f8c2042
Removing intermediate container 1f250f8c2042
 ---> a9384f1a6a14
Successfully built a9384f1a6a14
Successfully tagged demo:latest
[root@ecs-214025 springboot]# docker images
REPOSITORY   TAG                IMAGE ID       CREATED          SIZE
demo         latest             a9384f1a6a14   17 seconds ago   660MB
tomcat       latest             451d25ef4583   13 days ago      483MB
redis        5.0.9-alpine3.11   3661c84ee9d0   2 years ago      29.8MB
java         8                  d23bdf5b1b1b   5 years ago      643MB
[root@ecs-214025 springboot]# 

创建启动容器测试

[root@ecs-214025 springboot]# docker run -d -P --name demo01 demo
53ed7f7602085949331e5d075681be2b9fc6d2503ebb1344063f57581db5f2a8

[root@ecs-214025 springboot]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                         NAMES
53ed7f760208   demo      "java -jar /app.jar …"   53 seconds ago   Up 52 seconds   0.0.0.0:49168->8080/tcp, :::49168->8080/tcp   demo01

[root@ecs-214025 springboot]# curl localhost:49168/hello
hello,world![root@ecs-214025 springboot]# 

保存镜像

:::info
参考文档:https://blog.csdn.net/qq_44273583/article/details/114387368
:::

[root@ecs-214025 springboot]# docker images
REPOSITORY   TAG                IMAGE ID       CREATED          SIZE
demo         latest             a9384f1a6a14   13 minutes ago   660MB
tomcat       latest             451d25ef4583   13 days ago      483MB
redis        5.0.9-alpine3.11   3661c84ee9d0   2 years ago      29.8MB
java         8                  d23bdf5b1b1b   5 years ago      643MB
[root@ecs-214025 springboot]# docker save a9384f1a6a14 > demo.tar
[root@ecs-214025 springboot]# ll
total 676176
-rw-rw-r-- 1 admin admin  16568613 Jul 11 16:36 demo-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 root  root  675822592 Jul 11 16:55 demo.tar
-rw-rw-r-- 1 admin admin       120 Jul 11 16:35 Dockerfile
[root@ecs-214025 springboot]# 

posted @   清风(学习-踏实)  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-07-11 080_Mybatis缓存
2021-07-11 070_Mybatis动态SQL
2021-07-11 060_Mybatis结果映射(多对一和一对多)
2021-07-11 050_Lombok
2021-07-11 040_Mybatis注解开发
2021-07-11 030_Mybatis日志和分页
2021-07-11 020_Mybatis配置解析
点击右上角即可分享
微信分享提示