容許docker被遠程訪問html
見:http://www.javashuo.com/article/p-gvzcttro-kw.htmlweb
教程:https://spring.io/guides/gs/spring-boot-docker/#initialspring
在idea新建項目docker
spring boot啓動類加上apache
@RestController
和centos
@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加入tomcat
<packaging>war</packaging>
和app
<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,配置如圖webapp
生成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啓動慢
http://www.javashuo.com/article/p-ucwmojwh-eo.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