使用docker部署spring-boot多模块依赖工程
docker自动部署多模块的maven工程
转载:https://hbxflihua.iteye.com/blog/2432005

使用docker部署spring-boot多模块依赖工程
项目架构

dw

|

|----dw-demo(接口demo)

|

|----dw-core(代码核心)

|

|----dw-api(接口,依赖dw-core)

|

|----dw-manage(后台,依赖dw-core)

一、准备工作

首先看下各模块pom文件

1、项目父工程POM

<?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.rd</groupId>  
    <artifactId>dw</artifactId>  
    <version>2.0.0</version>  
    <packaging>pom</packaging>  
  
    <modules>  
        <module>dw-core</module> <!--核心业务 -->  
        <module>dw-manage</module><!-- 后台 -->  
        <module>dw-api</module><!-- API -->  
        <module>dw-demo</module><!-- demo -->  
    </modules>  
  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.0.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
      
</project>  

2、dw-core的POM

<?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>  
  
    <artifactId>dw-core</artifactId>  
    <packaging>jar</packaging>  
  
    <parent>  
        <groupId>com.rd</groupId>  
        <artifactId>dw</artifactId>  
        <version>2.0.0</version>  
        <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->  
    </parent>  
  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
        <java.version>1.8</java.version>  
        <relativePath/>  
    </properties>  
  
    <dependencies>  
        ......  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
                <configuration>  
                    <classifier>exec</classifier>  
                </configuration>  
            </plugin>           
        </plugins>  
    </build>  
  
</project>  

3、dw-demo的pom文件

<?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>  
  
    <artifactId>dw-demo</artifactId>  
    <packaging>jar</packaging>  
  
    <parent>  
        <groupId>com.rd</groupId>  
        <artifactId>dw</artifactId>  
        <version>2.0.0</version>  
        <relativePath>../pom.xml</relativePath>   
    </parent>  
  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
        <java.version>1.8</java.version>  
    </properties>  
  
    <dependencies>  
        ......  
    </dependencies>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
                <configuration>  
                    <executable>true</executable>  
                </configuration>  
            </plugin>  
              
            <!-- Docker maven plugin -->  
            <plugin>  
                <groupId>com.spotify</groupId>  
                <artifactId>docker-maven-plugin</artifactId>  
                <version>1.0.0</version>  
                <configuration>  
                    <imageName>${project.groupId}/${project.artifactId}:${project.version}</imageName>  
                    <dockerDirectory>src/main/resources/docker</dockerDirectory>  
                    <resources>  
                        <resource>  
                            <targetPath>/</targetPath>  
                            <directory>${project.build.directory}</directory>  
                            <include>${project.build.finalName}.jar</include>  
                        </resource>  
                    </resources>  
                </configuration>  
            </plugin>  
            <!-- Docker maven plugin -->  
              
        </plugins>  
    </build>  
      
</project>  

4、dw-api和dw-manage的pom和dw-demo类似,这里就不展示了

二、创建Dockerfile文件

在dw-demo、dw-api、dw-manage三个模块的sr/main/resources/docker下分别创建Dockerfile文件,Dockerfile 文件用来说明如何来构建镜像。这里以dw-demo为例来说明,文件内容如下:

# 指定基础镜像  
FROM hub.c.163.com/library/java  
# 指定作者名和E-mail  
MAINTAINER lihua "lh@erongdu.com"  
  
#指定挂载点  
VOLUME /tmp  
#将本地文件或目录复制到由dockerfile构建的镜像中  
ADD dw-demo-2.0.0.jar demo.jar  
#设置环境变量  
#ENV JAVA_OPTS=""  
#提供容器运行的默认命令  
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/demo.jar"]  

三、构建打包环境

在服务器上安装jdk、maven 、docker、svn,具体参考如下链接,这里不做赘述。

ubuntu16.04搭建jdk1.8运行环境

ubuntu16.04安装maven

Ubuntu16.04安装Docker

Ubuntu SVN客户端安装

四、docker部署spring-boot工程

1、进入父工程pom文件所在目录,打包编译,将依赖包放至本地仓库

mvn clean install package -Dmaven.test.skip  

2、进入各模块,使用 DockerFile 构建镜像

cd dw-demo  
mvn package docker:build -Dmaven.test.skip  

3、运行该镜像

docker run -p 9882:8082 -t com.rd/dw-demo:2.0.0  

五、查看服务

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