[英]How to pass arguments to Docker container in Kubernetes or OpenShift through command line?
I have a bash script in a Docker image to which I can pass a command line argument through docker run
(having specified the bash script in ENTRYPOINT
and a default parameter in CMD
like in this answer ).我在 Docker 映像中有一个 bash 脚本,我可以通过
ENTRYPOINT
docker run
将命令行参数传递给它(已在ENTRYPOINT
中指定了 bash 脚本,并在CMD
中指定了一个默认参数,如本答案中所示)。 So I would run something like所以我会运行类似的东西
docker run my_docker_test argument_1
Now I would like to deploy multiple (ca. 50) containers to OpenShift or Kubernetes, each with a different value of the argument.现在我想将多个(大约 50 个)容器部署到 OpenShift 或 Kubernetes,每个容器都有不同的参数值。 I understand that in Kubernetes I could specify the
command
and args
in the object configuration yaml file.我知道在 Kubernetes 中我可以在对象配置 yaml 文件中指定
command
和args
。 Is there a possibility to pass the argument directly from the command line like in docker run
, eg passing to kubectl
or oc
, without the need to create a new yaml file each time I want to change the value of the argument?是否有可能像在
kubectl
docker run
那样直接从命令行传递参数,例如传递给kubectl
或oc
,而无需在每次我想更改参数值时创建新的 yaml 文件?
The right answer is from @Jonas but you can also use environment variables in your yaml file as stated below :正确答案来自@Jonas,但您也可以在 yaml 文件中使用环境变量, 如下所述:
As an alternative to providing strings directly, you can define arguments by using environment variables
作为直接提供字符串的替代方法,您可以使用环境变量定义参数
env:
- name: ARGUMENT
value: {{ argument_1 }}
args: ["$(ARGUMENT)"]
Where {{ argument_1 }} is an environment variable.其中 {{ argument_1 }} 是环境变量。
以下命令应该这样做:
kubectl run my-app --image=my_docker_test -- argument_1
I think the best way is to define them in your manifest (yaml file) in the container level, although environmental variables also work.我认为最好的方法是在容器级别的清单(yaml 文件)中定义它们,尽管环境变量也有效。
If you spawn multiple containers in your pod (not usually recommended but sometimes useful) I think the environmental variables is messy as they are defined on a pod level.如果您在 pod 中生成多个容器(通常不推荐但有时有用),我认为环境变量是混乱的,因为它们是在 pod 级别定义的。
Note: If you do define environmental variables they are parsed as strings so you need to put numeric values in quotes.
注意:如果您确实定义了环境变量,它们将被解析为字符串,因此您需要将数值放在引号中。
containers:
- name: <foo>
image: <bar>:latest
args: ["<argumet_value>"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.