Docker learning journey - using Docker deployment jar package

The project package to generate executablejar

We went to the root directory of the project, run mvn clean packagethe project package, after running will targetgenerate a directory xxx.jarof executable jarfiles.

Production Dockermirror

First, we will prepare a xxx.jarcopy to our  Ubuntu18  on the machine, do not copy the method described, we recount it. I am here I will generate docker-demo-0.0.1.jarcopied to the /var/docker/folder. Here we do not necessarily use Linuxthe machine, but here I used the demo, as far as possible in line with the production environment.

writeDockerfile

Use vi Dockerfileof the file for editing.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD docker-demo-0.0.1.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
 

The above explanation related commands:

  1. Docker get from the warehouse openjdkas a container of our project
  2. VOLUMEPoints to a /tmpdirectory, because the Spring Boot using the built-in Tomcat container, Tomcat is used by default /tmpas the working directory. The effect is the host /var/lib/dockercreates a temporary file in the directory, and is connected to the container /tmp.
  3. The project docker-demo-0.0.1.jaras app.jaradded to the container.
  4. ENTRYPOINT Implementation of the project  app.jar. In order to shorten the Tomcat startup time, add a system property points /dev/urandom as Entropy Source

Construction of Docker Mirror

We /var/docker/testdockerdirectory, execute Dockerthe command to build the image.

docker build -t docker-user-service:latest .

 

This command is to use Dockerthe buildcommand to build the mirror, and the mirror has a name for docker-demo-user-serviceit TAGis latestin the current folder.

We used docker imagesto see all the images.

 

We found that there are our mirror list generated image docker-demo.

 

Run-time image

Through the above steps we have completed the creation of the mirror, then we only need to use the docker run -p 8080:8080 -t jerome.xin/docker-demo command to run our projects. Next we pass http://192.168.2.119:8080/access to the project. Results are as follows:

 

 

We can see the normal return our expected results. Here to explain 192.168.2.119this is the address of my virtual machines. If you do not want to see Spring Bootthe startup log we can use this command to docker run -d -p 8080:8080 -t jerome.xin/docker-demostart the project.

Execution docker psto see the image that we have to run.

 docker ps

If you need to stop the vessel, you can execute the following command.

docker stop 55e9e3a65e07

 

 

to sum up

Use Dockercan be used in the implementation of jarthe production and operation of the mirror, it can be summarized in two steps. The first step is ready to be executed by a jarfile, the second step to prepare Dockerfiledocuments, and finally use Dockerthe buildcommand you can put an executable jarproduction of documents into images. Because our executable jarfile is used embedded Tomcatcontainer, so we are just from Dockerthe central warehouse to pull a JDKas a container to our image.

Spring Boot,Docker,Jar

Published 146 original articles · won praise 21 · views 180 000 +

Guess you like

Origin blog.csdn.net/lifeifei2010/article/details/104425588