Case study of CentOS 8.3 and docker deploying springboot project

Time:2021-10-25

introduction

At present, k8s is very hot. I bought a book to learn about it, but k8s is often hundreds of server operation and maintenance. For applications with only a few servers, using k8s is a bit like shooting mosquitoes with a cannon. For the application operation and maintenance of only a few servers, the traditional Tomcat deployment is cumbersome and inefficient. It takes more than ten minutes to deploy a service, and the Jenkins deployment is too complex. After considering for a long time, I chose to deploy using docker + dockerfile. This way of deployment is simple and efficient.

Docker installation

curl -fsSL  https://get.docker.com  |Bash - s docker -- mirror aliyun # one click installation script
Systemctl enable docker.service # sets docker startup
Systemctl restart docker.service # starts the docker service

Open docker remote access port

Edit / usr / lib / SYSTEMd / system / docker.service file and add – H tcp://0.0.0.0:12375 -H unix://var/run/docker.sock As shown below:

After saving the file, reload the configuration and restart

Systemctl daemon reload # reload configuration
    Systemctl restart docker.service # restart the docker service

Note: – H tcp://0.0.0.0:12375 Any IP can use TCP to access this port, and the IP can be matched according to the actual situation. Because there is no token or key here, you should pay special attention. If you use ECs, it is strongly recommended to set it as security group IP white list access. I was attacked three or four hours after using docker in the test environment, but I used port 2375, which is particularly vulnerable to attack.

Configuring the idealocker environment

Idea installs the docker environment plug-in, as shown in the following figure:

After installation, restart idea, open the springboot project, and create a dockerfile in the root directory. As shown below:

Edit dockerfile as follows:

FROM openjdk:11
#The image is inherited from openjdk: 11 JDK Alpin
 
VOLUME /root/tmp
#Indicates that the / root / tmp directory is mounted in the container
 
ADD build/libs/brief-plus-0.0.1-SNAPSHOT.jar apprun.jar
#Add bootjar to the image. The command in the root directory is apprun.jar
 
ENTRYPOINT ["java","-jar","/apprun.jar"]
#Entrypoint executes the Java command to run the program after the container is started
 
#Set container time
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#======== other dockerfile commands ========== we don't use them here, but let's mention them
#COPY package.json /usr/src/app/
#Add more advanced copy files
#The format and nature of add instruction and copy are basically the same. However, some functions are added on the basis of copy.
#CMD instruction is used to specify the startup command of the default container main process.
#Env setting environment variables
#Healthcheck health check
#The expose instruction declares that the runtime container provides a service port. This is just a declaration. The application will not open the service of this port because of this declaration at runtime
EXPOSE 8090

Mainly pay attention to the sentence “build / LIBS / brief-plus-0.0.1-snapshot.jar apprun.jar”. The files compiled with gradle are located in the build / LIBS / directory, as shown in the following figure:

Maven compiled files are located in the target directory, as shown in the following figure:

Dockerfile is obtained and generated according to the specific directory and packaged name, so there must be no error here.

Configure docker service

Configure running environment

Create a TCP connection service

3. Configure the service published to docker

Detailed configuration of binding port IP

Packaging and publishing

Experience and suggestions

1. Docker’s image files are divided into blocks. Each block has its own signature. Each upload will be compared, and the different files will be uploaded again.

2. Remember to change the tag every time you upload it to facilitate rollback according to the tag.

3. To modify the server’s network configuration and firewall, you need to restart the docker service.

Common docker commands:

Restart docker

Systemctl restart docker # restart docker service
Systemctl daemon reload # reloads the docker configuration

View logs within 30 minutes


docker logs --since 30m id

The above is the details of the actual combat records of CentOS 8.3 and docker deploying springboot project. For more information about docker deploying springboot project, please pay attention to other relevant articles of developeppaer!