具有入口点变量扩展和 CMD 参数的 Docker 容器 [英] Docker container with entrypoint variable expansion and CMD parameters

查看:21
本文介绍了具有入口点变量扩展和 CMD 参数的 Docker 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个充当可执行文件的 Docker 映像,用户将令牌作为环境变量传递给该可执行文件.可执行文件具有用户应通过 dockers CMD 传递的子命令(考虑通过 Env 进行身份验证的 git).但是,Docker 不会将 CMD 附加到入口点.我的 Dockerfile 的相关部分如下所示:

I want to create a Docker Image that acts as an executable for which the user passes a token as an environment variable. The executable has sub commands that the user should pass via dockers CMD (think of git with authentication via Env). However, Docker does not append the CMD to the entrypoint. The relevant part of my Dockerfile looks like this:

ENTRYPOINT ["/bin/sh", "-c", "/usr/bin/mycmd --token=$MY_TOKEN"]
CMD ["pull", "stuff"]

所以如果这个容器在没有任何 CMD 覆盖和 secret 作为 MY_TOKEN 变量的情况下执行,我希望

So if this container is executed without any CMD overrides and secret as the MY_TOKEN variable, I would expect

mycmd --token=secret pull stuff

被执行.如果用户使用覆盖启动容器,例如

to be executed. If the user starts the container with an override, e.g.

docker run -it -e MY_TOKEN=secret myimage push junk

我希望

mycmd --token=secret push junk

被执行.然而,如上所述,只有 mycmd --token=secret 被执行,CMD 被忽略 - 无论我是在启动期间覆盖它还是在 Dockerfile 中设置它.

to be executed. As mentioned above, however, only mycmd --token=secret gets executed, the CMD is ignored - no matter if I override it during start or set it in the Dockerfile.

推荐答案

使用 /bin/sh -c "script" 语法,-c 参数之后的任何内容都变为脚本的参数.您可以使用 $0$@ 作为/bin/sh 脚本的一部分来访问它们:

With /bin/sh -c "script" syntax, anything after the -c argument becomes an argument to your script. You can reach them with $0 and $@ as part of your /bin/sh script:

ENTRYPOINT ["/bin/sh", "-c", "exec /usr/bin/mycmd --token=$MY_TOKEN $0 $@"]
CMD ["pull", "stuff"]

请注意,您还可以将入口点更改为添加到映像中的 shell 脚本,该脚本运行 exec/usr/bin/mycmd --token=$MY_TOKEN "$@" 并执行该 shell带有 docker 的 exec 语法的脚本:

Note that you could also change your entrypoint to be a shell script added to your image that runs exec /usr/bin/mycmd --token=$MY_TOKEN "$@" and execute that shell script with docker's exec syntax:

ENTRYPOINT ["/entrypoint.sh"]

这篇关于具有入口点变量扩展和 CMD 参数的 Docker 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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