Location>code7788 >text

SpringBoot + Docker + Nginx Deploying Front and Backend Projects

Popularity:158 ℃/2024-09-29 09:42:38

Deploying SpringBoot Projects (Passbook)

I. Overview

utilizationjava -jar Commands to deploy the project's JAR package directly and using Docker to make an image for deployment are two common deployment methods. The following is an overview and brief analysis of the advantages and disadvantages of these two approaches:

1.1. Usejava -jar command directly deploys the project's JAR package

Overview:

  • pass (a bill or inspection etc)java -jar Deploying a project's JAR package directly is one of the easiest and most straightforward ways to deploy. It directly utilizes the features of Java and requires no additional container technical support.
  • You can use the command linejava -jar Launch a JAR file so that you can run your Java application.

Advantage:

  • Simple and direct: directly using the JVM to run JAR packages , no need to rely on other container technology , simple deployment .
  • Scope of application: For simple, single application deployments.

Disadvantage:

  • Poor resilience and isolation: runs directly on the host, has no resource isolation and may be affected by the host environment.
  • Difficult to manage: Difficult to control different versions of the application, no flexibility to manage multiple instances.

1.2. Use Docker to make images for deployment

Overview:

  • Using Docker you can package applications, runtime environments, dependencies, etc. into a single image and deploy them consistently across different environments.
  • Docker makes it easier to manage the deployment and running of applications, as well as to realize multi-instance deployment and horizontal scaling.

Advantage:

  • Flexible Deployment: Containerized applications are easier to deploy and run in different environments.
  • Resource Isolation: Using containers allows for resource isolation and better application security.
  • Consistency: Docker images provide a repeatable, consistent runtime environment that solves the "it's fine on my machine" problem.

Disadvantage:

  • Learning curve: the introduction of Docker adds some learning and adaptation costs.
  • Operation and maintenance complexity: You need to consider the operation, monitoring, network configuration and other aspects of the container, adding a certain degree of operation and maintenance complexity.

In summary, there are advantages and disadvantages to both deployment methods. The advantages of usingjava -jar Direct deployment is simple and straightforward, and is suitable for simple, single-application deployments; while using Docker to make images for deployment is more flexible and easy to achieve consistent deployment and resource isolation, but requires consideration of the learning curve and O&M complexity.

Second, use java -jar to run the jar package

The advantages of using this deployment method are: simple, fast

2.1 Downloading and installing the Java JDK on CentOS 7 17

2.1.1. Operational steps

  1. Use wget to download Java JDK 17 to the/usr/local/java installation directory and unzip
## Enter the installation directory
cd /usr/local/
## Create a new Java installation directory and go to it
mkdir java
cd java/
## Download the JDK
wget /java/17/latest/jdk-17_linux-x64_bin.
## Extract the installation package to the java directory
tar -zxvf /root/jdk-17_linux-x64_bin.
## Enter the directory
cd jdk-17.0.4.1/

2. Configure environment variables

## Configure environment variables
vim /etc/profile

Add the following to the end of the file

## set java environment
JAVA_HOME=/usr/local/java/jdk-17.0.4.1
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME PATH

3. Reload the file to make it effective

## Reload the profile
source /etc/profile

4. At this point, the installation of Java JDK 17 is complete, verify the version of Java

## View Java version
java -version

2.2. Operational projects

It's easy to start this one, just find the uploaded jar package and execute the command

java -jar 

Third, the use of docker to create images to deploy their own project jar package

step by step

3.1 Install docke

3.1.1. Pre-installation preparations

  • Check the server system version and kernel version
cat /etc/redhat-release

# Check the server kernel version
uname -r

We are using CentOS 7.4 with kernel version 3.10.

  • Installation of dependency packages
 yum install -y yum-utils device-mapper-persistent-data lvm2
  • Setting up an AliCloud Mirror Source
yum-config-manager --add-repo /docker-ce/linux/centos/

3.2 Install Docker

3.2.1 Docker version description

Docker has been divided into two editions since version 17.03: Community Edition (abbreviated as CE) and Enterprise Edition (abbreviated as EE).
The Enterprise Edition includes some paid services that are generally not used by individual developers, so we just need to install the Community Edition version of docker-ce.

3.2.2 docker-ce installation

yum install -y docker-ce

3.2.3. Activate the docker and set up bootup.

