Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes - Passing multiple commands to the container

Tags:

kubernetes

I want send multiple entrypoint commands to a Docker container in the command tag of kubernetes config file.

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:  # specification of the pod’s contents
  restartPolicy: Never
  containers:
  - name: hello
    image: "ubuntu:14.04"
    command: ["command1 arg1 arg2 && command2 arg3 && command3 arg 4"]

But it seems like it does not work. What is the correct format of sending multiple commands in the command tag?

like image 999
Dimuthu Avatar asked Nov 29 '15 04:11

Dimuthu


People also ask

How do you pass multiple commands in Kubernetes Yaml?

The first part(command: ["/bin/sh","-c"]) just tells the machine to run a shell and then run the following commands. The portion in args is where the actual commands are passed to the shell. You use semicolon to separate the commands. OR an easier wary in my opinion is to build your own dockerfile.

How do you pass arguments in Kubernetes?

Define Commands and Arguments for a Kubernetes Pod We can pass these arguments to the command using the args field. In the below example, we have passed the command printenv to the container for it to print the values for the environment variable KUBECONFIG as an argument to it.

What is the difference between command and args in Kubernetes?

If you supply a command but no args for a Container, only the supplied command is used. The default EntryPoint and the default Cmd defined in the Docker image are ignored. If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.


4 Answers

There can only be a single entrypoint in a container... if you want to run multiple commands like that, make bash be the entry point, and make all the other commands be an argument for bash to run:

command: ["/bin/bash","-c","touch /foo && echo 'here' && ls /"]

like image 135
Jordan Liggitt Avatar answered Oct 23 '22 05:10

Jordan Liggitt


Jordan's answer is correct.

But to improve readability I would prefer:

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:  # specification of the pod’s contents
  restartPolicy: Never
  containers:
  - name: hello
    image: "ubuntu:14.04"
    command: ["/bin/sh"]
    args:
      - -c
      - >-
          command1 arg1 arg2 &&
          command2 arg3 &&
          command3 arg4

Read this to understand YAML block scalar (The above >- format).

like image 42
Victor Wong Avatar answered Oct 23 '22 07:10

Victor Wong


use this command

command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]
like image 23
brian Avatar answered Oct 23 '22 07:10

brian


You could simply list the commands as you would normally deal with yaml arrays/lists. Take a look at this question on yaml array syntax.

Below is an example of how to pass argument lists to command. Please note the semicolon at the end of commands , otherwise you'll get an error.

  containers:
  - name: my-container
    image: my-image:latest
    imagePullPolicy: Always
    ports:
    - containerPort: 80
    command: [ "/bin/bash", "-c" ]
    args:
     - 
        echo "check if my service is running and run commands";
        while true; do
            service my-service status > /dev/null || service my-service start;
            if condition; then
                    echo "run commands";
            else
                    echo "run another command";
            fi;
        done
        echo "command completed, proceed ....";
like image 32
Zstack Avatar answered Oct 23 '22 05:10

Zstack