Dock Maven Plugin
配置 docker maven plugin
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.38.0</version>
<configuration>
<!--suppress MavenModelInspection -->
<dockerHost>${docker.host}</dockerHost>
<!--suppress MavenModelInspection -->
<pullRegistry>${docker.pull.registry}</pullRegistry>
<!--suppress MavenModelInspection -->
<pushRegistry>${docker.push.registry}</pushRegistry>
<authConfig>
<pull>
<!--suppress MavenModelInspection -->
<username>${docker.pull.username}</username>
<!--suppress MavenModelInspection -->
<password>${docker.pull.password}</password>
</pull>
<push>
<!--suppress MavenModelInspection -->
<username>${docker.push.username}</username>
<!--suppress MavenModelInspection -->
<password>${docker.push.password}</password>
</push>
</authConfig>
<images>
<image>
<alias>${project.name}</alias>
<name>backend-beta/${project.name}</name>
<build>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
</tags>
<from>surpath-basic-image/surpath-basic-jdk11:v1.0.0</from>
<ports>
<port>8889</port>
</ports>
<assemblies>
<!-- copy cat client config -->
<assembly>
<name>data/appdatas/cat</name>
<inline>
<id>copy-file</id>
<files>
<file>
<source>${project.build.directory}/classes/application.yml</source>
<outputDirectory>.</outputDirectory>
</file>
</files>
</inline>
</assembly>
<!-- copy executable jar -->
<assembly>
<name>data/server</name>
<inline>
<id>copy-file</id>
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}.jar</source>
<outputDirectory>.</outputDirectory>
</file>
</files>
</inline>
</assembly>
</assemblies>
<entryPoint>
<exec>
<arg>/bin/bash</arg>
<arg>-c</arg>
<arg>$JAVA_HOME/bin/java -Dapollo.meta=$apollo_meta -Denv=$apollo_env -Dapp.id=$apollo_app -jar /data/server/${project.build.finalName}.jar</arg>
</exec>
</entryPoint>
</build>
</image>
</images>
</configuration>
</plugin>
</plugins>
</build>
</profile>
注意
- 文件拷贝
assembly节点的name属性会作为构建时在build目录下创建的文件夹名,name的值需唯一。outputDirectory属性的值为目的文件夹,name下的相对路径。 entryPoint的写法需注意,建议使用exec模式。exec模式中注入环境变量的解决方式见上面配置文件(其实就是在exec中使用shell的折中方案)。- 环境变量名需注意,经测试
.,-作为分隔符无法识别。
打包 docker 镜像
mvn package -Dmaven.test.skip=true -P jdk11 && \
mvn -f ./project_home/pom.xml -P docker docker:build
注意
- 先构建出可执行
jar,再使用docker配置打包镜像。
在 docker 中启动
docker run -p 8889:8889 -v /path/of/host:/path/of/container \
-e apollo.env=my.apollo.env -e apollo.app=my.apollo.app -e apollo.meta=my.apollo.meta \
docker_image:latest
注意
- 启动容器时需要注意
run的[options]需在镜像名称前面。 - 环境变量名需注意,经测试
.,-作为分隔符无法识别。