How to pass arguments to Docker container in Kubernetes or OpenShift through command line?

11/28/2019

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, e.g. 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?

-- Daniel P
docker
kubernetes
openshift

3 Answers

11/28/2019

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>"]
-- julian
Source: StackOverflow

11/28/2019

The following command should do it:

kubectl run my-app --image=my_docker_test -- argument_1
-- Jonas
Source: StackOverflow

11/28/2019

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.

-- georgeos
Source: StackOverflow