英文:
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:
<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>
My 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 $@"]
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 "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
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), "utf-8");
} catch (UnsupportedEncodingException e) {
throw new MojoExecutionException("Could not build image", 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('+', ' ');
^^^^^^^^^^^^^^^^^^^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论