How to deploy SpringBoot project using Dockerfile

How to deploy SpringBoot project using Dockerfile

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 to store the jar package

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

Then use XShell to upload the jar package to the above folder

4. Write the Dockerfile

# Create a new image based on the java image FROM java:8
# Author: MAINTAINER Howinfun
# Add the jar package to the container and rename it to app.jar
ADD jar/app.jar /root/docker_test/app.jar
# Run jar package ENTRYPOINT ["nohup","java","-jar","/root/docker_test/app.jar","&"]

Note: The ADD and COPY instructions are used in the same way, the only difference is that ADD supports extracting and decompressing archive files (tar, gzip, bzip2, etc.). It is also important to note that the directory that the COPY instruction needs to copy must be placed in the same directory as the Dockerfile file.

5. Make a mirror

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

Command parameters:

-t: Specify the new image name
.: indicates that the Dockfile is in the current path

If our Dockerfile file path is not in this directory, or has another file name, we can give the path of the Dockerfile file separately through 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 the Dockerfile and the second parameter is the folder where the Dockerfile is located. After the creation is complete, use the docker images command to view the image we created:

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

6. Start the container

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

Command parameters:

-d: Run in the background
-p: Publicly specify the port number
--name: Give the container a name

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

[root@izwz90lvzs7171wgdhul8az docker_test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5096c8c7b36f sbdemo "nohup java -jar /ro?? 4 seconds ago Up 2 seconds 0.0.0.0:8888->8888/tcp mysbdemo

7. View container startup log

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

[root@izwz90lvzs7171wgdhul8az docker_test]# docker logs mysbdemo

 . ____ _ __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|===============|___/=/_/_/_/
 :: Spring Boot :: (v2.1.6.RELEASE)

2019-10-11 02:10: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 02:10:46.267 DEBUG 1 --- [ main] com.hyf.DatabaseApplication : Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-10-11 02:10:46.268 INFO 1 --- [ main] com.hyf.DatabaseApplication : No active profile set, falling back to default profiles: default
2019-10-11 02:10:49.139 WARN 1 --- [ main] omsmapper.ClassPathMapperScanner : Skipping MapperFactoryBean with name 'bookMapper' and 'com.hyf.mapper.BookMapper' mapperInterface. Bean already defined with the same name!
2019-10-11 02:10:49.139 WARN 1 --- [ main] omsmapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.hyf]' package. Please check your configuration.
2019-10-11 02:10:49.246 INFO 1 --- [ main] .sdrcRepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-10-11 02:10:49.257 INFO 1 --- [ main] .sdrcRepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-10-11 02:10:49.328 INFO 1 --- [ main] .sdrcRepositoryConfigurationDelegate : Finished Spring Data repository scanning in 39ms. Found 0 repository interfaces.
2019-10-11 02:10:50.345 INFO 1 --- [ 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:10:51.255 INFO 1 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8888 (http)
2019-10-11 02:10:51.359 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-11 02:10:51.359 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-10-11 02:10:51.778 INFO 1 --- [ main] oaccC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-10-11 02:10:51.779 INFO 1 --- [ main] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 5104 ms
2019-10-11 02:10:54.164 INFO 1 --- [ main] ossconcurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-11 02:10:56.081 INFO 1 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ''
2019-10-11 02:10: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 is started, we try to use postman or other http tools to access the application interface deployed in the container.

Summarize

The above is the method of using Dockerfile to deploy SpringBoot project introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile
  • How to write the best Dockerfile
  • Detailed explanation of COPY and ADD commands in Dockerfile
  • A detailed introduction to the Dockerfile image building file and related commands in Docker
  • Detailed explanation of using Dockerfile to build MySQL image and implement data initialization and permission setting
  • Detailed explanation of the specific use of the ENV instruction in Dockerfile
  • Dockerfile usage examples
  • Dockerfile to create the official Tomcat image and detailed explanation of image usage
  • Summary of common commands in Dockerfile
  • How to use Dockerfile to create a mirror of the Java runtime environment
  • How to create your own image using Dockerfile
  • Introduction to Dockerfile instructions ADD and COPY
  • Detailed explanation of multi-stage (multi-stage build) in Dockerfile
  • Docker Basics: Detailed Explanation of Dockerfile Commands
  • How to deploy nodejs service using Dockerfile
  • Dockerfile instructions explained
  • A brief introduction to the Dockerfile instruction VOLUME
  • Dockerfile simple introduction

<<:  MySQL DeadLock troubleshooting full process record

>>:  Teach you how to build a react+antd project from scratch

Recommend

11 Linux KDE applications you didn't know about

KDE Abbreviation for Kool Desktop Environment. A ...

How to use Flex layout to achieve scrolling of fixed content area in the head

The fixed layout of the page header was previousl...

Detailed explanation of using Vue.prototype in Vue

Table of contents 1. Basic Example 2. Set the sco...

Vue implements interface sliding effect

This article example shares the specific code of ...

Raspberry Pi msmtp and mutt installation and configuration tutorial

1. Install mutt sudo apt-get install mutt 2. Inst...

Web project development VUE mixing and inheritance principle

Table of contents Mixin Mixin Note (duplicate nam...

Detailed tutorial on installing nacos in docker and configuring the database

Environment Preparation Docker environment MySQL ...

HTML Tutorial: Collection of commonly used HTML tags (5)

These introduced HTML tags do not necessarily ful...

How to use VUE to call Ali Iconfont library online

Preface Many years ago, I was a newbie on the ser...

Web Design Tutorial (4): About Materials and Expressions

<br />Previous Web Design Tutorial: Web Desi...

Front-end JavaScript operation principle

Table of contents 1. What is a JavaScript engine?...

Implementation of sharing data between Docker Volume containers

What is volume? Volume means capacity in English,...

Installation and use tutorial of Elasticsearch tool cerebro

Cerebro is an evolution of the Elasticsearch Kopf...

VMware WorkStation 14 pro installation Ubuntu 17.04 tutorial

This article records the specific method of insta...