Springboot project jar package docker deployment

The SpingBoot project is packaged as a jar package and deployed in a docker container starting with two files:

1. Project jar package (not explained here);

2. dockerfile files (files without any suffixes)

2.1 Writing dockerfile:

The contents of the file are as follows:

FROM java:8

VOLUME /tmp

COPY springBootDocker-0.0.1-SNAPSHOT.jar app.jar

RUN bash -c "touch /app.jar"

EXPOSE 9011

ENTRYPOINT ["java","-jar","app.jar","--spring.profiles.active=test","--server.port=9011",">/log/app.log"]

A brief explanation of the above is given:

java:8 refers to the official java mirror on Docker Hub, version number 8 is jdk1.8. With this basic mirror, Dockerfile can get its status directly through the FROM directive - that is, java is installed in the container, and then run the Spring Boot application through custom commands:

VOLUME points to a / tmp directory, and since Spring Boot uses the built-in Tomcat container, Tomcat uses / tmp as its working directory by default.The effect is that a temporary file is created in the host's/var/lib/docker directory and connected to the container's/tmp.

COPY adds the jar file of the project to the container as app.jar

RUN s indicate that some commands are executed in the newly created image and the results are submitted to the current image.The touch command is used here to change the modification time of the file. All container files created by Docker are "unmodified" by default.This is not required for simple applications, but it requires a "modification time" for files with static content such as index.html.

EXPOSE Container Exposure Port

ENTRYPOINT Apply Startup Command Parameter Settings

 

3. Upload the prepared dockerfile files and jar packages to the same folder on the server as follows:

-rw-r--r-- 1 root root      228 Apr 20 10:24 Dockerfile
-rw-r--r-- 1 root root 16103675 Apr 20 10:43 springBootDocker-0.0.1-SNAPSHOT.jar

4. Execute the command Package jar as a mirror (docker build -t mirror name.) Do not drop the last'. 'command

[root@xxxxx dockerTest]# docker build -t spring-boot-docker-test .
Sending build context to Docker daemon  16.11MB
Step 1/6 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/6 : VOLUME /tmp
 ---> Running in 6b4e03ffc2e7
Removing intermediate container 6b4e03ffc2e7
 ---> 8769a34dcd31
Step 3/6 : COPY springBootDocker-0.0.1-SNAPSHOT.jar app.jar
 ---> 949fe4a2a361
Step 4/6 : RUN bash -c "touch /app.jar"
 ---> Running in fb3d86a4882a
Removing intermediate container fb3d86a4882a
 ---> a15fe3f2a1b0
Step 5/6 : EXPOSE 9011
 ---> Running in 04dc3e21e88b
Removing intermediate container 04dc3e21e88b
 ---> d1cccdf738de
Step 6/6 : ENTRYPOINT ["java","-jar","app.jar","--spring.profiles.active=test","--server.port=9011",">/log/app.log"]
 ---> Running in 7e000ca7e6b7
Removing intermediate container 7e000ca7e6b7
 ---> 0969ececcf8b
Successfully built 0969ececcf8b
Successfully tagged spring-boot-docker-test:latest

Succfully indicates that the image was packaged successfully

Execute docker images:

[root@xxxx dockerTest]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
spring-boot-docker-test   latest              0969ececcf8b        37 seconds ago      675MB

5. Run Mirror

[root@xxxx dockerTest]# docker run \
> --name testServer -d \
> -v /home/testServer/logs:/log \
> -p 9011:9011 spring-boot-docker-test

View the running status:

[root@xxxx dockerTest]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                  PORTS                                                               NAMES
135a2da801eb        spring-boot-docker-test   "java -jar app.jar -..."   9 seconds ago       Up 7 seconds            0.0.0.0:9011->9011/tcp                                              testServer

Tags: Programming Docker Spring Java snapshot

Posted on Sun, 19 Apr 2020 21:01:55 -0700 by br