kubectl run --command vs -- arguments
Asked Answered
P

3

31

I was a little confused with below command:

kubectl run busybox --image=busybox --restart=Never -o yaml --dry-run -- /bin/sh -c 'echo hello;sleep 3600'

YAML:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - args:
    - /bin/sh
    - -c
    - echo hello;sleep 3600
    image: busybox
    name: busybox
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

I know if we don't specify parameter --command, then parameters after -- will be treated as arguments.

But I wanted to know, how /bin/sh -c "echo hello;sleep 3600" was working in case of arguments? According to Kubernetes documentation(https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#notes), If we specify only arguments in a POD then docker EntryPoint will be used as EntryPoint command. So the resultant command which will be executed in the docker image will be Docker EntryPoint + kubectl arguments.

As Busybox DockerFile does not contain any EntryPoint(https://github.com/docker-library/busybox/blob/master/musl/Dockerfile), so arguments specified in the kubectl command will only be used, so the command will look like:

/bin/sh -c 'echo hello;sleep 3600'

And if we specify --command, then according to Kubernetes documentation, DockerFile arguments(CMD) and command(EntryPoint) both will be overridden with command specified in the kubectl command, so it will look similar to above:

/bin/sh -c 'echo hello;sleep 3600'

So it would be same in the end.

Prothesis answered 9/12, 2019 at 11:50 Comment(0)
B
57

When working with containers in Kubernetes, you should be careful not to mix up Kubenetes command and Docker Cmd.

  • the command field in Kubernetes corresponds to the EntryPoint field in Docker
  • the args field in Kubernetes corresponds to the Cmd field in Docker

From Kubernets documentation:

When you override the default Entrypoint and Cmd, these rules apply:

  • If you do not supply command or args for a Container, the defaults defined in the Docker image are used.

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

  • If you supply a command and args, the default Entrypoint and the default Cmd defined in the Docker image are ignored. Your command is run with your args.

Bithynia answered 6/2, 2021 at 15:45 Comment(0)
H
12

To modify a command and parameter from the command line use the following command:

kubectl run nginx --image=nginx --command -- sleep 1000

Another example for the same:

kubectl run nginx --image=nginx --command -- python myapp.py

If you only want to overwrite a parameter (not the command itself) it would be:

kubectl run nginx --image=nginx -- 1000    #(sending the 1000 seconds to 
sleep)
Higdon answered 15/10, 2022 at 3:44 Comment(0)
P
6

In this example, yes both are same. Lets say if an entry point (command) is set as sleep 1000 but if your args are set as sleep 3000 then container command is ignored and sleep 3000 is executed.

Args takes precedence over command, and overrides command values if args exists

Peria answered 10/12, 2019 at 14:19 Comment(1)
Your last statement could be misinterpreted (Kubernetes command vs Docker Cmd). --> If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.Bithynia

© 2022 - 2024 — McMap. All rights reserved.