Shell dockerfile中的$()
在dockerfile上,我想从env var中的json导出数据。 我的问题是$(),不要与jq合作 我怎么做 MyDockerFile:Shell dockerfile中的$(),shell,dockerfile,Shell,Dockerfile,在dockerfile上,我想从env var中的json导出数据。 我的问题是$(),不要与jq合作 我怎么做 MyDockerFile: WORKDIR /myCurrentDir RUN wget "http://stedolan.github.io/jq/download/linux64/jq" RUN chmod 777 ./jq RUN cat /myCurrentDir/build/BUILD_INFO.json | /myCurrentDir/jq '.version' #
WORKDIR /myCurrentDir
RUN wget "http://stedolan.github.io/jq/download/linux64/jq"
RUN chmod 777 ./jq
RUN cat /myCurrentDir/build/BUILD_INFO.json | /myCurrentDir/jq '.version'
#work: return v1.23
RUN $(cat /myCurrentDir/build/BUILD_INFO.json | /myCurrentDir/jq '.version')
#don't work: /bin/sh: /myCurrentDir/jq: not found
我尝试使用jq而不是/myCurrentDir/jq,./jq,它仍然不起作用。ls显示jq安装在/myCurrentDir首先,您没有指定基本映像,因此Dockerfile不可复制 我试过这个:
FROM alpine
WORKDIR /myCurrentDir
RUN wget "http://stedolan.github.io/jq/download/linux64/jq"
RUN chmod 777 ./jq
RUN mkdir build && echo -e "{\"version\":\"v1.23\"}" > /myCurrentDir/build/BUILD_INFO.json
RUN cat /myCurrentDir/build/BUILD_INFO.json | /myCurrentDir/jq ".version"
即使在这个阶段,我也有
/bin/sh:/myCurrentDir/jq:未找到
首先,我建议您使用数据包管理器安装
jq
,这样您就不必担心它的可用性。
其次,可以通过在jq
命令中指定文件名来导出变量。例如,Dockerfile:
FROM alpine
WORKDIR /myCurrentDir
RUN apk add --no-cache jq
RUN mkdir build && echo -e "{\"version\":\"v1.23\"}" > /myCurrentDir/build/BUILD_INFO.json
RUN VER=$(jq '.version' /myCurrentDir/build/BUILD_INFO.json) && echo $VER > temp
为了检验这一点:
$ docker build -t test .
$ docker run test cat temp
"v1.23"