作者:你给的未来丶我不要 | 来源:互联网 | 2023-05-19 09:43
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
我是Docker的新手,我很难按自己的意愿设置Docker容器。我有一个nodejs应用,启动时可以取两个参数。例如,我可以使用。
node server.js 0 dev
节点服务器。js 0开发
or
或
node server.js 1 prod
节点服务器。js - 1刺激
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
在生产模式和开发模式之间切换,并确定是否应该打开集群。现在我想用参数创建docker图像来做类似的事情,到目前为止我唯一能做的就是调整Dockerfile以得到一条直线。
CMD [ "node", "server.js", "0", "dev"]
CMD[“节点”、“服务器。js”、“0”、“开发”)
and
和
docker build -t me/app .
to build the docker.
docker构建-t me/app。构建码头工人。
Then docker run -p 9000:9000 -d me/app
to run the docker.
然后docker运行-p 9000:9000 -d me/app来运行docker。
But If I want to switch to prod mode, I need to change the Dockerfile CMD to be
但是如果我想切换到prod模式,我需要将Dockerfile CMD更改为
CMD [ "node", "server.js", "1", "prod"]
,
CMD[“节点”、“服务器。js”、“1”、“刺激”),
and I need to kill the old one listening on port 9000 and rebuild the image. I wish I can have something like
我需要杀死那个听9000端口的老家伙,重建这个图像。我希望我能有类似的东西
docker run -p 9000:9000 envirOnment=dev cluster=0 -d me/app
docker运行-p 9000:9000环境=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?
要创建一个映像并使用“environment”和“cluster”参数运行nodejs命令,所以我不再需要更改Dockerfile并重新构建docker。我怎样才能做到这一点呢?
3 个解决方案
19
Make sure your Dockerfile declares an environment variable with ENV
:
确保Dockerfile使用ENV声明了一个环境变量:
ENV environment default_env_value
ENV cluster default_cluster_value
The ENV
form can be replaced inline.
ENV
表单可以内联替换。
Then you can pass an environment variable with docker run
然后,您可以通过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:
您的Dockerfile CMD可以使用该环境变量,但是,正如第5509号问题所提到的,您需要以sh -c形式这样做:
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.
解释是shell负责扩展环境变量,而不是Docker。当您使用JSON语法时,您显式地请求您的命令绕过shell并直接执行。
Same idea with Builder RUN (applies to CMD
as well):
Builder RUN也有同样的想法(同样适用于CMD):
Unlike the shell form, the exec form does not invoke a command shell.
This means that normal shell processing does not happen.
与shell表单不同,exec表单不调用命令shell。这意味着正常的shell处理不会发生。
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" ]
.
例如,RUN ["echo", "$HOME"]不会对$HOME进行变量替换。如果您想要shell处理,那么可以使用shell表单或直接执行shell,例如: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.
当使用exec形式并直接执行shell时,就像在shell表单中一样,它是环境变量扩展的shell,而不是docker。