2017-04-03 80 views
0

我正在使用Spotify的docker-maven插件来构建docker镜像。更确切地说这一个:使用spotify maven插件构建Docker镜像时更改小二进制文件

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

我的机器都有一个Windows 7,所以我跑泊坞窗机版本docker-machine version 0.9.0, build 15fd4c7 泊坞版本是这样

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直接构建Docker镜像,它将正常传输证书的密钥库文件。

如果我使用spotify maven插件构建Docker镜像,keystore文件将被损坏。一个比较表明它的尺寸比它大得多,它的内容比较(hexdump)看起来好像是洒了(我不知道该怎么说更好)和额外的字节。

我已经创建了一个小例子示出的行为: 项目结构:

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

创建这样example.jks(使用密钥工具从的openjdk 8)

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"] 

输出IF直接内置每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 

而可悲输出运行时与行家内置:

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

在其他上下文中,复制等罐子大的二进制文件时,战争,耳朵或邮编档案我没有遇到任何困难。但这个似乎并不奏效。

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

有什么我失踪?

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

回答

0

这个问题是由我的示例中激活的资源过滤(以及我的生产代码)引起的。 ,我从My Technical Life了解到这又链接到计算器问题jar file gets corrupted while building with 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修复我的示例代码和创建工作的码头工人的形象。

如果您需要过滤该怎么办,就像我这样做,因为我在我的生产代码Dockerfile中引用了项目版本,并希望Maven替换一些标记?

解决方案是稍微改变项目结构并分离可过滤和不可过滤的资源。

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

-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插件!

相关问题