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 deploy a SpringBoot project using Dockerfile

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

Share

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

How to deploy a SpringBoot project using Dockerfile? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Create a SpringBooot project and package it into a jar package

2. Create a folder in Linux to do docker testing

[root@izwz90lvzs7171wgdhul8az ~] # mkdir / root/docker_test

3. Upload the jar package to Linux

Create a folder where jar packages are stored

[root@izwz90lvzs7171wgdhul8az docker_test] # mkdir / root/docker_test/jar

Then upload the jar package to the folder above using XShell

4. Write Dockerfile files

# create a new image based on java image FROM java:8# author MAINTAINER Howinfun# add the jar package to the container and rename it to app.jarADD jar/app.jar / root/docker_test/app.jar# run the jar package ENTRYPOINT ["nohup", "java", "- jar", "/ root/docker_test/app.jar", "&"]

Note: ADD and COPY instructions are used in the same way, except that ADD supports extracting and decompressing archived files (tar, gzip, bzip2, etc). It is also important to note that the directory that the COPY directive needs to copy must be placed in the sibling directory of the Dockerfile file.

5. Make an image

[root@izwz90lvzs7171wgdhul8az docker_test] # docker build-t sbdemo.

Command parameters:

-t: specify a new image name

.: indicates that Dockfile is in the current path

If our Dockerfile file path is not in this directory, or if we have another file name, we can give the path to the Dockerfile file separately with the-f option

[root@izwz90lvzs7171wgdhul8az docker_test] # docker build-t sbdemo-f / root/docker_test/Dockerfile / root/docker_test/

Command parameters:

-f: the first parameter is the path of Dockerfile. The second parameter is to view the image created by us through the docker images command after the folder where Dockerfile is created:

[root@izwz90lvzs7171wgdhul8az docker_test] # docker images | grep sbdemosbdemo latest 7efac46ef997 4 hours ago 686MB

6. Start the container

[root@izwz90lvzs7171wgdhul8az docker_test] # docker run-d-p 8888Switzerland 8888-- name mysbdemo sbdemo:latest

Command parameters:

-d: running in the background

-p: expose the specified port number

-- name: name the container

After startup, you can view the running containers through docker ps:

[root@izwz90lvzs7171wgdhul8az docker_test] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5096c8c7b36f sbdemo "nohup java-jar / ro?? 4 seconds ago Up 2 seconds 0.0.0.0 purl 8888-> 8888/tcp mysbdemo

7. View the container startup log

We can view the log of the specified container through docker logs:

[root@izwz90lvzs7171wgdhul8az docker_test] # docker logs mysbdemo. _ _ _ / / _ _ _ (() _ _ _ (()\ _ _ _ |'_ | |'_ / _ _ `|\ / _ _ _) | | | (_ | |) '| _ |. _ _ | _ | | _ | _ | | _\ _ _ | | / = | _ | = | _ _ / /:: Spring Boot:: (v2.1.6.RELEASE) 2019-10-11 02 INFO 46.264 INFO 1-[main] com.hyf.DatabaseApplication: Starting DatabaseApplication v0.0.1-SNAPSHOT on 6d85ac5d8751 with PID 1 (/ root/docker_test/app.jar started by root in /) 2019-10-11 0215 DEBUG 1-- [main] com.hyf.DatabaseApplication: Running with Spring Boot v2.1.6.RELEASE Spring v5.1.8.RELEASE2019-10-11 02 INFO 10 com.hyf.DatabaseApplication 46.268 INFO 1-[main] o.m.s.mapper.ClassPathMapperScanner: Skipping MapperFactoryBean with name 'bookMapper' and' com.hyf.mapper.BookMapper' mapperInterface. Bean already defined with the same nameplate 2019-10-11 02 main main o.m.s.mapper.ClassPathMapperScanner: No MyBatis mapper was found in'[com.hyf] 'package. Please check your configuration.2019-10-11 02 INFO 10 main 49.246 Delegate 1-[main] .s.d.r.c.RepositoryConfigurationDelegate: Multiple Spring Data modules found Entering strict repository configuration modebanks 2019-10-11 02 INFO 10 Bootstrapping Spring Data repositories in DEFAULT mode.2019 49.257 INFO 1-[main] .s.d.r.c.RepositoryConfigurationDelegate: Bootstrapping Spring Data repositories in DEFAULT mode.2019-10-11 02VOL10 INFO 1-[main] .d.r.c. RepositoryConfigurationDelegate: Finished Spring Data repository scanning in 39ms. Found 0 repository interfaces.2019-10-11 02 INFO 10 main trationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$2c6b335] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-10-11 02 INFO 10 INFO 1-[main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat initialized with port (s) ): 8888 (http) 2019-10-11 02 INFO 10 o.apache.catalina.core.StandardService: Starting service [Tomcat] 2019-10-11 02 o.apache.catalina.core.StandardService 10 o.apache.catalina.core.StandardService 51.359 INFO 1-[main] org.apache.catalina.core.StandardEngine: Starting Servlet engine: [Apache Tomcat/9.0.21] 2019-10-11 02 INFO 1051.778 INFO 1-- [main] o .a.c.C. [tomcat]. [localhost]. [/]: Initializing Spring embedded WebApplicationContext2019-10-11 02 Initializing Spring embedded WebApplicationContext2019 10 o.s.web.context.ContextLoader 51.779 INFO 1-[main] o.s.web.context.ContextLoader: initialization completed in 5104 ms2019-10-11 02 o.s.web.context.ContextLoader 10 o.s.web.context.ContextLoader 54.164 Tomcat 1-[main] o.s.s.concurrent.ThreadPoolTaskExecutor: Initializing ExecutorService 'applicationTaskExecutor'2019-10-11 02:10:56. 081 INFO 1-[main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started on port (s): 8888 (http) with context path''2019-10-11 02 Gang 10 INFO 56.090 INFO 1-[main] com.hyf.DatabaseApplication: Started DatabaseApplication in 11.49 seconds (JVM running for 12.624)

8. Access interface

After the container starts, we try to use postman or other http tools to access the application interface deployed in the container.

After reading the above, have you mastered how to deploy a SpringBoot project using Dockerfile? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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