docker -SpringBoot微服务打包Docker镜像
Posted hequnwang10
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker -SpringBoot微服务打包Docker镜像相关的知识,希望对你有一定的参考价值。
1、架构springboot项目
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!";
}
}
2、打包应用
3、编写dockerfile
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
将jar包和Dockerfile上传至服务器
4、构建镜像
root@vm:/home/idea# ls
demo-0.0.1-SNAPSHOT.jar Dockerfile
#构建镜像
docker build -t whq .
root@vm:/home/idea# docker build -t whq .
Sending build context to Docker daemon 17.06MB
Step 1/5 : FROM java:8
root@vm:/home/idea# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
whq latest 8faba24825d8 About a minute ago 660MB
tomcat latest c43a65faae57 6 days ago 667MB
redis 5.0.9-alpine3.11 3661c84ee9d0 13 months ago 29.8MB
java 8 d23bdf5b1b1b 4 years ago 643MB
5、发布运行
#发布运行
root@vm:/home/idea# docker run -d -P --name whq-springboot-web whq
cc76bb7b1f2fb33a2cbecbdbc3f5e317b7a2d4cc8cb54fe9552822033767f66c
root@vm:/home/idea# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc76bb7b1f2f whq "java -jar /app.jar …" 14 seconds ago Up 13 seconds 0.0.0.0:49159->8080/tcp, :::49159->8080/tcp whq-springboot-web
root@vm:/home/idea# curl localhost:49159
{"timestamp":"2021-05-20T14:20:47.461+00:00","status":404,"error":"Not Found","message":"","path":"/"}
root@vm:/home/idea# curl localhost:49159/hello
hello!
完成发布!
以上是关于docker -SpringBoot微服务打包Docker镜像的主要内容,如果未能解决你的问题,请参考以下文章