Docker - RUN vs ENTRYPOINT vs CMD

 



RUN - Specify commands to make changes to your Image and subsequently the Containers started from this Image. This includes updating packages, installing software, adding users, creating an initial database, setting up certificates, etc. These are the commands you would run at the command line to install and configure your application.

Let's start with a basic dockerfile, where we are building an image with ubuntu as base image.



Let's build the image.


The size of the image which we build is "78.1" MB.

I am going to install packages like "curl", "wget" on the base image.



Now, the image size has increased from 78MB to 136MB. RUN command add another layer to the base image.

RUN is primarily used for software installation and any configuration changes.

ENTRYPOINT - Must be used if you want to start or a define a container main application or command. It always runs regardless of an additional CMD parameters.


This image is build using base "alpine" with entry point "ls". So, when the container starts it list files and folders.


Now, we can see the moment container starts it does list.

Can we pass argument to ENTRYPOINT? YES

Arguments can be passed when creating container $ docker run <image> <parameter>

#

SSSame argument can passed via CMD parameter.



It does the same. 

CMD - It does the same as ENTRYPOINT, but it can be overridden with the argument.



We are passing "ls" as an argument. This can be also implemented via CMD.



What happens if I used ENTRPOINT and CMD together?


WWhen we use both, CMD is passed as an argument to ENTRYPOINT.  So, this is what gets executed when the container starts # ls -lrt ls 


Above error is expected. Let's tune the file docker file.



This gets translated as # ls -lrt etc/
sd
      Another example.





A













S










Comments

Popular posts from this blog

K8s - ETCD

SRE/DevOps Syllabus

K8s - Deployment and HPA replicas