Dockerfile特殊转义符

huangapple go评论87阅读模式
英文:

Dockerfile special escape symbol

问题

当我使用带有'--build-arg'的命令构建Docker镜像时,传入参数中的空格被替换为加号。

我的Maven pom.xml:

<plugin>
   <groupId>com.spotify</groupId>
   <artifactId>dockerfile-maven-plugin</artifactId>
   <version>1.4.13</version>
   <configuration>
      <repository>192.168.1.200/library/${project.artifactId}</repository>
      <tag>${project.version}</tag>
      <buildArgs>
         <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
         <JVM_OPTS>-Xms300m -Xmx300m</JVM_OPTS>
      </buildArgs>
   </configuration>
</plugin>

我的Dockerfile:

FROM 192.168.1.200/library/openjdk:8-jdk-alpine
MAINTAINER gaojiayiner@163.com
LABEL version="1.0.0"
RUN mkdir -p /project && mkdir -p /data
ENV workdir /project
WORKDIR $workdir
ARG JAR_FILE
ARG JVM_OPTS
ENV jvm $JVM_OPTS
COPY target/${JAR_FILE} /project/demo.jar
EXPOSE 8080
ENTRYPOINT ["sh","-c","java -jar /project/demo.jar $0 $@"]

终端执行记录:

[root@localhost docker-java]# mvn clean package dockerfile:build
[root@localhost docker-java]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
192.168.1.200/library/demo      0.0.1-SNAPSHOT      13373a4761a1        3 seconds ago       131MB
192.168.1.200/library/openjdk   8-jdk-alpine        a3562aa0b991        14 months ago       105MB
[root@localhost docker-java]# docker run -d  13373a4761a1
d0b5ce9392b12505419d5648332f204dc3d59327efd389c27a950aa0d1daba35
[root@localhost docker-java]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                            NAMES
d0b5ce9392b1        13373a4761a1             "sh -c 'java -jar /p…"   4 seconds ago       Up 4 seconds        8080/tcp                                         reverent_kirch
[root@localhost docker-java]# docker exec -it d0b5ce9392b1 sh
/project # env
jvm=-Xms300m -Xmx300m
JAVA_ALPINE_VERSION=8.212.04-r0
HOSTNAME=d0b5ce9392b1

您可以看到代码中的 jvm=-Xms300m -Xmx300m。我该如何解决这个问题?

英文:

When I use the command with '--build-arg' to build the docker-image,the spaces in incoming parameters are replaced with a plus sign.

My maven pom.xml:

&lt;plugin&gt;
   &lt;groupId&gt;com.spotify&lt;/groupId&gt;
    &lt;artifactId&gt;dockerfile-maven-plugin&lt;/artifactId&gt;
    &lt;version&gt;1.4.13&lt;/version&gt;
    &lt;configuration&gt;
      &lt;repository&gt;192.168.1.200/library/${project.artifactId}&lt;/repository&gt;
      &lt;tag&gt;${project.version}&lt;/tag&gt;
      &lt;buildArgs&gt;
         &lt;JAR_FILE&gt;${project.build.finalName}.jar&lt;/JAR_FILE&gt;
         &lt;JVM_OPTS&gt;-Xms300m -Xmx300m&lt;/JVM_OPTS&gt;
      &lt;/buildArgs&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

My Dockerfile:

FROM 192.168.1.200/library/openjdk:8-jdk-alpine
MAINTAINER gaojiayiner@163.com
LABEL version=&quot;1.0.0&quot;
RUN mkdir -p /project &amp;&amp; mkdir -p /data
ENV workdir /project
WORKDIR $workdir
ARG JAR_FILE
ARG JVM_OPTS
ENV jvm $JVM_OPTS
COPY target/${JAR_FILE} /project/demo.jar
EXPOSE 8080
ENTRYPOINT [&quot;sh&quot;,&quot;-c&quot;,&quot;java -jar /project/demo.jar $0 $@&quot;]

Terminal execution record:

[root@localhost docker-java]# mvn clean package dockerfile:build
[root@localhost docker-java]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
192.168.1.200/library/demo      0.0.1-SNAPSHOT      13373a4761a1        3 seconds ago       131MB
192.168.1.200/library/openjdk   8-jdk-alpine        a3562aa0b991        14 months ago       105MB
[root@localhost docker-java]# docker run -d  13373a4761a1
d0b5ce9392b12505419d5648332f204dc3d59327efd389c27a950aa0d1daba35
[root@localhost docker-java]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                            NAMES
d0b5ce9392b1        13373a4761a1             &quot;sh -c &#39;java -jar /p…&quot;   4 seconds ago       Up 4 seconds        8080/tcp                                         reverent_kirch
[root@localhost docker-java]# docker exec -it d0b5ce9392b1 sh
/project # env
jvm=-Xms300m+-Xmx300m
JAVA_ALPINE_VERSION=8.212.04-r0
HOSTNAME=d0b5ce9392b1

You can see the code jvm=-Xms300m+-Xmx300m. How can I solve this?

答案1

得分: 2

您需要自行重新编译spotify/dockerfile-maven,因为修复此错误的拉取请求尚未合并/接受。

请查看"spotify/dockerfile-maven PR 350"。

它指向了问题的根本原因:plugin/dockerfile/BuildMojo.java#encodeBuildParam

private static String encodeBuildParam(Object buildParam) throws MojoExecutionException {
  try {
    return URLEncoder.encode(new Gson().toJson(buildParam), "utf-8");
  } catch (UnsupportedEncodingException e) {
    throw new MojoExecutionException("无法构建映像", e);
  }
}

实际上,我们可以在这里(URLEncoder类)阅读到,URLEncoder将空格字符" "替换为加号"+"。

在编码之后,我添加了一个替换操作,将加号替换为空格:因为编码后所有的加号已经被编码为"/0x2B",所以剩下的加号都是来自空格。

修复包括:

return URLEncoder.encode(
         new Gson().toJson(buildParam), StandardCharsets.UTF_8.toString()
       ).replace('+', ' ');
       ^^^^^^^^^^^^^^^^^^^
英文:

You would need to recompile the spotify/dockerfile-maven yourself, because the pull request which fixes this bug is not yet merged/accepted.

See "spotify/dockerfile-maven PR 350"

It points out to the culprit: plugin/dockerfile/BuildMojo.java#encodeBuildParam

  private static String encodeBuildParam(Object buildParam) throws MojoExecutionException {
    try {
      return URLEncoder.encode(new Gson().toJson(buildParam), &quot;utf-8&quot;);
    } catch (UnsupportedEncodingException e) {
      throw new MojoExecutionException(&quot;Could not build image&quot;, e);
    }
  }

> Indeed, we can read here (class URLEncoder) that URLEncoder replaces the space character " " with a plus sign "+".
>
> After the encoding, I added a replace to substitute the + sign with a space: after the encoding all the plus signs will be already encoded to /0x2B, so the ones left are the ones are coming from spaces.

The fix would include:

      return URLEncoder.encode(
               new Gson().toJson(buildParam), StandardCharsets.UTF_8.toString()
             ).replace(&#39;+&#39;, &#39; &#39;);
              ^^^^^^^^^^^^^^^^^^^

huangapple
  • 本文由 发表于 2020年8月1日 13:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63202063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定