Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy executable jar package in Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Docker how to deploy executable jar package, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Method 1. Directly build the image of the jar package.

Package the project and upload it to the specified directory on the server

Create a Dockerfile file in this directory

Vi Dockerfile

Dockerfile writes the following instructions

FROM java:8MAINTAINER demoADD demo-0.0.1-SNAPSHOT.jar demo.jarEXPOSE 8080ENTRYPOINT ["java", "- jar", "demo.jar"]

The meaning of the instruction is:

FROM: basic image. FROM java:8 refers to pulling an image of jdk1.8.

MAINTAINER: author, MAINTAINER demo author is demo

ADD: copy the packaged executable jar package to the image and rename it (you can also use COPY), and ADD demo-0.0.1-SNAPSHOT.jar demo.jar copy the demo-0.0.1-SNAPSHOT.jar to the image and rename it to demo.jar

EXPOSE: declare port

ENTRYPOINT: the command executed after the container starts. Java-jar demo.jar starts jar.

After creating the Dockerfile file, execute the command to build the image

Docker build-t my-demo.

Pay attention to the last one. Indicates that Dockerfile is in the current file directory. My-demo represents the built image. After a successful construction, you can use the docker images command to view the image.

After the image is built successfully, you can run the container

Docker run-d-- restart=always-- name demo-p 8080 my-demo

The meaning of the parameters:

-d: runs the container in the background and returns the container ID

-restart=always: the container will restart automatically after it stops or the server starts up.

-p: specify port mapping

The last my-demo specifies the image

After starting the container, you can use the docker ps command to view the launched container.

Docker logs-- tail 300-f container id, which can view the service log.

If you want to update the jar package, you can simply use the docker cp demo-0.0.1-SNAPSHOT.jar container ID:/demo.jar, copy the demo-0.0.1-SNAPSHOT.jar into the container and rename it, and then the docker restart container ID restarts the container.

Method 2. Run the container based on jdk image

The first method is to directly build an image to run the jar package, and there is another way to run the container based on the jdk image.

Pull the image of jdk1.8 in the server

Docker pull jdk8

Create a directory and upload the jar package to that directory

Cd / server/ mkdir deploy/jar

Run container: specify the run of the package in the command to run the container

Docker run-d\-restart=always\-v / server/deploy/jar:/jar-v / server/logs/demo:/mnt/logs/demo\-- name demo\ jdk8 / usr/bin/java-jar\-Duser.timezone=GMT+08\ / jar/demo-1.0.jar

The description of the above command:

-Duser.timezone: specify the time zone

Jdk8 / usr/bin/java-jar: execute the jar package

/ jar/demo-1.0.jar: specify the location of the jar package that needs to be executed.

For future release, you only need to replace the jar package in the host directory and restart the container.

Method 3: deploy based on Maven plug-in

Modify Docker configuration to enable remote access to Docker. It is easy to do so. Modify the / usr/lib/systemd/system/docker.service file and add the following:

-H tcp://0.0.0.0:2375-H unix:///var/run/docker.sock

After the configuration is complete, save and exit, and then restart Docker:

Systemctl daemon-reload service docker restart

Create a new Dockerfile file in the directory of the SpringBoot project

FROM java:8VOLUME / tmpADD target/docker-0.0.1-SNAPSHOT.jar app.jarENTRYPOINT ["java", "- jar", "/ app.jar"]

Configure the Maven plug-in: add the following plug-in to the pom.xml file:

Com.spotifydocker-maven-plugin1.2.0build-imagepackagebuild http://192.168.66.131:2375javaboy/${ project.artifactId} ${project.version} true$ {project.basedir} / ${project.build.directory} ${project.build.finalName} .jar

Configuration instructions for this plug-in:

First of all, configure the execution node to execute docker:build when performing mvn package.

Then configure the host address of the Docker, the name of the image, and the tags of the image in the configuration, where dockerDirectory represents the location of the specified Dockerfile.

Finally, configure the location and name of the jar in the resource node.

Execute a command

Is it helpful for mvn clean package docker:build to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report