#The command to start docker
systemctl start docker
#Set the boot command
systemctl enable docker
#View docker version
docker version

3.3 Configure Aliyun Mirror Source

The docker image source address is foreign by default, the network speed is slow, and if you do not configure it, you can also pull the time will report an error

3.3.1, first of all need to create the account of Aliyun (free registration), Aliyun official website address.

3.3.2, Search container image acceleration

3.3.3, find your own mirror address

3.3.4 Configuring mirrors in Lunix

Path: /etc/docker/

If not, just create a new file

{
  "registry-mirrors": [""]
}

3.3.5, restart save

# Restart docker
sudo systemctl daemon-reload
sudo systemctl restart docker

3.3.6, Viewing results

Here's your mirrored acceleration address for AliCloud

IV. Creating a dockerFile

eip This can be changed to the name of your project, but the commands that follow require a series of changes.

# Use OpenJDK 17 as the base image
FROM openjdk:17

# Author
MAINTAINER admin

# Create a temporary folder in the container
VOLUME /tmp

# Copy the local jar file to the container and rename it to
COPY eip-0.0.

# Update the modification time of the file
RUN bash -c 'touch /'

# Define the command to run when the container starts
ENTRYPOINT ["java","-=file:/dev/. /urandom","-jar","/"]

V. Building mirrors

Build an eip image from the dockerfile in that directory.

Here the jar package and dockerfile, must be in the same directoryI put it in my home directory, and eip is a new folder I created.

 # Build the image
 docker build -t eip .

 # View the command, and the built image will appear
 docker images

Results:

VI. Configuring docker network

so that containers on the same network within the host can communicate with each other

# Create network name: eip_net
docker network create --driver bridge eip_net

# Find the name or ID of the network
docker network ls

#delete the network name (this doesn't need to be executed, it's just provided here)
docker network rm your_network

Results:

Seven, according to the image to run the container

# where eipcontainer is the container name eip_net is the network name eip is the image name (just generated above)

docker run -d --name eipcontainer --network eip_net -p 8069:8069 eip

# Check the command
docker ps

Result: Here I am generating the container twice, and the container name has been changed to eipcontainer == "eip eipcontainer (as in name).

Getting here is a success:Manually use the dockerfile to image your own jar package inside docker and run the container

At this time we can also access the interface address, but only if the project has its own integration documentation

Example: IP+Port/

Comprehensive Scripts

If you feel that the above deployment is too troublesome, here to provide a startup script, so that each modified code only need to re-upload the jar and re-execute the script is the latest code, without the above complex process.

Prerequisites: The files must be in the same directory and the Dockerfile and jar packages must have been uploaded.


1.

New Script

echo "eip startup"

echo "Deleting containers and images"
docker stop $1
docker rm $1
docker rmi $(docker images | awk '/^<none>/ { print $2 }')


echo "Building an image"
docker build -t eip .

echo "Building containers from images"
docker run -d --name eip --network eip_net -p 8069:8069 eip

echo "eip startup success"

Execute script files with one click

# Execute the command eip for $1 of the script 8069 for $2 of the script similarly passed as a parameter
sh eip 8069

The reason I didn't bother to pull the code from the repository here is because of the rapid deployment, which is also a point that can be optimized. If you want to pull code, one-click packaging, automatic deployment, you can refer to the following article:

Article address:

High-end operation: repository + Jenkins + maven + docker + K8S deployment, this is another knowledge point!

Other commands

# Delete an image
docker rmi Image ID
# Delete container
docker rm Container ID

# View image
docker images
# View running containers
docker ps

#Live view 100 lines
docker logs -f your_container | tail -n 100
#Live view
docker logs -f your_container

forward part of sth.

Installation of Nginx can be done by Baidu

Path: home/eipWeb/nginx

# Upload the dist package
npm run build:stage

Launch scripts with one click:

# Stop and delete the container
docker stop eip_ui
echo "Stopped container successfully !"

#
docker rm -f eip_ui
echo "Deleting container succeeded !" # docker rm -f eip_ui

# Run the container and hang
docker run -d -p 7069:80 --name eip_ui -v /home/eipWeb/nginx/dist:/usr/share/nginx/html --restart=always /acs-sample/nginx

echo "Running the startup container successfully !"

Feel free to point out anything wrong with the article in the comments section at the end!!!!
If you feel it would help youkudosorfocusA little bit of it!!!