20

I have read the docs about CMD and ENTRYPOINT

https://docs.docker.com/engine/reference/builder/#entrypoint

Here, they have mentioned in the table that "NO CMD and NO ENTYRPOINT is not allowed", But I created a Dockerfile without CMD and ENTRYPOINT and the image was built successfully. Download alpine tar from here Alpine Tar

Dockerfile

from scratch 
ADD alpine-minirootfs-3.11.2-x86_64.tar.gz /
COPY . /

Building the image:

docker build -t test:1 .
Sending build context to Docker daemon  2.724MB
Step 1/3 : from scratch
-----
Successfully tagged test:1

docker run -ti test:1 /bin/sh
/ # 

It worked!! So why in the docs it's mentioned that either CMD or ENTRYPOINT is necessary?

Ashwani
  • 1,340
  • 1
  • 16
  • 34
  • Because it's necessary *if you just want to run the container*, and not have to explicitly pass in a command as you do in run. – jonrsharpe Jan 05 '20 at 12:15
  • 1
    No, in docs they have mentioned that it's an error. – Ashwani Jan 05 '20 at 12:16
  • 1
    Yes, as you can see if you just `docker run test:1`. It's an error *"when running a container"*, not when building it. – jonrsharpe Jan 05 '20 at 12:18
  • 1
    Note that it also says: ``1. Dockerfile should specify at least one of CMD or ENTRYPOINT commands.`` Should not must! I agree that that the first row in the table is a bit confusing – AAber Jan 05 '20 at 12:23
  • Yes, that's why I got confused and tried this example using Scratch image – Ashwani Jan 05 '20 at 12:24
  • Related discussions: https://stackoverflow.com/q/21553353/320399 , https://stackoverflow.com/q/37634483/320399 , and https://stackoverflow.com/a/37636548/320399 – blong May 15 '23 at 19:03

3 Answers3

15

Specifying a command at the end of the docker run command line supplies (or overrides) CMD; similarly, the docker run --entrypoint option supplies (or overrides) ENTRYPOINT. In your example you gave a command /bin/sh so there's something for the container to do; if you leave it off, you'll get an error.

As a matter of style your Dockerfiles should almost always declare a CMD, unless you're extending a base image that's already running the application automatically (nginx, tomcat). That will let you docker run the image and launch the application embedded in it without having to remember a more specific command-line invocation.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • No, in docs they have mentioned that it's an error if we don't specify CMD or ENTRYPOINT. – Ashwani Jan 05 '20 at 12:17
  • I know it's a "style" and we should either of them in Dockerfile but it's not must. – Ashwani Jan 05 '20 at 12:18
  • 2
    In [Understand how CMD and ENTRYPOINT interact](https://docs.docker.com/engine/reference/#understand-how-cmd-and-entrypoint-interact), read the four bullet points carefully: "Dockerfile _should_ specify at least one of `CMD` or `ENTRYPOINT` commands", "`CMD` will be overridden when running the container with alternative arguments". I believe you're in a `CMD [""]`, "No ENTRYPOINT" cell in the matrix. – David Maze Jan 05 '20 at 12:25
  • In the table it's mentioned that No CMD - No ENTRYPOINT -> error, not allowed – Ashwani Jan 05 '20 at 12:33
  • 5
    You have a `CMD` at the point you're in the table, it's the `/bin/sh` from the command line. – David Maze Jan 05 '20 at 12:34
8

The following line from documentation is incorrect.

Dockerfile should specify at least one of CMD or ENTRYPOINT commands.

It should probably say -

CMD or ENTRYPOINT is necessary for running a container.

Shashank V
  • 10,007
  • 2
  • 25
  • 41
  • Isn't that what it says? – samthegolden Jan 05 '20 at 13:16
  • It currently says `Dockerfile` should specify CMD or ENTRYPOINT and the table in referenced page says having no CMD or ENTRYPOINT in Dockerfile is `error, not allowed`. You can clearly build a docker image from Dockerfile without CMD or ENTRYPOINT as shown by OP. – Shashank V Jan 05 '20 at 13:19
  • 3
    The doc uses `should`, not to be confused with `must`. If there is a CMD and/or ENTRYPOINT in the base image (including possible defaults detailed in the doc), then there is no obligation to add one unless you need to override it. – Zeitounator Jan 05 '20 at 14:40
  • 3
    That's what the confusion is , it isn't necessary to have CMD or ENTRYPOINT. The statement in table is wrong. – Ashwani Jan 05 '20 at 16:29
2

CMD and ENTRYPOINT are both Dockerfile instructions used to specify the command that will be executed when a Docker container is run. ENTRYPOINT sets the main command to be executed every time the container starts and does not get overridden by the command line arguments. CMD, on the other hand, provides default arguments for the ENTRYPOINT or a default command that can be easily overridden by command line arguments at runtime. If used together, CMD parameters become additional arguments for the ENTRYPOINT command.

CMD/ENTRYPOINT gets inherited from base image

if ENTRYPOINT exists
  CMD is arguments to ENTRYPOINT
else if CMD exists
  CMD should have an executable and optionally arguments


CMD can be overridden at runtime as `docker run` arguments at the end
To override ENTRYPOINT, need to use `--entrypoint`

Even if there's no CMD or ENTRYPOINT neither in base images nor the final Dockerfile you still can run the container with docker run by specifying a command at the end.

Otherwise it's an error!

% docker inspect scratch:test | jq '.[].Config.Cmd, .[].Config.Entrypoint'
null
null

% docker run -it --rm --name scratch-test scratch:test /bin/sh
/ # exit

% docker run -it --rm --name scratch-test scratch:test
docker: Error response from daemon: No command specified.
See 'docker run --help'.
Shinto C V
  • 714
  • 1
  • 9
  • 16