Docker 部署spring web项目

1.项目打包为war包

2.启动一个tomcat容器,具体如下:

root@iZuf6axmuekh1n14dwcufmZ:~# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

f3b0f76293f9 tomcat:latest "catalina.sh run" 8 hours ago Up About an hour 0.0.0.0:8080->8080/tcp tomcat1

4ce1634ce6f1 mysql:latest "docker-entrypoint..." 4 weeks ago Up 4 weeks 0.0.0.0:3306->3306/tcp mysql1

root@iZuf6axmuekh1n14dwcufmZ:~# docker run --name tomcat2 -d -p 8083:8080 tomcat:latest

91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854

root@iZuf6axmuekh1n14dwcufmZ:~# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

91ebdd8d5c65 tomcat:latest "catalina.sh run" 9 seconds ago Up 9 seconds 0.0.0.0:8083->8080/tcp tomcat2

f3b0f76293f9 tomcat:latest "catalina.sh run" 8 hours ago Up About an hour 0.0.0.0:8080->8080/tcp tomcat1

4ce1634ce6f1 mysql:latest "docker-entrypoint..." 4 weeks ago Up 4 weeks 0.0.0.0:3306->3306/tcp mysql1

root@iZuf6axmuekh1n14dwcufmZ:~# 

关键命令:docker run --name tomcat2 -d -p 8083:8080 tomcat:latest

这里,将宿主的8083端口映射到tomcat容器的8080端口,启动后别名为tomcat2。

(此时,访问ip:8083就可以访问到此tomcat了)

3.上传war包到服务器上,然后复制到此tomcat容器的webapps目录下,具体如下:

root@iZuf6axmuekh1n14dwcufmZ:~# rz

rz waiting troot@iZuf6axmuekh1n14dwcufmZ:~# rz

root@iZuf6axmuekh1n14dwcufmZ:~# ls

tmall2.war

如何上传本地文件到服务器,点击这里

4.将此文件复制到刚才启动的tomcat2容器的webapps下,具体如下:

root@iZuf6axmuekh1n14dwcufmZ:~# docker cp tmall2.war 91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854:usr/local/tomcat/webapps

    root@iZuf6axmuekh1n14dwcufmZ:~# docker exec -it tomcat2 /bin/bash 进入容器

    root@91ebdd8d5c65:/usr/local/tomcat# ls

    LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work

    root@91ebdd8d5c65:/usr/local/tomcat# cd webapps/

    root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls

    ROOT docs examples host-manager manager tmall2 tmall2.war

    root@91ebdd8d5c65:/usr/local/tomcat/webapps# exit

91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854,为此容器的id的全称,如果不知道,获取命令为:

docker inspect -f '{{.Id}}' tomcat2
root@iZuf6axmuekh1n14dwcufmZ:~# docker inspect -f '{{.Id}}' tomcat2

91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854

5.重启tomcat2

root@iZuf6axmuekh1n14dwcufmZ:~# docker restart tomcat2

tomcat2

6.去浏览器访问:

SpringBoot项目发布到服务器,是一件很简单的事情,但是发布到服务器上docker启动的tomcat容器中,有一些坑,需要注意。

下面完整的介绍一下:将SpringBoot web项目发布到docker启动的tomcat容器中。

Spring项目发布,请参考:docker---(3)docker 发布spring web项目

SpringBoot入门,请参考:SpringBoot--从0开始学SpringBoot

Docker入门,请参考: Docker---从零开始学Docker

1.pom.xml

将打包方式改为war包

[java] view plain copy

  1. <packaging>war</packaging>

移除springBoot内置的tomcat,添加servlet-api

[java] view plain copy

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <!--移除内置的tomcat-->
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-tomcat</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <!--添加servlet-api-->
  13. <dependency>
  14. <groupId>org.apache.tomcat</groupId>
  15. <artifactId>tomcat-servlet-api</artifactId>
  16. <version>8.0.36</version>
  17. <scope>provided</scope>
  18. </dependency>

在build中添加项目访问路径

[java] view plain copy

  1. <finalName>tweb</finalName>

[java] view plain copy

  1. <build>
  2. <!--打包后的项目访问名称-->
  3. <finalName>tweb</finalName>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-maven-plugin</artifactId>
  8. </plugin>
  9. </plugins>
  10. </build>

2.自定义一个Servletinitializer类,继承SpringBootServletInitializer,用来启动上下文

(和启动类目录同级)

