1.新建springboot项目
2.打包项目
3.编写Dockerfile
4.构架镜像
5.发布运行
6.上传至私服
7.拉取私服镜像运行
1.新建springboot项目
通过idea创建一个项目,新建一个HelloController 并新增一个hello方法便与测试
package com.gblfy.hello.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("hello")public String hello() {return "welcome gblfy.com";}
}
运行项目:访问localhost:8080/hello
请求是否返回welcome gblfy.com
2.打包项目
通过idea打包
or
cd gblfy-hello
mvn clean package
3.编写Dockerfile
在项目根路径下面创建Dockerfile
FROM java:8COPY *.jar /app.jarCMD ["--server.port=8080"]EXPOSE 8080ENTRYPOINT ["java","-jar","/app.jar"]
4.构构建镜像
在宿主机上新家一个目录idea(名称自定义)
将target下面打包好的项目jar(gblfy-hello-0.0.1-SNAPSHOT.jar)和项目根目录下面的Dockerfile文件上传至宿主机
开始构建镜像
docker build -t docker-gblfy-springboot:1.0 .
构建记录:
[root@harbor idea]# docker build -t docker-gblfy-springboot:1.0 .
Sending build context to Docker daemon 17.62MB
Step 1/5 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar---> 2c0c4693978e
Step 3/5 : CMD ["--server.port=8080"]---> Running in 314b4f66983b
Removing intermediate container 314b4f66983b---> eb868b307f32
Step 4/5 : EXPOSE 8080---> Running in 0446be7bc6a1
Removing intermediate container 0446be7bc6a1---> cedaf2924d66
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]---> Running in a298708effe5
Removing intermediate container a298708effe5---> 732ee773b3af
Successfully built 732ee773b3af
Successfully tagged docker-gblfy-springboot:1.0
[root@harbor idea]#
5.发布运行
查看docker镜像
systemctl start dockerdocker images
创建容器并运行
docker run -d --name docker-gblfy-springboot -p 8080:8080 docker-gblfy-springboot:1.0
运行记录
[root@harbor idea]# docker run -d --name docker-gblfy-springboot -p 8080:8080 docker-gblfy-springboot:1.0
c37cceec7a1615cb838d31346b94c95681f289375801b6a98e1fa4a764dde9fe[root@harbor idea]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37cceec7a16 docker-gblfy-springboot:1.0 "java -jar /app.jar …" 4 seconds ago Up 2 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp docker-gblfy-springboot
6.上传至私服
7.拉取私服镜像运行