Kubernetes - Passing multiple commands to the container

Kubernetes

Kubernetes Problem Overview


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?

Kubernetes Solutions


Solution 1 - Kubernetes

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

Solution 2 - Kubernetes

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

Solution 3 - Kubernetes

use this command

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

Solution 4 - Kubernetes

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

Solution 5 - Kubernetes

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

Solution 6 - Kubernetes

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDimuthuView Question on Stackoverflow
Solution 1 - KubernetesJordan LiggittView Answer on Stackoverflow
Solution 2 - KubernetesVictor WongView Answer on Stackoverflow
Solution 3 - KubernetesbrianView Answer on Stackoverflow
Solution 4 - KubernetesZstackView Answer on Stackoverflow
Solution 5 - KubernetesSMRITI MAHESHWARIView Answer on Stackoverflow
Solution 6 - Kubernetesns94View Answer on Stackoverflow