允许docker被远程访问
见:https://www.cnblogs.com/wintersoft/p/10921396.html
教程:https://spring.io/guides/gs/spring-boot-docker/#initial
在idea新建项目
spring boot启动类加上
@RestController
和
@RequestMapping("/") public String home() { return "Hello Docker!"; }
在启动类同级,新建Servletinitializer类
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class Servletinitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DockerDemo2Application.class); } }
pom.xml加入
<packaging>war</packaging>
和
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
点击idea右上的Edit Confgurations -> + -> Docker -> Dockerfile,配置如图
生成war包,终端输入:mvn clean package
项目根目录新建/tomcat/conf
复制tomcat的server.xml到/tomcat/conf
若没有tomcat配置文件可以下载一个,如:
wget -P /opt/downloads http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.20/bin/apache-tomcat-9.0.20.tar.gz
tar zxvf /opt/downloads/apache-tomcat-9.0.20.tar.gz -C /opt
server.xml的host节点内添加<Context path="" docBase="app" reloadable="true" debug="0" />
项目根目录创建Dockerfile
FROM tomcat MAINTAINER sundong ARG WAR=docker-demo2-1.0.0.war COPY /tomcat/conf/server.xml /publish/tomcat/conf/server.xml COPY target/$WAR /publish/app/$WAR RUN mkdir $CATALINA_HOME/webapps/app && cp /publish/app/$WAR $CATALINA_HOME/webapps/app && cp /publish/tomcat/conf/server.xml $CATALINA_HOME/conf && unzip $CATALINA_HOME/webapps/app/$WAR -d $CATALINA_HOME/webapps/app/ && rm $CATALINA_HOME/webapps/app/$WAR && cd $CATALINA_HOME/webapps/app && echo "succ" > a.txt EXPOSE 8080
项目根目录创建docker-compose.yml
version: '3.4'
services:
dockerweb:
image: dockerdemox2:latest
container_name: dockerdemox2
restart: always
build:
context: .
dockerfile: Dockerfile
volumes:
- ./tomcat:/publish/tomcat
- ./target:/publish/app
ports:
- "28080:8080"
解决tomcat启动慢
https://www.cnblogs.com/wintersoft/p/10942845.html
把项目中的target文件夹的war包、tomcat文件夹、Dockerfile和docker-compose.yml 复制到centos
进入centos网站目录
docker-compose up --build
或
docker-compose build
docker-compose up
浏览器输入192.168.1.102:38080,测试站点是否启动成功
若没问题,退出后,以守护进程启动站点
docker-compose up -d