2017-04-03 74 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插件!

相關問題