温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Docker部署Spring Boot项目

发布时间:2021-12-29 10:25:39 来源:亿速云 阅读:153 作者:小新 栏目:开发技术

这篇文章主要介绍如何使用Docker部署Spring Boot项目,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创建一个简单的springboot项目

一、在 pom.xml 中 ,使用 Spring Boot 2.2.10 相关依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.10.RELEASE</version>
</parent>

二、添加 web 和测试依赖

<dependencies>
     <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>

三、创建一个 DockerController,在其中有一个hello()方法,访问时返回:hello,nihao

@RestController
public class DockerController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "hello,nihao";
    }
}

四、启动类

@SpringBootApplication
public class DockerApplication {

 public static void main(String[] args) {
  SpringApplication.run(DockerApplication.class, args);
 }
}

添加完毕后启动项目,启动成功后浏览器访问:http://localhost:8080/hello,页面返回:hello,nihao,说明 Spring Boot 项目配置正常。

使用 Docker 部署 Spring Boot 项目

一、将项目打成jar包,拷贝到服务器上,测试一下

[root@jiangwang springbootDemo]# ls
demo-0.0.1-SNAPSHOT.jar  Dockerfile
[root@jiangwang springbootDemo]# java -jar demo-0.0.1-SNAPSHOT.jar 

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

2021-03-18 14:49:18.241  INFO 12886 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT on jiangwang with PID 12886 (/home/springbootDemo/demo-0.0.1-SNAPSHOT.jar started by root in /home/springbootDemo)
2021-03-18 14:49:18.244  INFO 12886 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2021-03-18 14:49:19.924  INFO 12886 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-18 14:49:19.938  INFO 12886 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-18 14:49:19.938  INFO 12886 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2021-03-18 14:49:20.013  INFO 12886 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-18 14:49:20.014  INFO 12886 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1657 ms
2021-03-18 14:49:20.321  INFO 12886 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-18 14:49:20.520  INFO 12886 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-18 14:49:20.523  INFO 12886 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.899 seconds (JVM running for 3.369)

二、看到 Spring Boot 的启动日志后表明环境配置没有问题,编辑Dockerfile文件:

FROM java:8
COPY *.jar /app.jar

CMD ["--server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

三、接下来我们使用 Dockerfile 构建镜像:

## 构建镜像
[root@jiangwang springbootDemo]# docker build -t springboot-demo .
Sending build context to Docker daemon  17.72MB
Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> f4d6aeabd3f0
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in a6311f7cf7b5
Removing intermediate container a6311f7cf7b5
 ---> d8117b10cefa
Step 4/5 : EXPOSE 8080
 ---> Running in ae180be637bb
Removing intermediate container ae180be637bb
 ---> f16702c75ab6
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in fafa00625666
Removing intermediate container fafa00625666
 ---> d4c3e225699d
Successfully built d4c3e225699d
Successfully tagged springboot-demo:latest

四、运行镜像:

# 运行镜像
[root@jiangwang springbootDemo]# docker run -d -p 39005:8080 --name my-springboot springboot-demo
7ac35852cb91cb10612cd28fdbe7c50c7c59df4cccf19b2f1d30dcabbfe501f4
[root@jiangwang springbootDemo]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                     NAMES
7ac35852cb91   springboot-demo       "java -jar /app.jar …"   33 seconds ago   Up 32 seconds   0.0.0.0:39005->8080/tcp   my-springboot
[root@jiangwang springbootDemo]# curl localhost:39005/hello
hello,nihao[root@jiangwang springbootDemo]#

五、浏览器输入外网网址访问一下:

这里你的外网39005端口首先要开放了,可以去安全组设置

如何使用Docker部署Spring Boot项目

说明使用 Docker 部署 Spring Boot 项目成功!

以上是“如何使用Docker部署Spring Boot项目”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI