Docker – difference between CMD and ENTRYPOINT

The CMD line in a Dockerfile defines the default command to run when a container is launched. If the command requires arguments then you need to use an array, for example [“cmd”, “-a”, “arga value”, “-b”, “argb-value”], which will be combined together and the command cmd -a arga value -b argb-valuewould be run.

An alternative approach to CMD is ENTRYPOINT. While a CMD can be overridden when the container starts, a ENTRYPOINT defines a command which can have arguments passed to it when the container launches. The default ENTRYPOINT is “/bin/sh -c” so even if we don’t define in the Dockerfile.

Lets do this with an example

Docker_CMD_ENT1.JPG

Dockerfile – Using the busybox as base image I have copied the scripts. Here the entrypoint is “shell” which is executed always. Next line is command “echo.sh” so if we do not override it then “echo.sh” will be executed.

Docker_CMD_ENT2.JPG

Lets see what we have in the scripts – “echo.sh” prints FROM CONTAINER and “run.sh” prints the list of files.

Docker_CMD_ENT3.JPG

Time to build the image and run the container

Docker_CMD_ENT4.JPG

Running the container AS IS

Docker_CMD_ENT5.JPG

Running the container with “run.sh”

Docker_CMD_ENT6.JPG

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s