在CMD中使用环境变量 [英] Use environment variables in CMD

查看:745
本文介绍了在CMD中使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在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"]

通过包含我的docker run命令的--link参数将自动设置$ MEMCACHE_11211_TCP_ *的位置. $ 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作为外壳程序运行:

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中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