Docker研学- SpringBoot微服务打包Docker镜像

学习自b站狂神说Docker: https://www.bilibili.com/video/BV1og4y1q7M4/?spm_id_from=333.337.search-card.all.click

SpringBoot微服务打包Docker镜像

  1. 构架springboot项目
  2. 打包应用
  3. 编写dockerfile
  4. 构建镜像
  5. 发布运行
// 构建简单项目 8080端口自测成功
@RestController
public class HelloController {
    @RequestMapping
    public String hello(){
        return "hello,hailong";
    }
}

// 打包 单击Maven中Lifecycle中package选项 jar包在target路径下(show in explorer可直接打开路径)

// 在文件夹中用cmd命令行测试一下能否正常运行
java -jar demo-0.0.1-SNAPSHOT.jar
    
// 当jar包可用,我们需要将他发布到Dockerfile里面(需安装插件Docker),有了插件可以在Settings的Build中的Docker中连接远程的Docker仓库
    
// 右击项目生成file文件(起名为Dockerfile会高亮),进行编写
// 基础镜像是基于java8构建的
FROM java:8
// 将当前目录下的所有jar包都拷到项目下面(最后生成的应用)
// 服务器只发布jar包和dockerfile即可
COPY *.jar /app.jar
// 映射服务的地址
CMD ["--server.port=8080"]
// 暴露端口
EXPOSE 8080
// 执行命令 当前jar包的jar
ENTRYPOINT ["java","-jar","/app.jar"]
    
// home中创建idea目录 将jar包和dockerfile上传到idea中
[root@localhost home]# mkdir idea
[root@localhost idea]# rz
[root@localhost idea]# ls
demo-0.0.1-SNAPSHOT.jar  Dockerfile

// 通过build构建镜像,将jar包装进去
[root@localhost idea]# docker build -t hailong6 .
[root@localhost idea]# docker images
REPOSITORY   TAG                IMAGE ID       CREATED              SIZE
hailong6     latest             2bcc908f0549   About a minute ago   660MB

// 启动镜像
[root@localhost idea]# docker run -d -P --name hailongplay hailong6
    
// 测试连接
[root@localhost idea]# curl localhost:32768
{"timestamp":"2023-10-25T12:33:39.236+00:00","status":404,"error":"Not Found","message":"","path":"/hello"}
    
// 访问地址的hello请求
[root@localhost idea]# curl localhost:32768/hello
hello,hailong

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰勒疯狂展开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值