我想在 docker 运行期间将 JSON 文件的内容作为环境变量传递。 docker run 在 systemd 服务文件中初始化。
我做了类似的事情:
export TEMP_CONFIG=$(cat /etc/config.json)
docker run \
--env SERVICE_NAME=${CONTAINER_NAME} \
--env TEMP_CONFIG \
root@ip-10-109-7-77:/usr/local/nginx/conf# echo ${TEMP_CONFIG}
root@ip-10-109-7-77:/usr/local/nginx/conf#
--env TEMP_CONFIG=$(cat /etc/config.json) \
docker: Error parsing reference: "\"conf\"" is not a valid repository/tag.
{
"conf" :
{
"appname" :
{
"dbhost" : "xxxx",
"dbname" : "dbname",
"dbuser" : "user",
"dbpassword" : "xxxxx",
"hostname" : "xxxxxx"
},
"cacheBaseDir" : "/storage/",
"iccprofile" : "/etc/nginx/RGB.V1.0.icc",
"tmpDir" : "/tmp",
"mdb" :
{
"user" : "user",
"password" : "xxxxx",
"rights" : "GlobalAdministrator",
"company" : "somecompany"
}
}
}
最佳答案
更新答案
您提到您使用 docker run
systemd 单元文件中的命令。一个系统 ExecStart
options 不是在 shell 中启动的。名称支持环境变量替换。另见 the documentation对此:
Basic environment variable substitution is supported. Use "
${FOO}
" as part of a word, or as a word of its own, on the command line, in which case it will be replaced by the value of the environment variable including all whitespace it contains, resulting in a single argument.
StartExec
不在 shell 中执行:This syntax is intended to be very similar to shell syntax, but only the meta-characters and expansions described in the following paragraphs are understood. Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported. [...] Note that shell command lines are not directly supported.
ExecStart
启动一个 shell,然后使用 -c
传递命令标志(您仍然需要引用我在下面的原始答案中提到的变量):ExecStart=/bin/bash -c "docker run -e \"TEMP_CONFIG=$(</etc/config.json)\" ..."
TEMP_CONFIG=$(cat /etc/config.json)
本质上等同于:--env TEMP_CONFIG={ "conf" : { "...
TEMP_CONFIG
环境变量将具有值 {
, 和 docker run
将承担 "conf"
成为下一个参数(在本例中为图像名称)。--env "TEMP_CONFIG=$(cat /etc/config.json)"
cat
当您不必:--env "TEMP_CONFIG=$(</etc/config.json)"
https://stackoverflow.com/questions/42444021/
相关文章:
opencv - OpenCV || matchShapes findContours断言失败
opencv - mac上opencv2.3.1中 undefined symbol ,cvSmoo
android - 使用 OpenCV 在 Android 中将 4 channel 矩阵转换为 1
visual-studio-2008 - 应用程序无法启动:VS中的OpenCV x64编译使用x8
c# - DLL 'cvCreateContourTree'中的 'opencv_imgproc23