In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to package SpringBoot applications into Docker images and run". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
(1) first of all, there must be a working SpringBoot application.
Clone the github repository from the github of Jerry to local:
Cd goes into the project folder and uses the command line mvn spring-boot:run
When you see the prompt that the console outputs Tomcat started on port: 5030 (http), the SpringBoot application starts successfully locally.
At this point, you can access the SpringBoot application using the following url, if everything is all right
Http:///product
You can see Hello World in the browser:
Note: the port that the SpringBoot application listens to is 5030. If you want to change it to another port, modify it in application.properties.
(2) the next step is to log in to the Ali CVM and package the SpringBoot into a Docker image.
I have already written a Dockerfile file in my github repository, and the Docker image is based on this Dockerfile.
The first line of the FROM command specifies that our image is based on the openjdk image.
The second line of the VOLUME command defines a persistent store that points to the tmp folder in the container. The default working directory created by the SpringBoot application for the built-in Tomcat server instance is tmp. With this command, you can create a temporary directory in the host directory / var/lib/docker where Docker is running, and connect it to the tmp inside the container.
If your SpringBoot application does not perform persistent writes, this step can be omitted.
Third, add the typed jar file in the target folder in the local directory to the container and rename it app.jar.
Line 4: the function of the ENV command is to set environment variables. In a complex usage scenario, we may need to start JVM with various parameters that are passed into the Java command through the environment variables set by the ENV command. In this simple example, the setting of the environment variable can be omitted.
Line 5: ENTRYPOINT, as the name implies, is the starting point for the container image to run.
Now that we understand the purpose and syntax of the Dockerfile, we use docker build to generate an image based on the Dockerfile.
> docker build-t jerry/springbootexample:v1.
The last "." of the above command line. It is not a punctuation mark that indicates the end, but the "." in the Linux system, which represents the current directory.
After executing the above command line, you will see that the five commands we defined in Dockerfile are executed sequentially. The first step is to download the basic image openJDK:
Then perform the remaining steps in turn.
When you see the Successfully built prompt message, it indicates that the mirror was created successfully.
Using the docker images command line, you can see the image, the size of which is 136MB.
(3) finally, use the docker run command to perform the mirror.
The run command has many parameters, such as running the image interactively:
> docker run-it jerry/springbootexample:v1
In this way, the output of the mirror processing the user's request is automatically redirected to the console of the host.
The-p parameter can achieve port mapping. The following command line means to map the port listened to by the SpringBoot application in Docker to port 8000 of the host. In this way, when the user accesses in the browser, the port used should be the host's port 8000.
> docker run-p 8000 9000-- name jerrydockerdemo-d jerry/springbootexample:v1
You can use the docker ps command to get the ID of the running image, and then use the docker stop command to terminate the operation of the image.
You can also use the command docker exec-it to enter a running container:
> sudo docker exec-it 8302db78f838 / bin/sh
When we were writing Dockerfile, the tmp created with the VOLUME instruction can be observed inside the container at this time. In tmp, sure enough, we found the data generated under the working directory of its built-in Tomcat instance during the execution of SpringBoot.
This is the end of "how to package SpringBoot applications into Docker images and run". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.