1 新建SpringBoot项目
1.1 项目依赖(pom.xml)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gp6</groupId>
<artifactId>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<!--
说明: Spring boot的项目必须要将parent设置为spring boot的parent,其中包含大量默认的配置
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<!--添加web支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2 项目启动类(HelloApplication)
package com.gp6;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 测试Docker部署SpringBoot
*
* @author gp6
* @date 2020/1/8
*/
@Controller
@SpringBootApplication
@Configuration
public class HelloApplication {
@RequestMapping("hello")
@ResponseBody
public String hello() {
return "hello world!";
}
public static void main(String[] args) {
// 运行的类必须包含@SpringBootApplication
SpringApplication.run(HelloApplication.class, args);
}
}
1.3 启动
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
2020-01-09 09:43:32.120 INFO 6388 --- [ main] com.gp6.HelloApplication : Starting HelloApplication on GP6 with PID 6388 (D:\Study\SpringBoot\target\classes started by GP6 in D:\Study\SpringBoot)
2020-01-09 09:43:32.127 INFO 6388 --- [ main] com.gp6.HelloApplication : No active profile set, falling back to default profiles: default
2020-01-09 09:43:36.693 INFO 6388 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-01-09 09:43:36.775 INFO 6388 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-09 09:43:36.775 INFO 6388 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
2020-01-09 09:43:36.787 INFO 6388 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\SoftWare\Work\Java\jdk1.8.0_161\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Windows\System32\OpenSSH\;D:\SoftWare\Work\Version\Git\cmd;D:\SoftWare\Work\SQL\MySQL5.5\bin;D:\SoftWare\Work\SQL\MySQL\bin;"%M2_HOME%\bin;";%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;D:\SoftWare\Work\Apache\Apache_Maven_3.2.3\bin;;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\wzhx\AppData\Local\Microsoft\WindowsApps;;.]
2020-01-09 09:43:37.086 INFO 6388 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-09 09:43:37.087 INFO 6388 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4664 ms
2020-01-09 09:43:37.167 INFO 6388 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2020-01-09 09:43:37.175 INFO 6388 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-01-09 09:43:37.175 INFO 6388 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-01-09 09:43:37.176 INFO 6388 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
2020-01-09 09:43:37.176 INFO 6388 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2020-01-09 09:43:37.555 INFO 6388 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-09 09:43:37.933 INFO 6388 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-09 09:43:37.941 INFO 6388 --- [ main] com.gp6.HelloApplication : Started HelloApplication in 8.239 seconds (JVM running for 10.347)
1.4 测试
1.5 将项目打包
2 Docker部署SpringBoot项目
2.1 编辑 Dockerfile
# 切换到该目录
[root@localhost boot]# pwd
/usr/local/gp6/boot
# 编辑 Dockerfile
[root@localhost boot]# vi Dockerfile
FROM openjdk:8
MAINTAINER gp6 <gp6@test.com>
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为docker-boot.jar
ADD SpringBoot-1.0-SNAPSHOT.jar docker-boot.jar
# 运行jar包
RUN bash -c 'touch /docker-boot.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-boot.jar"]
2.2 将SpringBoot-1.0-SNAPSHOT.jar包传入/usr/local/gp6/boot
2.3 制作镜像
[root@localhost boot]# docker build -t docker-boot .
Sending build context to Docker daemon 16.62MB
Step 1/6 : FROM openjdk:8
---> f8146facf376
Step 2/6 : MAINTAINER gp6 <gp6@test.com>
---> Using cache
---> e484406249b5
Step 3/6 : VOLUME /tmp
---> Using cache
---> 58957d5a13b8
Step 4/6 : ADD SpringBoot-1.0-SNAPSHOT.jar docker-boot.jar
---> Using cache
---> 15cc5f40453a
Step 5/6 : RUN bash -c 'touch /docker-boot.jar'
---> Using cache
---> cddb3c20c4c3
Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-boot.jar"]
---> Using cache
---> 74332039ad49
Successfully built 74332039ad49
Successfully tagged docker-boot:latest
2.4 启动容器
[root@localhost ~]# docker run -di --name dockerBoot -p 8080:8080 docker-boot
3bdd2dcd86000ed1f335cdbbca3883f502f59204135d7b6b7b2d797b233f10ae
2.5 宿主机测试
# 宿主机测试
[root@localhost ~]# curl 127.0.0.1:8080/hello
hello world!
2.6 浏览器测试
http://宿主机IP:8080/hello