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 to use Docker deployment to package and publish springboot projects

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

Share

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

This article will explain in detail how to use Docker deployment package to release springboot project. 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.

Preface

From the installation of docker to a variety of ways to package and release, compile, image, container and other problems, encounter a variety of problems, and finally live up to expectations, step by step to solve, this article I will compare a large span to explain all the solutions.

First: environment

1. The centos7 installed by the virtual machine.

2.docker has been installed. No, you can refer to my docker installation article.

three. Get a very simple springboot project, just get the release and compilation done first.

Second: start describing the process from the project to the docker release:

1.springboot project code:

Hierarchical diagram:

Haha, just ignore the red fork, my environmental problems, it doesn't matter.

Code: pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.0.BUILD-SNAPSHOT com.king springdocker 0.0.1-SNAPSHOT springdocker Demo project for Spring Boot UTF-8 UTF-8 springdemo 1.8 org.springframework.boot spring-boot -starter-web org.springframework.boot spring-boot-starter-tomcat provided org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test Org.junit.vintage junit-vintage-engine junit junit org.springframework.boot spring-boot-maven-plugin Com.spotify docker-maven-plugin 0.4.3 ${docker.image.prefix} / ${project.artifactId} src/main/docker / ${project.build.directory} ${project.build.finalName} .jar spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone spring-snapshots Spring Snapshots https://repo. Spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone

2:Dockerfile file

FROM java:8VOLUME / tmpADD springdocker-0.0.1-SNAPSHOT.jar app.jarRUN sh-c 'touch / app.jar'ENV JAVA_OPTS= "" ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"]

The document explains:

VOLUME specifies the temporary file directory as / tmp. The effect is to create a temporary file in the host / var/lib/docker directory and link to the container's / tmp. The steps are optional.

It is necessary if the application of the file system is involved. The / tmp directory is used to persist to the Docker data folder because the embedded Tomcat container used by Spring Boot uses / tmp as the working directory by default

The project's jar file is added as "app.jar" to the container's

ENTRYPOINT executes the project app.jar. To shorten the Tomcat startup time, add a system property pointing to "/ dev/urandom" as Entropy Source

3: rest of the code:

Package com.king.springdocker.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping (value= "/ demo") public class helloController {@ RequestMapping (value= "/ hello") public String hello () {return "hello king!";}} = applicationProperties=server.port=80server.servlet.context-path=/king==package com.king.springdocker;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication @ SpringBootApplicationpublic class SpringdockerApplication {public static void main (String [] args) {SpringApplication.run (SpringdockerApplication.class, args);} =

Third:

1.xhell goes to the server. I create a folder king under / home/, package the project locally with development tools, and put the jar package and Dockerfile into the king directory. As shown in the figure:

two。 Execute the following command:

Docker build-t hellodocker.

Command explanation: hellodocker name as you like, image name from itself,-t represents the image tag to be built.

Don't forget the dot, which means that the dot is in the current directory, where the Dockerfile file is now located.

[root@localhost king] # docker build-t hellodocker .Sending build context to Docker daemon 17.36MBStep 1 build context to Docker daemon 17.36MBStep 6: FROM java:88: Pulling from library/java5040bd298390: Pull complete fce5728aad85: Pull complete 76610ec20bf5: Pull complete 60170fec2151: Pull complete e98f73de8f0d: Pull complete 11f7af24ed9c: Pull complete bb9cdec9c7f3: Pull complete Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9dStatus: Downloaded newer image for java:8-- > d23bdf5b1b1bStep 2 Pull complete bb9cdec9c7f3 6: VOLUME / tmp-- > Running in a6293dd37995Removing intermediate container a6293dd37995-- > 08f10f02092eStep 3 Pull complete e98f73de8f0d 6: ADD springdocker-0.0.1- SNAPSHOT.jar app.jar-> c4eaf809b297Step 4 touch 6: RUN sh-c 'touch / app.jar'-- > Running in 6c8a30b4220fRemoving intermediate container 6c8a30b4220f-- > dba5ee5d2d27Step 5 e1982350619dStep 6: ENV JAVA_OPTS= "- > Running in 25f5b705eb27Removing intermediate container 25f5b705eb27-- > e1982350619dStep 6 e1982350619dStep 6: ENTRYPOINT [" java " "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"]-- > Running in f98b4cc2f63bRemoving intermediate container f98b4cc2f63b-- > 1d881201c9c5Successfully built 1d881201c9c5Successfully tagged hellodocker:latest

3.docker images, see the following figure:

Hellodocker appears successfully. Now start execution, open the browser, ip port address access, hey hey, no, oh, let's see what's going on.

I found that I opened port 80, so it is easy to copy all kinds of online tutorial examples, so it is better to think about them yourself.

Change it to: docker run-d-p 80:80 hellodocker.

Well, revisit the address, all right, done, print successfully: hello king!

Fourth: a collection of all kinds of mistakes and lessons.

Before looking for information on the Internet, this blog, that blog, as a result, it was either this kind or that kind of mistake, ah, there was no one else to blame, who could say in that detail? To sum up:

Error 1: the Dockerfile file is:

FROM frolvlad/alpine-oraclejdk8:slimVOLUME / tmpADD springdocker-0.0.1-SNAPSHOT.jar app.jarRUN sh-c 'touch / app.jar'ENV JAVA_OPTS= "" ENTRYPOINT ["sh", "- c", "java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / app.jar"]

The error is as follows:

Sending build context to Docker daemon 17.36MB

Step 1/6: FROM frolvlad/alpine-oraclejdk8:slim

Pull access denied for frolvlad/alpine-oraclejdk8, repository does not exist or may require 'docker login'

It is also speechless. FROM has been changed a lot, and it is also done with various mirrors and configurations. at last, it is changed to: FROM java:8, done. This, take a look at the introduction of the docker command to understand the usage:

Docker concept

1. Docker: it was originally a set of container management tools produced by dotCloud, but then Docker became so popular that even the name of the company was changed from dotCloud to Docker.

2. Dockerfile: it is the description file of Docker image, which can be understood as A, B, C, D of rocket launch. The steps of.

3. Docker image: it is made through Dockerfile and contains the basic files of the operating system and the software running environment. It uses hierarchical storage mode.

4. Docker container: it is a running image. To put it simply, a Docker image is equivalent to a program, and a container is equivalent to a process.

Dockerfile instruction

Dockerfile consists of multiple instructions. Each instruction executes the corresponding program to perform certain functions when compiling the image. It consists of instructions + parameters, separated by commas, and # as the starting character of comments. Although instructions are not case-sensitive, general instructions use uppercase, and parameters use lowercase.

Error 2:

Before also used to put the whole project on the server, and then use the command: mvn package docker:build, to package and compile, the result is an error, in fact, it is also due to the wrong source of Dockerfile from. The error is as follows:

This is the end of this article on "how to deploy and package a springboot project using Docker". 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