使用docker run命令将参数传递给Dockerfile中的CMD [英] Use docker run command to pass arguments to CMD in Dockerfile
问题描述
node server.js 0 dev
或
node server.js 1 prod
在生产模式和开发模式之间切换,并确定是否应该打开集群。现在我想用参数创建docker图像来做类似的事情,到目前为止我唯一可以做的就是调整Docker文件有一行
CMD [node,server.js,0,dev]
和
docker build -t me / app。
来构建docker。
然后 docker运行-p 9000:9000 -d me / app
运行docker。
但是如果我想切换到prod模式,我需要将Dockerfile CMD更改为
CMD [node,server.js 1,prod]
,
,我需要杀死在9000端口上侦听的旧版本,并重建图像。
我希望我可以有一些像
docker run -p 9000:9000 environment = dev cluster = 0 -d me / app
创建一个图像,并使用environment和cluster参数运行nodejs命令,所以我不需要更改Docker文件并重建Docker。如何实现这一点?
确保您的Dockerfile声明一个环境变量与 ENV
:
ENV环境default_env_value
ENV集群default_cluster_value
ENV< key> < value>
表单可以替换为内联。
然后你可以通过一个带有docker运行的环境变量
docker run -p 9000:9000 -e environment = dev -e cluster = 0 -d me / app
或者你可以通过您的撰写文件设置它们:
节点:$ b $ b环境:
- 环境= dev
- 集群= 0
您的Dockerfile CMD
可以使用该环境变量,但如 issue 5509 ,您需要在 sh -c
form:
CMD [sh,-c,node server.js $ { cluster} $ {environment}]
解释是shell负责扩展环境变量,而不是Docker。当您使用 JSON语法时,您明确要求您的命令绕过shell并直接执行。
与 Builder RUN 相同的想法(适用于 CMD
):
与shell窗体不同,exec窗体不调用命令shell。
这意味着正常的shell处理不会发生。
例如,
RUN [echo,$ HOME]
不会在$ HOME
。如果你想要shell处理,那么可以使用shell窗体或者直接执行shell,例如:RUN [sh,-c,echo $ HOME]
。
当使用exec表单并直接执行一个shell时,就像shell表单一样,它是正在执行环境变量扩展的shell,而不是Docker。
I'm new to Docker and I'm having a hard time to setup the docker container as I want. I have a nodejs app can take two parameters when start. For example, I can use
node server.js 0 dev
or
node server.js 1 prod
to switch between production mode and dev mode and determine if it should turn the cluster on. Now I want to create docker image with arguments to do the similar thing, the only thing I can do so far is to adjust the Dockerfile to have a line
CMD [ "node", "server.js", "0", "dev"]
and
docker build -t me/app .
to build the docker.
Then docker run -p 9000:9000 -d me/app
to run the docker.
But If I want to switch to prod mode, I need to change the Dockerfile CMD to be
CMD [ "node", "server.js", "1", "prod"]
,
and I need to kill the old one listening on port 9000 and rebuild the image. I wish I can have something like
docker run -p 9000:9000 environment=dev cluster=0 -d me/app
to create an image and run the nodejs command with "environment" and "cluster" arguments, so I don't need to change the Dockerfile and rebuild the docker any more. How can I accomplish this?
Make sure your Dockerfile declares an environment variable with ENV
:
ENV environment default_env_value
ENV cluster default_cluster_value
The ENV <key> <value>
form can be replaced inline.
Then you can pass an environment variable with docker run
docker run -p 9000:9000 -e environment=dev -e cluster=0 -d me/app
Or you can set them through your compose file:
node:
environment:
- environment=dev
- cluster=0
Your Dockerfile CMD
can use that environment variable, but, as mentioned in issue 5509, you need to do so in a sh -c
form:
CMD ["sh", "-c", "node server.js ${cluster} ${environment}"]
The explanation is that the shell is responsible for expanding environment variables, not Docker. When you use the JSON syntax, you're explicitly requesting that your command bypass the shell and be executed directly.
Same idea with Builder RUN (applies to CMD
as well):
Unlike the shell form, the exec form does not invoke a command shell.
This means that normal shell processing does not happen.For example,
RUN [ "echo", "$HOME" ]
will not do variable substitution on$HOME
. If you want shell processing then either use the shell form or execute a shell directly, for example:RUN [ "sh", "-c", "echo $HOME" ]
.When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.
这篇关于使用docker run命令将参数传递给Dockerfile中的CMD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!