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?
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.
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.
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.
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 /"]
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).
use this command
command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]
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 ....";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With