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

Example Analysis of Docker containerized spring boot Application

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you the "Docker containerized spring boot application example analysis", 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 containerized spring boot application example analysis" this article.

Precondition

Environment required for containerized spring boot applications:

Jdk 1.8 +

Maven 3.0 +

Our requirement is to use maven to package the spring boot application into a docker image and upload it to docker hub. On other machines, you can directly docker pull and run the container.

Create a spring boot application

The structure of the spring boot package is:

└── src └── main └── java └── me └── ithakar

Create the spring boot Application main class, src/main/java/me/ithakar/Application.java

Package me.ithakar;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class Application {@ RequestMapping ("/") public String home () {return "docker app running";} public static void main (String [] args) {SpringApplication.run (Application.class, args);}}

Pom.xml:

4.0.0 me.ithakar docker-app 0.1.0 org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

Package and start the application

Mvn package & & java-jar target/docker-app-0.1.0.jar

Visit http://localhost:8080/ to get a response:

Docker app running

Then the spring boot application starts successfully

Create a Dockerfile file

Dockerfile is a text file, similar to a shell script, that contains instructions one by one. The content of each instruction is to describe how the corresponding mirror layer should be constructed.

Dockerfile location: src/main/docker/Dockerfile

FROM frolvlad/alpine-oraclejdk8:slim VOLUME / tmp ADD docker-app-0.1.0.jar app.jar RUN sh-c 'touch / app.jar' ENV JAVA_OPTS= "" ENTRYPOINT ["sh", "- c", "java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / app.jar"]

The FROM command specifies the base image, based on one image, and customizes it. Our basic image here is frolvlad/alpine-oraclejdk8:slim.

VOLUME defines anonymous volumes. For applications that may need to manipulate database classes and save dynamic data, the data file should be saved in the volume of the main clause, not in the storage layer of the container. Here we mount it in the / tmp location, thus ensuring that the container is stateless.

The ADD command is a more advanced copy command that copies the jar package to / app.jar

Commands executed by default after the ENTRYPOINT container is run

Add a maven docker plug-in

The docker maven plug-in is a maven plug-in developed by spotify for the convenience of java developers. Add the following code to the pom.xml file.

Ithaka com.spotify docker-maven-plugin 0.4.11 ${docker.image.prefix} / ${project.artifactId}: ${project.version} src/main/docker true / ${project.build.directory} ${project.build.finalName} .jar

Note that the ithaka in docker.image.prefix is my docker hub account. When you use it, replace it with your own.

Use the maven command to package the application, build a docker image, and upload the image to docker hub:

Mvn package docker:build-DpushImage

At this point we will get an error:

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4.11:build (default-cli) on project docker-app: Exception caught: denied: requested access to the resource is denied

This is because when we push in maven, we do not include the user information of docker, so the permission verification fails.

The solution is to add docker hub server information to the maven global setting.xml configuration

Docker-hub ithaka password mailbox

In the docker-maven-plugi plug-in in pom.xml, specify server

Com.spotify docker-maven-plugin docker-hub

And then continue to execute

Mvn package docker:build-DpushImage

Appear

The push refers to a repository [docker.io/ithaka/docker-app]

8c02854cd300: Pushed

A03c3b5dbe04: Pushed

8018d52798bb: Pushed

1e6468b956ae: Pushed

3fb66f713c9f: Pushed

0.0.1: digest: sha256:63456345745755577c19103dcf4838894e34d45a68a1ec23b2f69d6f059 size: 1375

That is, the docker hub was uploaded successfully.

Start the docker container

On another machine, execute the command

Docker run-p 8080 8080-t ithaka/docker-app

Visit http://ip:8080/ to get a response:

Docker app running

Docker application started successfully

The above is all the contents of the article "sample Analysis of Docker containerized spring boot 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

Servers

Wechat

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

12
Report