简体   繁体   English

docker-compose:CMD 变量替换

[英]docker-compose: CMD variable substitution

I am building a new Docker image but my CMD command with variable substitution does not work.我正在构建一个新的 Docker 映像,但我的带有变量替换的 CMD 命令不起作用。

I know that I need to turn on the interpreter with -c like this:我知道我需要像这样用-c打开解释器:

CMD ["sh", "-c", "hello.sh $VARIABLE"]

But my use case is different because I need to execute something with sudo :但是我的用例不同,因为我需要使用sudo执行某些操作:

CMD ["sudo", "-u", "oracle", "$ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/startWebLogic.sh"]

This works properly:这正常工作:

CMD $ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/startWebLogic.sh

Ho to make it works with sudo ?如何使它与sudo

是一样的,只是前面加了sudo

CMD ["sudo", "-u", "oracle", "sh", "-c", "hello.sh $VARIABLE"]

Try:尝试:

CMD ["sudo", "-E", "-u", "oracle", "sh", "-c", "\"$ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/startWebLogic.sh\""]

By default sudo starts a new environment.默认情况下sudo启动一个新环境。 To have sudo preserve the environment you need to pass -E option.要让sudo保留环境,您需要传递-E选项。

Then to have $variables expand, you need to run it under a shell, so they can expand.然后要扩展$variables ,您需要在 shell 下运行它,以便它们可以扩展。 But then to protect against word splitting shell expansion I think you have to quote the variables expansions, thus the additional \\" .但是为了防止分词外壳扩展,我认为您必须引用变量扩展,因此附加\\"

Maybe this would be easier and also work:也许这会更容易并且也有效:

CMD ["sh", "-c", "sudo -u oracle \"$ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/startWebLogic.sh\""]

But in docker I think it's preferable to just set the user:但在 docker 中,我认为最好只设置用户:

USER oracle
CMD $ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/startWebLogic.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM