问题描述
我可以在 Dockerfile 的 CMD 节中使用环境变量吗?
Can I use environment variables in my CMD stanza in a Dockerfile?
我想做这样的事情:
CMD ["myserver", "--arg=$ARG", "--memcache=$MEMCACHE_11211_TCP_ADDR:$MEMCACHE_11211_TCP_PORT"]
其中 $MEMCACHE_11211_TCP_* 将通过包含我的 docker run
命令的 --link 参数自动设置.并且 $ARG 可以由用户在运行时配置,也许可以通过-e"参数?
Where $MEMCACHE_11211_TCP_* would be set automatically by the inclusion of the --link parameter of my docker run
command. And $ARG would be configurable by the user at runtime, maybe by the "-e" parameter?
这似乎对我不起作用,例如,它似乎是字面上传递字符串$ARG".
This doesn't seem to be working for me, it seems to be literally passing through the string "$ARG" for example.
推荐答案
这个回答可能有点晚了.但是根据您编写参数的方式,CMD
的环境解释略有不同.如果您将 CMD
作为字符串(不在数组内)传递,它会作为 shell 而不是 exec 启动.请参阅 https://docs.docker.com/engine/reference/builder/#cmd.
This answer may be a little late. But environment for CMD
is interpreted slightly differently depending on how you write the arguments. If you pass the CMD
as a string (not inside an array), it gets launched as a shell instead of exec. See https://docs.docker.com/engine/reference/builder/#cmd.
您可以尝试不带数组语法的 CMD
作为 shell 运行:
You may try the CMD
without the array syntax to run as a shell:
CMD myserver --arg=$ARG --memcache=$MEMCACHE_11211_TCP_ADDR:$MEMCACHE_11211_TCP_PORT
这篇关于在 CMD 中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!