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 SpringBoot uses Docker to rapidly deploy projects

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how SpringBoot uses Docker to quickly deploy projects. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Brief introduction

First of all, let's find out what Docker is.

Docker is a kind of encapsulation of Linux container, which provides an easy-to-use interface to the container. It is currently the most popular Linux container solution.

Docker packages the application and its dependencies in a file. Running this file will generate a virtual container. The program runs in this virtual container as if it were running on a real physical machine. With Docker, you don't have to worry about the environment.

Generally speaking, the interface of Docker is quite simple, and users can easily create and use containers and put their own applications into containers. Containers can also be versioned, copied, shared, and modified, just like managing ordinary code.

What is the use of Docker?

Provide an one-time environment, provide flexible cloud services to build a micro-service architecture

How to build Docker environment

Install using yum

# check your current kernel version uname-r # install Dockeryum-y install docker# to start Docker backend service service docker start# test run hello-world. Since there is no hello-world image locally, an image of hello-world will be downloaded and run in the container. Docker run hello-world

Script installation:

# make sure that the yum package is updated to the latest sudo yum update# to execute the Docker installation script, which adds the docker.repo source and installs Docker. Curl-fsSL https://get.docker.com/ | sh# starts the Docker process sudo service docker start# verifies whether docker is installed successfully and executes a test image sudo docker run hello-world in the container

Because the speed of downloading images in China is slow, it is recommended to replace them with domestic mirror sources.

Execute a command

Vim / etc/docker/daemon.json adds the following code {"registry-mirrors": ["]}

two。 Create a project

First of all, let's create a SpringBoot project, this step is not repeated, you can use the IDEA build or visit the SpringBoot project to build the project.

Complete project structure:

Boot-docker ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── example │ │ └── bootdocker │ │ └── BootDockerApplication.java │ └── resources application.properties static templates

Then create a controller, which is not too complicated here, but mainly to demonstrate how Docker deploys the project.

Main code:

Start and open the browser provider address:

The project is complete and deployment to Docker is now under way.

3. Deploy the project

3.1 use Dockerfile

We need to know what Dockerfile is before we deploy the project.

Dockfile is a script interpreted by a Docker program. Dockerfile consists of one instruction, each corresponding to a command under the Linux. The Docker program translates these Dockerfile instructions into real Linux commands. Dockerfile has its own writing format and supported commands, and the Docker program solves the dependencies between these commands, similar to Makefile. The Docker program will read the Dockerfile and generate a custom image according to the instructions. An obvious script like Dockerfile is more acceptable to users than a black box like image, which clearly shows how image is created. With Dockerfile, when we need to customize our own additional requirements, we just need to add or modify instructions on Dockerfile to regenerate image, saving us the trouble of typing commands. Referenc

Let's first package the project for execution and execution.

Mvn package

Create a Dockerfile file

Touch Dockerfile

Add the following information to Dockerfile:

Note that Dockerfile must be in the same path as the Jar package

FROM java:8EXPOSE 8080VOLUME / slmADD boot-docker-0.0.1-SNAPSHOT.jar boot-docker.jarRUN sh-c 'touch / boot-docker.jar'ENV JAVA_OPTS= "" ENTRYPOINT ["sh", "- c", "java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / boot-docker.jar"]

FROM basic image is necessary, which means your project will be built on this basis. EXPOSE allows the specified port to forward VOLUME to create a mount point that can be mounted from the local host or other containers, which is generally used to store databases and data to be maintained. ADD files from the path ENV can be used to set the environment variable ENTRYPOINT for the docker container to specify the command or file to be executed when Docker image runs as instance (that is, Docker container). Both CMD and ENTRYPOINT can be used to specify the program to start running, and both commands have two unused syntax:

CMD ["ls",''- l "] CMD ls-l

Start building:

Docker build-t boot-docker.

-t boot-docker represents the name you want to build

Enter more parameters to see: docker build-- help

Console output:

Sending build context to Docker daemon 16.81MBStep 1 Using cache--- 7: FROM java:8--- > d23bdf5b1b1bStep 2 Acer 7: EXPOSE 8080 Merry-> Using cache--- > b2445bf62da8Step 3 Acer 7: VOLUME / slm--- > Using cache--- > b73d0b73b868Step 4 Lexus 7: ADD boot-docker-0.0.1-SNAPSHOT.jar boot-docker.jar--- > Using cache--- > 2b4868aafca9Step 5 Raza 7: RUN sh-c 'touch / boot-docker.jar'--- > Using cache--- > 816b59f199afStep 6 / 7: ENV JAVA_OPTS= "--> Using cache--- > 784f033b9dd6Step 7 sh 7: ENTRYPOINT [" sh " "- c", "java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / boot-docker.jar"]-> Using cache--- > 92a0da91ea19Successfully built 92a0da91ea19Successfully tagged bootdocker:latest

We can see that the build has been completed, and the sentence Successfully built 92a0da91ea19 indicates the mirror ID that we just built. We can now operate on this ID.

Enter the run command to start.

Docker run-d-p 8080 92a0da91ea19

Console output:

Root@izz30yg92yl9i3z / # docker run-d-p 8080 92a0da91ea1962b837ac75e3d83a4be2d7b0f6edee5ff70c69a98bac4ff74c7ed6d3e70282eeroot@izz30yg92yl9i3z / #

-d means running in the background

-p mapping port

The access interface has been run successfully. Note that the IP plus port of the host is accessed because it is mapped to the host port.

Enter more parameters to see: docker build-- help

3.2 build using Maven

It said above to use Dockerfile to build, now use Maven to build

Add docker to pom to build dependencies

Com.spotify docker-maven-plugin 0.4.13 ${docker.image.prefix} / ${project.artifactId} / slm/ / ${project.build.directory} ${project.build.finalName} .jar

Note that dockerDirectory still needs to set the path to the Dockerfile file.

Copy the project to the Linux host and execute the decompression command

Unzip boot-docker.zipcd boot-docker

Execute the command:

Mvn package docker:build

Console output:

[INFO] Building image boot-docker/boot-dockerStep 1/7: FROM java:8

-- > d23bdf5b1b1bStep 2 Compact 7: EXPOSE 8080

-> Using cache--- > b2445bf62da8Step 3amp 7: VOLUME / slm

-> Using cache--- > b73d0b73b868Step 4amp 7: ADD boot-docker-0.0.1-SNAPSHOT.jar boot-docker.jar

-- > Using cache--- > 2b4868aafca9Step 5 touch 7: RUN sh-c 'touch / boot-docker.jar'

-> Using cache--- > 816b59f199afStep 6Unip 7: ENV JAVA_OPTS= ""

-- > Using cache--- > 784f033b9dd6Step 7 jar 7: ENTRYPOINT ["sh", "- c", "java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / boot-docker.jar"]

-> Using cache--- > 92a0da91ea19ProgressMessage {id=null, status=null, stream=null, error=null, progress=null ProgressDetail=null} Successfully built 92a0da91ea19Successfully tagged boot-docker/boot-docker:latest [INFO] Built boot-docker/boot-docker [INFO]-[INFO] BUILD SUCCESS [INFO]- -[INFO] Total time: 19.908s [INFO] Finished at: Wed Jul 10 16:00:21 CST 2019 [INFO] Final Memory: 35M/86M [INFO]-

Same as above. Start

This is the end of the article on "how SpringBoot uses Docker to deploy the project". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report