The spring boot application realizes the process of building, running and publishing through docker

1. Docker introduction

Docker is an open source application container engine, which allows developers to package their applications and dependency packages into a portable container, and then publish them to any popular Linux machine, or realize virtualization. Containers are completely sandboxed, and there will be no interface between them. Docker image is a scheme for running containerized processes. In this article, we will build a simple spring boot application.

2. Environmental construction

JDK 1.8+ Maven 3.0+

Docker latest version.

3. Build the project with maven

3.1 create directory structure

In Linux or MAC system.

3.2 create POM XML file

be careful:

The spring boot Maven plugin provides many convenient functions:

1) It collects all jar files on the classpath and constructs a single, runnable jar, which makes it easier to execute and transfer services.

2) It searches the public static void main() method of the to mark the class as runnable.

3) It provides a built-in dependency parser to set the version number to match the dependency of spring boot. You can overwrite any version you want, but it will default

The version set of the selected boot.

Spotify's docker Maven plugin is used to build Maven's docker image

1) Imagename specifies the name of the image. This example is springio / Lidong spring boot demo

2) The dockerdirectory specifies the location of the dockerfile

3) Resources refer to those files that need to be put together with dockerfile and used when building images. Generally, application jar packages need to be included.

4. Write the first spring boot application

Write a simple spring boot application:

src/main/java/com/lidong/demo/SampleController. java:

Class is identified by @ springbootapplication @ restcontroller, and spring MVC can be used to process web requests@ Requestmapping maps / to home() and responds with the text "Hello docker world". The main () method uses springapplication. Com of spring boot Run () method to start the application.

5. Operation procedure

5.1 using Maven command

mvn package

function:

Access project

If the program runs correctly, the browser accesses http://localhost:8081/ , you can see the page "Hello docker world." word.

Next, visit in the browser http://localhost:8081/ , you can see the page "Hello docker world." word.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>