Docker部署Springboot WebAPI项目

  • 确定docker安装好
Client:
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.17.3
 Git commit:        20.10.12-0ubuntu4
 Built:             Mon Mar  7 17:10:06 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
  • 写一个简单的springboot项目,端口9000
package com.example.demo.controller;

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

@RestController
public class TestController {

    @GetMapping("hello")
    public String Hello() {
        return "hello world";
    }
}
  • 打包项目,得到demo-0.0.1-SNAPSHOT.jar,并上传到服务器上某个路径下
  • 在该路径下新建Dockerfile
FROM openjdk:19

LABEL version="1.0" maintainer="liu<gisliuliang@qq.com>"

VOLUME /tmp

EXPOSE 9000

COPY demo-0.0.1-SNAPSHOT.jar demo.jar

RUN bash -c "touch /demo.jar"

ENTRYPOINT ["java", "-jar", "/demo.jar"]
  • 制作镜像:sudo docker build -t demo:v1 .
Sending build context to Docker daemon  17.63MB
Step 1/7 : FROM openjdk:19
19: Pulling from library/openjdk
051f419db9dd: Pull complete
aa51c6010a14: Pull complete
dba785fff917: Pull complete
Digest: sha256:4123be55fd6853980020c59e7530d017ea08996abbe71741a51c62f7b7586bee
Status: Downloaded newer image for openjdk:19
 ---> 2e6f6690e479
Step 2/7 : LABEL version="1.0" maintainer="liu<gisliuliang@qq.com>"
 ---> Running in e57b29a5b246
Removing intermediate container e57b29a5b246
 ---> c83d1c179e99
Step 3/7 : VOLUME /tmp
 ---> Running in a784a1301559
Removing intermediate container a784a1301559
 ---> 29ac61d06f0c
Step 4/7 : EXPOSE 9000
 ---> Running in 560e957e9b39
Removing intermediate container 560e957e9b39
 ---> 881d66fde5d0
Step 5/7 : COPY demo-0.0.1-SNAPSHOT.jar demo.jar
 ---> 81624581eb31
Step 6/7 : RUN bash -c "touch /demo.jar"
 ---> Running in cd305a07501f
Removing intermediate container cd305a07501f
 ---> d535e84abab6
Step 7/7 : ENTRYPOINT ["java", "-jar", "/demo.jar"]
 ---> Running in 8136b9aad84f
Removing intermediate container 8136b9aad84f
 ---> 5bfade2bb744
Successfully built 5bfade2bb744
Successfully tagged demo:v1
  • 启动容器:sudo docker run -d -p 9001:9000 demo:v1,9001是服务器端口,9000是项目端口号(容器开放端口号)

  • 测试:curl http://localhost:9001/hello,输出:hello world

posted @ 2022-10-26 16:25  gisliuliang  阅读(70)  评论(0编辑  收藏  举报