CMD and Entrypoint in Kubernetes

Unlike virtual machines, containers are not meant to host an operating system. Containers are meant to run a specific task or process such as to host an instance of a web server, application server, or database, or simply to carry out some kind of computation or analysis. once the task is complete, the container exits. A container only lives as long as the process inside it is alive. If the web service inside the container is stopped or crashes the container exits.

So who defines what process is run within the container?

CMD [“nginx”]

CMD which stands for command that defines the program that will be run within the container when it starts.

So how do you specify a different command to start the container?

One option is to append a command to the docker run command and that way it overrides the default command specified within the image.

docker run -d debian [COMMAND]
docker run -d debian sleep 300
docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                      NAMES
92c37d183255        debian                "sleep 300"              10 seconds ago      Up 9 seconds                                                   great_easley

ENTRYPOINT [“sleep”]

FROM debian

ENTRYPOINT ["sleep"]

Create Image

docker build -t debian-sleeper .
Step 1/2 : FROM debian
 ---> 6d6b00c22231
Step 2/2 : ENTRYPOINT ["sleep"]
 ---> Running in 9ee5332611d5
Removing intermediate container 9ee5332611d5
 ---> e862ec774ca2
Successfully built e862ec774ca2
Successfully tagged debian-sleeper:latest
docker run -d debian-sleepr 30

The container will sleep after 30 seconds because sleep command will be invoked automatically.

The entry point instruction is like the command instruction as in you can specify the program that will be run when the container starts and whatever you specify on the command line.

If you use append a command,

  • CMD instruction the command line parameters passed will get replaced entirely.
  • ENTRYPOINT instruction the command line parameters will get appended.

Configure a default value for the command

If one was not specified in the command line that is where you would use both entry point as well as the command instruction.

FROM debian

ENTRYPOINT ["sleep"]

CMD ["5"]

You must always specify the entrypoint and command instructions in a JSON format.

Also you can overried the entry point.

docker run --entrypoint sleep -d debian-sleeper 30
e48cf4f84ba202cec4a8d3fc0a3264a4419fbd863ca65ed3176ec47552ca70cd
jayjo@Jay-Mac ~ $ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                      NAMES
e48cf4f84ba2        debian-sleeper        "sleep 30"               3 seconds ago       Up 2 seconds    

How about in Kubernetes pod?

FROM ubuntu

ENTRYPOINT ["sleep"]

CMD ["5"]

Create image in private repository

docker build -t ubuntu-sleeper .

Create pod using this image.

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-sleeper-pod

spec:
  containers:
    - name: ubuntu-sleeper
      image: ubuntu-sleeper
      args: ["100"]
  imagePullSecrets:
  - name: regcred
  • you can specify the additional argument in the pod definition file. with args
  • https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/

Pull an Image from a Private Registry

kubectl create secret generic regcred \
    --from-file=.dockerconfigjson=/home/vagrant/.docker/config.json \
    --type=kubernetes.io/dockerconfigjson
secret/regcred created
kubectl create -f ubuntu-sleeper-pod.yaml
pod/ubuntu-sleeper-pod created

Overwrite ENTRYPOINT

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-sleeper-pod

spec:
  containers:
    - name: ubuntu-sleeper
      image: ubuntu-sleeper
      args: ["100"]
      imagePullPolicy: IfNotPresent
  imagePullSecrets:
  - name: regcred
  • Command field corresponds to entrypoint instruction in the Dockerfile.
Dockerfilepod definition file
ENTRYPOINT[“”]command[“”]
CMD[“5”]args:[“”]
Dockerfile corresponds to k8s definition file

Leave a Reply

Your email address will not be published.

ANOTE.DEV