之前都是间接用公司的DevOps来打包公布,对容器镜像相干并不理解,当初开始从零学习相干常识( 争脸-_-|| )。

1. 创立maven我的项目

1. 从spring initializr下载webflux空我的项目

2. 增加测试接口

@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }    @Bean    public RouterFunction<ServerResponse> routerFunction() {        return RouterFunctions.route(RequestPredicates.path("/demo/hello/{name}"),                request -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)                        .body(Mono.just("hello " + request.pathVariable("name")), String.class)        );    }}

启动我的项目并拜访http://localhost:8080/demo/hello/name,浏览器显示hello name

2. 创立docker镜像

1. 创立Dockerfile文件

这里间接用了公司的jdk11根底镜像:

#tencent konajdk11FROM xxxx.xxxx.com/tjdk/tencentkona11LABEL maintainer="xxxx@tencent.com"# 我的项目放在指定目录下RUN mkdir -p /appADD target/*.jar /app#encoding settingsENV LANG en_US.UTF-8ENV LANGUAGE en_US:enENV LC_ALL en_US.UTF-8WORKDIR /appCMD java -Dfile.encoding=utf-8 \    -jar /app/demo-webflux-*.jar

2. 构建镜像

将我的项目打包好的jar文件Dockerfile文件上传到测试服务器。(开发机上没装置docker,专门申请了台测试服务器来做测试,测试服务器装置了docker)。

  • 因为测试推送到公司的外部仓库,先在命令行上登录仓库:

    docker login xxx.xxx.com
  • 构建镜像

    docker build -t xxx.xxx.com/xxx-dev/demo-webflux:latest -f Dockerfile .
  • 推送到仓库

    docker push xxx.xxx.com/xxx-dev/demo-webflux:latest

    通过docker images 能够看到构建的镜像。

3. 启动容器

执行命令

docker run -p 8080:8080 -itd  xxx.xxx.com/xxx-dev/demo-webflux

具体参考可见:Docker 命令大全
在浏览器上输出:http://测试服务器IP:8080/demo/hello/name,浏览器显示hello name