Create a simple Spring boot web project Use the idea tool to create a Spring boot web project. Since it is a test, just click next. Write a test API for access. The service port number does not need to be changed. I changed it to 8701 locally. When the program starts, it is found that the program is not the default port 8080. Visit: http://localhost:8701/v1/hello The above simple web project is built. Now let's run this demo project through docker. The first step is to install Docker (detailed steps are not given here). In the second step, we need a docker image with a java environment. I downloaded one from the NetEase Cloud Mirror Center, address: https://c.163yun.com/hub#/library/repository/info?repoId=65430. You can also find the corresponding image with a Java environment from Alibaba Cloud and other platforms to make it. Get the image and pull it locally docker pull hub.c.163.com/housan993/centos7_jdk8:latest In the third step, we will package the demo project into a jar package and use mvn install. For convenience, I will directly get the generated jar from target to the project root directory. In the fourth step, we write a Dockerfile file under the project to create a mirror of the demo project. Dockerfile file content: FROM hub.c.163.com/housan993/centos7_jdk8:latest COPY demo-0.0.1-SNAPSHOT.jar / CMD java -jar demo-0.0.1-SNAPSHOT.jar After writing the Dockerfile file, we use the Docker command to build an image. The docker command will automatically find the Dockerfile file in the current directory (the default file name is Dockerfile), and then specify the directory path. "." indicates the current docker build -t demo-img . If you see the following log, the image is created successfully. In the fifth step, we start the container of our program according to the created image, and map the port (8701) to 8701 of the local machine. docker run -d -p 8701:8701 demo-image After running, we will get a long string of characters, which is the CONTAINER ID of the container. Let's docker ps and see Let's take a look at the container startup log to see if our Java program is running. docker logs [CONTAINER ID] From the container log, we found that it is exactly the same as the log of starting a Java program locally, so I will access the demo program through the mapped port to see if it works. Let's stop the demo container and see if we can still access it. Let's start our demo container again The above is just a simple use of Docker to run Java programs. If we are interested in the future, we can package, build and other commands into shell scripts, automatically generate container versions, and dynamically take values of parameter variables. I can only say that container talk is very fun, and it will be very convenient for automated deployment of your own projects in the future. The k8s container orchestration tool will be introduced later, which will be even more interesting. This is the end of this article about the steps to start a Java program with docker. For more information about starting a Java program with docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Method of dynamically loading geojson based on Vue+Openlayer
>>: MySQL code execution structure example analysis [sequence, branch, loop structure]
Moreover, an article website built with a blog pro...
Preface MySQL is a relational database with stron...
Using the CSS3 border-image property, you can set...
MyISAM, a commonly used storage engine in MySQL c...
This article summarizes the principles and usage ...
Table of contents summary Problem Description Ana...
Since I installed the official version of IE8.0, ...
Preface Nginx 's built-in module supports lim...
login.html part: <!DOCTYPE html> <html l...
1. Please download the Busybox source code online...
In the project (nodejs), multiple data need to be...
Table of contents What is a Mapping Difference be...
1. Download the virtual machine Official download...
Prelude We all know that nginx is an excellent re...
There is often a lack of understanding of multi-c...