简体   繁体   中英

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 ). 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. I understand that in Kubernetes I could specify the command and args in the object configuration yaml file. 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?

The right answer is from @Jonas but you can also use environment variables in your yaml file as stated below :

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.

以下命令应该这样做:

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.

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.

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>"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM