Kubernetes and Docker CMD vs Entrypoint

August 8, 2018   

Docker

CMD simply sets a command to run in the image if no arguments are passed to docker run, while ENTRYPOINT is meant to make your image behave like a binary. The rules are essentially:

  • If your Dockerfile uses only CMD, the provided command will be run if no arguments are passed to docker run
  • If your Dockerfile uses only ENTRYPOINT, the arguments passed to docker run will always be passed to the entrypoint; the entrypoint will be run if no arguments are passed to docker run
  • If your Dockerfile declares both ENTRYPOINT and CMD, and no arguments are passed to docker run, then the argument(s) to CMD will be passed to the declared entrypoint

(this second is a shameless copy-paste from 1 below)

Kubernetes

Kubernetes provides the “command” and “args”.

  • command == Dockers ENTRYPOINT
  • args == Dockers CMD

What happens when both are defined?

Image Entrypoint Image Cmd Container command Container args Command run
[/ep-1] [foo bar] <not set> <not set> [ep-1 foo bar]
[/ep-1] [foo bar] [/ep-2] <not set> [ep-2]
[/ep-1] [foo bar] <not set> [zoo boo] [ep-1 zoo boo]
[/ep-1] [foo bar] [/ep-2] [zoo boo] [ep-2 zoo boo]

References

  1. http://www.projectatomic.io/docs/docker-image-author-guidance/
  2. https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/