[java] view plain copy

  1. package com.jd;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.boot.builder.SpringApplicationBuilder;
  4. import org.springframework.boot.web.support.SpringBootServletInitializer;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. /**
  8. * Date 2018/1/30
  9. * Description:启动spring的上下文
  10. */
  11. public class Servletinitializer extends SpringBootServletInitializer{
  12. @Override
  13. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  14. return builder.sources(TwebApplication.class);
  15. }
  16. }

3.添加一个测试接口,供测试访问使用

[java] view plain copy

  1. package com.jd;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * 启动类
  9. */
  10. @SpringBootApplication
  11. @RestController
  12. public class TwebApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(TwebApplication.class, args);
  15. }
  16. @RequestMapping(value = "hello",method = RequestMethod.GET)
  17. public String testGood(String name){
  18. return "Welcome to tweb Mr."+ name;
  19. }
  20. @RequestMapping(value = "test",method = RequestMethod.GET)
  21. public String test(){
  22. return "Welcome to tweb";
  23. }
  24. }

然后,maven clean package,打包完成后,项目的target目录下就会有此war包。

此时,项目就准备好了,接下来,就是服务器上的docker的使用了。

4.把war包上传至服务器

看tomcat下有没有其他项目,有就删除,没有就拉倒,我这里有项目,我先删除一下

不熟悉docker基本命令的,可以参考:docker---(1)常用命令

[java] view plain copy

  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker exec -it tomcat2 /bin/bash
  2. root@91ebdd8d5c65:/usr/local/tomcat# cd webapps/
  3. root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls
  4. ROOT docs examples host-manager manager tweb tweb.war
  5. root@91ebdd8d5c65:/usr/local/tomcat/webapps# rm tweb.war
  6. root@91ebdd8d5c65:/usr/local/tomcat/webapps# rm -rf tweb
  7. root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls
  8. ROOT docs examples host-manager manager

然后,将war包上传至服务器

rz用不了,看这里:阿里云---阿里云服务器ECS安装jdk环境

[java] view plain copy

  1. root@iZuf6axmuekh1n14dwcufmZ:~# rz
  2. root@iZuf6axmuekh1n14dwcufmZ:~# ls
  3. tweb.war

获取tomcat容器的id,我这里启动时名字叫tomcat2

[java] view plain copy

  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker inspect -f '{{.Id}}' tomcat2
  2. 91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854

复制war包到tomcat容器的webapps下,然后查看是否复制成功

[java] view plain copy

  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker cp tweb.war 91ebdd8d5c652b465cc15f86cdc1c41167e211aaae9f1e42ab1032e7db8d3854:usr/local/tomcat/webapps
  2. root@iZuf6axmuekh1n14dwcufmZ:~# docker exec -it tomcat2 /bin/bash
  3. root@91ebdd8d5c65:/usr/local/tomcat# ls
  4. LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work
  5. root@91ebdd8d5c65:/usr/local/tomcat# cd webapps/
  6. root@91ebdd8d5c65:/usr/local/tomcat/webapps# ls
  7. ROOT docs examples host-manager manager tweb tweb.war
  8. root@91ebdd8d5c65:/usr/local/tomcat/webapps# exit
  9. exit
  10. root@iZuf6axmuekh1n14dwcufmZ:~#

复制成功后,重启tomcat即可

[java] view plain copy

  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker restart tomcat2
  2. tomcat2

查看状态,我这里启动时端口映射为8083

[java] view plain copy

  1. root@iZuf6axmuekh1n14dwcufmZ:~# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 996ff5cc0db6 webcenter/activemq "/app/run.sh" 24 hours ago Up 16 hours 1883/tcp, 5672/tcp, 0.0.0.0:8161->8161/tcp, 61613-61614/tcp, 0.0.0.0:61616->61616/tcp activemq
  4. 35350de323ca tomcat:latest "catalina.sh run" 41 hours ago Up 44 minutes 0.0.0.0:8085->8080/tcp tweb_tomcat
  5. de67495c8a87 redis:latest "docker-entrypoint..." 12 days ago Exited (255) 22 hours ago 0.0.0.0:6379->6379/tcp redis1
  6. 91ebdd8d5c65 tomcat:latest "catalina.sh run" 2 weeks ago Up 28 seconds 0.0.0.0:8083->8080/tcp tomcat2
  7. f3b0f76293f9 tomcat:latest "catalina.sh run" 2 weeks ago Exited (255) 16 hours ago 0.0.0.0:8080->8080/tcp tomcat1
  8. 4ce1634ce6f1 mysql:latest "docker-entrypoint..." 6 weeks ago Exited (255) 16 hours ago 0.0.0.0:3306->3306/tcp mysql1
  9. root@iZuf6axmuekh1n14dwcufmZ:~# ^C

去浏览器访问

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券