'How to pass command and argument to a container in deployment file of kubernetes

I have to write a deployment file for my application which takes run time commands and arguments at runtime. e.g. ./foo bar -a=A -b=B This is my deployment file :

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: foo
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: foo
    spec:
      containers:
      - name: foo
        image: username/image:tag
        # command that to be executed at run time
        command: ["bar"]
        args:
        # This is the flag to pass at runtime
        - -a=A
        - -b=B
        ports:
        - containerPort: 9500

It says container command 'foo' not found or doesnot exist. i'm passing a script as entrypoint and it has exec /usr/local/bin/foo . What's wrong with it ?



Solution 1:[1]

If you want start ./foo bar -a=A -b=B in container, your deployment should contains

- name: foo
  image: username/image:tag
  command: ["foo"]
  args: ["bar", "-a=A", "-b=B"]

https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/

Be sure you have path to foo binary in your $PATH enviroment variable inside username/image:tag image (or use full path /usr/local/bin/foo).

You can check $PATH by

kubectl run  test -ti --rm --image=username/image:tag --command -- echo $PATH

kubectl run  test -ti --rm --image=username/image:tag --command -- foo

Solution 2:[2]

You can do something like this:

- name: foo
  image: username/image:tag
  command: 
    - bar
    - '-a=A'
    - '-b=B'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BMW
Solution 2 Mostafa Wael