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
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.
Lets see what we have in the scripts – “echo.sh” prints FROM CONTAINER and “run.sh” prints the list of files.
Time to build the image and run the container
Running the container AS IS
Running the container with “run.sh”