Menu

Dipesh Majumdar

Blog and Paintings

ENTRYPOINT and CMD of docker corresponds to command and args in k8s

Consider the Dockerfile - 

FROM ubuntu
#CMD sleep 5
#CMD ["sleep","30"]
ENTRYPOINT ["sleep"]
CMD ["35"]

Build above Dockerfile like this -

cd <to directory where the Dockerfile resides> 

docker build -t ubuntu_cust:v1  .

to execute the entrypoint and cmd of the above dockerfile simply run - 

docker run --name my_container ubuntu_cust:v1

if you want to overwrite the entrypoint and CMD as mentioned in the above dockerfile 

docker run --name=my_container --entrypoint grep  ubuntu_cust:v1 root /etc/passwd

docker rm my_container

docker run --name=my_container --entrypoint cat  ubuntu_cust:v1  /etc/passwd

docker rm my_container

##even if you only specify entrypoint it will overwrite Dockerfile CMD with a blank command... i mean nothing so when you spin up the container it only runs: ls (as shown below)

docker run --name my_container --entrypoint ls ubuntu_cust:v1 

docker rm my_container

now I don't want to over-write the ENTRYPOINT but only the command after the entrypoint... here is how to do it:

docker run --name my_container ubuntu_cust:v1 5

Now i want to use the customized image - ubuntu_cust:v1 in a pod

Consider the pod template - 

apiVersion: v1
kind: Pod
metadata:
 name: mypod1
 labels:
   os: ubuntu
   app: mypod1
spec:
 containers:
 - name: mycontainer
   image: ubuntu_cust:v1
   command: ["cat"]
   args: ["/etc/passwd"]

kubectl -n mns create -f pod.yaml

 

[dipesh.majumdar@demo ubunutu]$ kubectl -n mns logs mypod1 -f
Error from server (BadRequest): container "mycontainer" in pod "mypod1" is waiting to start: trying and failing to pull image

Do a describe and you can see - 

Failed to pull image "ubuntu_cust:v1": rpc error: code = Unknown desc = Error response from daemon: repository ubuntu_cust not found: does not exist or no pull access

 

The image exists in my local - but not in docker-hub, so it's not getting the image. Now i need to push my local image to docker hub or any other container registry. Lets push it to google cloud registry. 

First we need to configure docker to use gcloud command line tool to authenticate requests to container registry (one time activity)

gcloud auth configure-docker

now i need to tag the image to make it compliant with google cloud registry. 

This is how docker images for gcloud registry will have to be named - 

[HOSTNAME]/[PROJECT-ID]/[IMAGE]

docker tag ubuntu_cust:v1 eu.gcr.io/dipeshm1206/ubuntu_cust:v1

[dipesh.majumdar@demo ubunutu]$ docker push eu.gcr.io/dipeshm1206/ubuntu_cust:v1
The push refers to a repository [eu.gcr.io/dipeshm1206/ubuntu_cust]
b57c79f4a9f3: Layer already exists
d60e01b37e74: Layer already exists
e45cfbc98a50: Layer already exists
762d8e1a6054: Layer already exists
v1: digest: sha256:ff44cb098fcbb01682a758c2bd97edd4c492e4bc282ac83617633f5796e79b08 size: 1150

 

Now Let me use the pod manifest this way - 

[dipesh.majumdar@demo ubunutu]$ cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
 name: mypod1
 labels:
   os: ubuntu
   app: mypod1
spec:
 containers:
 - name: mycontainer
   image: eu.gcr.io/dipeshm1206/ubuntu_cust:v1
   command: ["cat"]
   args: ["/etc/passwd"]

 

[dipesh.majumdar@demo ubunutu]$ kubectl -n mns create -f pod.yaml
pod/mypod1 created
[dipesh.majumdar@demo ubunutu]$ kubectl -n mns get po
NAME                 READY   STATUS      RESTARTS   AGE
mypod1               0/1     Completed   0          8s

 

[dipesh.majumdar@demo ubunutu]$ kubectl -n mns logs mypod1 -f
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin

(more information here - https://cloud.google.com/container-registry/docs/quickstart

 

Go Back

Comment