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 Docker deploys SpringBoot applications

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

Share

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

This article mainly shows you "Docker how to deploy SpringBoot applications", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Docker how to deploy SpringBoot applications" this article.

Preface

When deploying a project, you may need to rely on node.js, Redis, RabbitMQ, MySQL, and so on. These services require different libraries, dependencies, and even conflicts. It brings great difficulties to deployment. However, Docker does solve these problems cleverly. In order to solve the problem of compatibility of dependencies, Docker uses two means:

Package application Libs (function library), Deps (dependency), configuration and application together

Run each application in an isolated container to avoid interference with each other

This article explains how to use Docker to deploy projects that we normally develop with SpringBoot:

Dockerfile, what is Dockerfile?

Dockerfile is a text file used to build an image. The text contains instructions and instructions needed to build the image.

Dockerfile syntax

When building a custom image, you don't need to copy and package files one by one.

We just need to tell Docker about the composition of our image, what BaseImage we need, what files we need to copy, what dependencies we need to install, and what the startup script is, and Docker will help us build the image in the future.

The file that describes the above information is the Dockerfile file.

A Dockerfile is a text file that contains instructions (Instruction) indicating what to do to build the image. Each instruction forms a layer of Layer.

Package the SpringBoot project

Prepare the project:

Project port

Server.port=8080

Create a new index.html under the templates folder

Docker deploys SpringBoot applications Docker deploys SpringBoot applications

Define the home page Controller and jump to index

@ Controllerpublic class indexController {@ RequestMapping ("/") public String index () {return "index";}}

Preview the effect locally:

Package SpringBoot applications locally into jar

Prepare the maven-compiler-plugin plug-in

App org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8

Maven is a project management tool. If you don't tell it which jdk version of the code to use to compile, it will use the default jdk version of maven-compiler-plugin to deal with it, so it is prone to version mismatch, which may lead to compilation failure. For example, if the code uses the new features of Java8, such as functional programming, but maven compiles with Java7, it is impossible to compile this code into a .class file. To avoid this, when building a maven project, I habitually configure the maven-compiler-plugin plug-in, specifying the jdk version of the project's source code, the compiled jdk version, and how it is encoded.

Then execute clean, and finally execute package to package the project.

The appearance of BUILD SUCCESS indicates that the package has been successfully packed.

After the package is successful, the target folder will appear, and there will be the jar package just packaged in the directory.

Prepare jar packages and Dockerfile

Copy jar to your desktop or custom location, create and write Dockerfile files locally (note: the name must be Dockerfile, remove the suffix name)

Write Dockerfile

Dockerfile instruction

FROM java:8-alpineCOPY. / app.jar / tmp/app.jarENTRYPOINT java-jar / tmp/app.jar

Dockerfile description

FROM java:8-alpine:

Build the image based on java8. By default, you need to install the configuration environment variable to build the java image, and the java:8-alpine image has already done all the previous steps.

COPY. / app.jar / tmp/app.jar

Copy the jar package

ENTRYPOINT java-jar / tmp/app.jar

Entry command

Use the tool to upload jar and Dockerfile to the server

Upload location depends on the individual, my storage location: / tmp/docker

Cd tmpmkdir docker

Choose to upload a file

Upload to / tmp/docker directory

Build an image

Enter the directory where the file is uploaded: cd / tmp/docker, and type ls to view the file

Enter a command to build an image

Docker build-t test:1.0.

Format: docker build-t image name: the directory path where the version number Dockerfile is located

Command description:

Secondary command of build:docker

Build an image

-t

-name: general image name format: name: version number (test:1.0)

.

The directory where Dockerfile is located, because we have entered the directory where Dockerfile is located. Represents the current directory

You can see that when building the image, it will be divided into three steps according to the three commands we wrote Dockerfile.

View Mirror

View the mirror command in Docker:

Docker images

You can see our custom image test

Create and run the container

Run the Docker mirror command:

Docker run-name springboot-p 8080R 8080-d test:1.0

Command description:

Docker run: docker secondary command

Create and run the container

-- name

Define the container name, followed by the container name (springboot)

-p port mapping

The port on the left is the port of Linux, and the port number of the container on the right

Map the linux port to the container port number, so that accessing the linux port is equal to accessing the container port number

-d

Run the container in the background, otherwise the container will stop running after exiting

Test:1.0- > Mirror name

Select the 1.0 version of the custom image test to create a container

Create and run the container successfully!

View running images

Running mirroring command

Docker ps

The browser accesses port 8080 of the server

Mobile access:

View the log

View Container Log command

Docker logs-f springboot

Description:

Check the container log but not update it in real time. If you want to view a new log, you need to run it again.

Docker logs + Container name

Update log in real time

Docker logs-f + Container name

These are all the contents of the article "how Docker deploys SpringBoot applications". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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