Dimuthu
Dimuthu

Reputation: 8576

Kubernetes - Passing multiple commands to the container

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?

Upvotes: 80

Views: 116211

Answers (6)

ns15
ns15

Reputation: 8684

Expanding on the correct answer from Jordon

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

Docker wants a single entrypoint in a container and this entrypoint should start one foreground process. This is why we need to use /bin/bash -c.

/bin/bash -c here will start one new process for a new shell and execute rest of the commands. This way container runtime can watch the single process and report if the container finishes gracefully.

Upvotes: 1

SMRITI MAHESHWARI
SMRITI MAHESHWARI

Reputation: 93

Another example with multiple bash commands for busybox image. This will run continuously with a while loop otherwise generally busybox images with simple scripts complete the task and the pod will get shut down after that. This yaml will run the pod continuously.

apiVersion: v1
kind: Pod
metadata:
labels:
  run: busybox
  name: busybox
spec:
  containers:
  - command:
    - /bin/sh
    - -c
    - |
      echo "running below scripts"
      i=0; 
      while true; 
      do 
        echo "$i: $(date)"; 
        i=$((i+1)); 
        sleep 1; 
      done
  name: busybox
  image: busybox

Upvotes: 4

Zstack
Zstack

Reputation: 4733

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 ....";

Upvotes: 10

Victor Wong
Victor Wong

Reputation: 3761

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).

Upvotes: 84

brian
brian

Reputation: 289

use this command

command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]

Upvotes: 28

Jordan Liggitt
Jordan Liggitt

Reputation: 18111

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

Upvotes: 102

Related Questions