john chen
john chen

Reputation: 85

Command and args in pod

There are many way to set command in pod.

1.
command: ["/bin/sh","-c","sleep 1000"]
2.
command: ["/bin/sh"]
args: ["-c","sleep 1000"]
3.
args:  ["/bin/sh","-c","sleep 1000"]

Are they the same or different?

Upvotes: 2

Views: 2891

Answers (2)

vinibali
vinibali

Reputation: 31

In case of multiple args in a Helm template you need to add them in separated lines. I even added a whitespace, IIRC that was needed for sure:

           args:
             - "--arg1"
             - " --arg2"

Upvotes: 0

David Maze
David Maze

Reputation: 158908

These are all almost the same; except that if the underlying image has a Docker ENTRYPOINT then the args: are passed to it as arguments in the third case, without command:.

In core Docker, there are two ways to specify the main container command, the Dockerfile ENTRYPOINT and CMD directives. Either or both of these can be overridden when the container starts, but resetting the ENTRYPOINT value always clears CMD. If both are present then the CMD is passed as arguments to the ENTRYPOINT.

Kubernetes has the same concept and uses the same underlying image structure, but its names are different:

  • Kubernetes command: overrides Docker ENTRYPOINT (and resets CMD)
  • Kubernetes args: overrides Docker CMD

So in the first case setting command: overrides the container entrypoint and resets the container command to an empty list; in the second you directly set both parts. In both cases the two lists are combined together and you get an identical 3-word command.

In the last case args: only overrides the container command part. If the image has a Dockerfile ENTRYPOINT then this list of arguments is passed as arguments to the entrypoint process.

Which one is "right" depends on how the image is constructed:

  • Some images use CMD to specify the main command to run and don't have ENTRYPOINT; in this case any of the forms you show will work.

    CMD the-main-program
    # override with either Kubernetes command: or args:
    
  • Some images use ENTRYPOINT to specify the main command to run (and depending on how they're constructed may completely ignore CMD). In this case you must use one of the first two forms with command:.

    ENTRYPOINT ["the-main-command"]
    CMD ["--argument", "value"]
    # override with Kubernetes command: and optionally args:
    
  • Some images use CMD to specify the main container command, and ENTRYPOINT as a wrapper script to do first-time setup. In this case you need the third form that sets only args: or else the entrypoint script will get skipped.

    ENTRYPOINT ["docker-entrypoint.sh"]
    CMD ["the-main-command"]
    # override with Kubernetes args: only
    

Upvotes: 5

Related Questions