使用spotify maven插件构buildDocker镜像时,会更改小型二进制文件

我正在使用Spotify的docker-maven-plugin来构buildDocker镜像。 更确切地说,这一个:

<groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> 

我的开发机器有一个Windows 7,所以我运行docker机版本docker-machine version 0.9.0, build 15fd4c7docker版本是这样的

 Client: Version: 1.13.1 API version: 1.26 Go version: go1.7.5 Git commit: 092cba3 Built: Wed Feb 8 08:47:51 2017 OS/Arch: windows/amd64 Server: Version: 17.03.0-ce API version: 1.26 (minimum version 1.12) Go version: go1.7.5 Git commit: 3a232c8 Built: Tue Feb 28 07:52:04 2017 OS/Arch: linux/amd64 Experimental: false 

我的应用程序使用我想提前准备的证书,并包含在图像中。

如果我直接使用Docker cli构buildDocker镜像,它将正常传输,证书的密钥库文件将被正确传输。

如果我使用spotify maven插件构buildDocker镜像,密钥库文件将被损坏。 一个比较显示,它的规模要大得多,它的内容比较(hexdump)看起来好像已经被多余的字节所散布 (我不知道该怎么说)。

我创build了一个显示行为的小例子:项目结构:

 -src |-main | |-docker | |-binaries | | |-example.jks | |-Dockerfile |-pom.xml 

像这样创buildexample.jks(使用来自openjdk 8的keytool)

 keytool -genkey -keyalg RSA -alias selfsigned -keystore example.jks -storepass password -keypass password -validity 18250 -keysize 2048 -dname "CN=Unknown, OU=Example, O=Example, L=Example, ST=Unknown, C=US" 

的pom.xml

 <?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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>smallbinary</artifactId> <name>Small binary problem</name> <version>1.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <resources> <resource> <!-- | Enable resource filtering pre dockerfile build calls. --> <directory>src/main/docker</directory> <targetPath>${project.build.directory}/docker-derived</targetPath> <filtering>true</filtering> </resource> </resources> <pluginManagement> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <executions> <execution> <id>build-image</id> <phase>compile</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <imageName>${project.artifactId}</imageName> <dockerDirectory>${project.build.directory}/docker-derived</dockerDirectory> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin><!-- Triggers the Docker build configured within the plugin management. --> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 

Dockerfile

 FROM openjdk:8-jre-alpine COPY binaries/* /opt/service/ CMD ["keytool", "-list", "-keystore", "/opt/service/example.jks", "-storepass", "password"] 

输出,如果直接docker build .每个docker build . 并运行docker run [imagename]

 Keystore type: JKS Keystore provider: SUN Your keystore contains 1 entry selfsigned, Apr 3, 2017, PrivateKeyEntry, Certificate fingerprint (SHA1): 07:56:26:66:16:82:DD:BF:6A:61:4B:94:E8:67:69:F8:77:36:5C:6D 

而令人遗憾的是,使用maven构build的输出:

 keytool error: java.io.IOException: Invalid keystore format 

在其他情况下,当复制更大的二进制文件,如jar子,战争,耳朵或邮编档案我没有遇到任何困难。 但是这个似乎不起作用。

我目前的解决方法是通过Dockerfile中的RUN命令直接在映像构build过程中创build证书。

有什么我失踪?

PS我在我的Linux Ubuntu 16.04 LTS笔记本电脑上遇到同样的问题。

问题是由我的例子中激活的资源过滤(以及我的生产代码)引起的。 我从我的技术生活中学到的,再次链接到一个stackoverflow问题的jar文件被损坏,而build设与maven

有问题的代码是这样的:

 <resource> <!-- | Enable resource filtering pre dockerfile build calls. --> <directory>src/main/docker</directory> <targetPath>${project.build.directory}/docker-derived</targetPath> <-- The next line breaks my binary--> <filtering>true</filtering> </resource> 

删除<filtering>true</filtering>或者明确地将其设置为false将修复我的示例代码并创build一个工作的Docker镜像。

如果你需要过滤,怎么办,像我这样做,因为我在我的生产代码Dockerfile中引用项目版本,并希望Mavenreplace一些令牌?

解决scheme是稍微改变一下项目结构,将可筛选和不可筛选的资源分开。

我改变了文件夹结构如下:

 -src |-main | |-docker | |-Dockerfile |-resources | |-binaries | |-example.jks |-pom.xml 

并改变了我的例子的资源部分是这样的:

 <resources> <resource> <!-- | Enable resource filtering pre dockerfile build calls for non binaries. --> <directory>src/main/docker</directory> <targetPath>${project.build.directory}/docker-derived</targetPath> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources/binaries</directory> <targetPath>${project.build.directory}/docker-derived/binaries</targetPath> </resource> </resources> 

然后它就像一个魅力。 抱歉怀疑spotify插件!