[Quick learning] docker build java project practice

noname
中文
Before, I used the company's DevOps to package and release it directly, and I didn't know much about container images. Now I'm starting to learn relevant knowledge from zero ( shame-_-|| ).

1. Create a maven project

1. Download the webflux empty project spring initializr

image.png

2. Add test interface

@SpringBootApplication
public 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)
        );
    }
}

Start the project and visit http://localhost:8080/demo/hello/name , the browser shows hello name .

2. Create a docker image

1. Create a Dockerfile

The company's jdk11 base image is directly used here:

#tencent konajdk11
FROM xxxx.xxxx.com/tjdk/tencentkona11

LABEL maintainer="xxxx@tencent.com"

# 项目放在指定目录下
RUN mkdir -p /app
ADD target/*.jar /app

#encoding settings
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

WORKDIR /app
CMD java -Dfile.encoding=utf-8 \
    -jar /app/demo-webflux-*.jar

2. Build the image

Upload the packaged jar file and Dockerfile file to the test server. (Docker is not installed on the development machine, and a test server is specially applied for testing, and the test server is installed with docker).

  • Because the tests are pushed to the company's internal repository, first log in to the repository on the command line:

    docker login xxx.xxx.com
  • build image

    docker build -t xxx.xxx.com/xxx-dev/demo-webflux:latest -f Dockerfile .
  • push to repository

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

    docker images can see the built image through 061ed2e3760bbf.

3. Start the container

Excuting an order

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

For detailed reference, see: Docker Command Encyclopedia
Enter on the browser: http://test server IP:8080/demo/hello/name, the browser displays hello name .

阅读 732

一只菜狗

281 声望
32 粉丝
0 条评论

一只菜狗

281 声望
32 粉丝
文章目录
宣传栏