Search code examples
linuxdockerdockerfilecontainersdocker-container

How hello world container is printing the welcome text?


I have read somewhere that the hello-world docker image is from scratch so it doesn't have a shell then how it is executing the hello executable file ? Don't we need a shell to execute a file ?


Solution

  • Because the docker CMD is designed to run executable.

    CMD

    The CMD instruction has three forms:

    CMD ["executable","param1","param2"] (exec form, this is the preferred form)
    CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
    CMD command param1 param2 (shell form)
    

    There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

    The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

    So CMD and entrypoint run executable you do not need shell. you will get same response if you run

    docker run -it --entrypoint ./hello --rm hello-world
    docker run -it --rm hello-world