简体   繁体   English

如何通过命令行将参数传递给 Kubernetes 或 OpenShift 中的 Docker 容器?

[英]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 文件中指定commandargs 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那样直接从命令行传递参数,例如传递给kubectloc ,而无需在每次我想更改参数值时创建新的 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.

相关问题 如何在kubernetes中传递命令行参数? - How to pass command line arguments in kubernetes? 通过kubernetes启动容器时,如何将所有参数传递给Docker容器的权威指南? - Is there any definitive guide on how to pass all the arguments to Docker containers while starting a container through kubernetes? 如何将命令行参数传递给docker镜像 - How to pass command line arguments to a docker image 使用Kubernetes启动容器时如何传递Docker CLI参数 - How to pass the Docker CLI Arguments when starting a container using Kubernetes 如何通过 docker 运行将命令行 Arguments 传递到 .NET 控制台应用程序(Docker for Windows) - How to pass Command Line Arguments to .NET Console App through docker run (Docker for Windows) 如何在kubernetes上接受命令行参数的docker镜像上运行? - How to run a docker image on kubernetes that accepts command line arguments? 如何将命令行参数传递给在docker中运行的python脚本 - how to pass command line arguments to a python script running in docker 使用 Ansible 将命令行参数传递给 Docker - Pass command line arguments to Docker with Ansible Docker 传递命令行参数 - Docker pass command-line arguments 使用docker exec作为标签传递命令行参数 - Pass command line arguments with docker exec as a tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM