Springboot3 Dockerfile Build Image

博客介绍了Spring Boot应用的Docker镜像构建过程。先有一个Spring Boot应用并提供API接口,接着在项目根目录添加Dockerfile文件,最后运行docker build image等命令,还涉及docker tag操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 首先有一个 springboot 应用, 提供一个 API 接口(Endpoint) springboot demo project
  2. 在项目根目录下加一个文件,名为: Dockerfile
  3. 运行 docker 命令, docker build image ...
Reference: https://www.baeldung.com/dockerizing-spring-boot-application

运行环境: macOS Venture + docker 4.16.2
# Dockerfile
FROM openjdk:17-jdk-slim-buster
MAINTAINER jory
COPY build/libs/sp3demo-0.0.1-SNAPSHOT.jar sp3demo.jar
ENTRYPOINT ["java","-jar","/sp3demo.jar"]
This file contains the following information:

FROM: As the base for our image, we'll take the Java-enabled Alpine Linux created in the previous section.
MAINTAINER: The maintainer of the image.
COPY: We let Docker copy our jar file into the image.
ENTRYPOINT: This will be the executable to start when the container is booting. We must define them as JSON-Array because we'll use an ENTRYPOINT in combination with a CMD for some application arguments.


docker build & Run


# To create an image from our Dockerfile.
   
# $> docker build --tag=message-server:latest .
    
➜  sp3demo git:(dockerfile) ✗ docker build --tag=sp3demo:latest .
    
➜  sp3demo git:(dockerfile) ✗ docker image ls
    REPOSITORY               TAG       IMAGE ID       CREATED             SIZE
    sp3demo                  latest    d501eca26277   About an hour ago   424MB
    docker/getting-started   latest    3e4394f6b72f   7 weeks ago         47MB
  
# run the container from our image
  
# $> docker run -p8887:8888 message-server:latest
  
docker run -p8089:8081 sp3demo:latest
  
Issue:
  1. http://localhost:8081/msg works fine but http://localhost:8081/sp3demo/msg does NOT work.
@RequestMapping("sp3demo") is not working.



Docker push
1. 本地登录你的 docker 账号. (我是用 docker desktop 登录好, 然后命令行执行)
2. 给本地image打 tag, tag包含 docker 用户名.

docker tag local_image_name {your name}/image_name

✗ docker tag sp3demo jory1002/sp3demo
✗ docker image ls
REPOSITORY                        TAG                   IMAGE ID       CREATED             SIZE
jory1002/sp3demo                  latest                3eb6eb853858   59 minutes ago      479MB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值