Ashwani
Ashwani

Reputation: 1480

Is CMD or ENTRYPOINT necessary to mention in Dockerfile?

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?

Upvotes: 23

Views: 27050

Answers (3)

Shinto C V
Shinto C V

Reputation: 774

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'.

Upvotes: 2

Shashank V
Shashank V

Reputation: 11183

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.

Upvotes: 13

David Maze
David Maze

Reputation: 158647

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.

Upvotes: 17

Related Questions