- 首先有一个 springboot 应用, 提供一个 API 接口(Endpoint) springboot demo project
- 在项目根目录下加一个文件,名为: Dockerfile
- 运行 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