乐趣区

关于后端:64maven插件将jar包部署到docker容器

1. 插件

<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.39.1</version>

2. 根目录 pom 的插件治理配置

<build>
    <pluginManagement>
        <plugin>
                    <groupId>io.fabric8</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>0.39.1</version>
                    <configuration>
                        <!-- 配置近程 docker 守护过程 url-->
                        <dockerHost>tcp://192.168.207.109:2375</dockerHost>
<!--                        <dockerHost>tcp://127.0.0.1:2375</dockerHost>-->
                        <images>
                            <image>
                                <!-- 指定 image 的名字(蕴含 registry 地址)-->
                                <name>ybss-cloud/${project.artifactId}:${project.version}</name>
                                <alias>${project.artifactId}</alias>
                                <build>
                                    <!-- 指定 dockerfile 文件的地位 -->
                                    <dockerFile>${project.basedir}/Dockerfile</dockerFile>
                                    <cleanup>true</cleanup>
                                    <noCache>true</noCache>
                                    <buildOptions>

                                        <!-- 网络的配置,与宿主主机共端口号 -->
                                        <network>host</network>
                                        <forceRm>true</forceRm>
                                        <noCache>true</noCache>
                                    </buildOptions>
                                </build>

                                <run>
                                    <namingStrategy>alias</namingStrategy>
                                    <network>
                                        <name>ybss_default</name>
                                        <alias>${project.artifactId}-net</alias>
                                    </network>
                                    <ports>
                                        <port>${docker.port}</port>
                                    </ports>
                                    <autoRemove>true</autoRemove>
                                </run>
                            </image>
                        </images>
                    </configuration>
                    <executions>
                        <execution>
                            <id>docker-exec</id>
                            <!-- 绑定 mvn install 阶段,当执行 mvn install 时 就会执行 docker build 和 docker push-->
                            <phase>install</phase>
                            <goals>
                                <goal>build</goal>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    </pluginManagement>
</build>

3. 子模块的 pom 配置

    <properties>
        <mainClass>com.xxx.gateway.GatewayApplication</mainClass>
        <docker.port>18080:8080</docker.port>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

4. Dockerfile 内容

Dockerfile 文件地位与 pom.xml 同级

FROM openjdk:8-jre

MAINTAINER xxx

WORKDIR /opt

VOLUME /opt/logs

ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
ENV TZ Asia/Shanghai

ADD target/*-gateway-1.0.jar app.jar
ADD target/lib lib

EXPOSE 8080

ENTRYPOINT ["java", "-Dfile.encoding=utf-8", "-Xms1200m", "-Xmx1200m", "-XX:MetaspaceSize=128m", "-XX:MaxMetaspaceSize=128m", "-Xss512K", "-jar", "/opt/app.jar"]

5. 其余子模块相似

6. 部署

mvn clean
mvn package
mvn docker:build
mvn docker:run
退出移动版