Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 789
Ashwani Avatar asked Jan 05 '20 12:01

Ashwani


People also ask

Should I use ENTRYPOINT or CMD?

Prefer ENTRYPOINT to CMD when building executable Docker images and you need a command always to be executed. Additionally, use CMD if you need to provide extra default arguments that could be overwritten from the command line when the docker container runs.

Can I have ENTRYPOINT and CMD in Dockerfile?

#6 Using ENTRYPOINT with CMD Still, they both can be used in your Docker file. There are many such cases where we can use both ENTRYPOINT and CMD. The thing is that you will have to define the executable with the ENTRYPOINT and the default parameters using the CMD command. Maintain them in exec form at all times.

Which instruction is mandatory in a Dockerfile?

WORKDIR. You can specify your working directory inside the container using the WORKDIR instruction. Any other instruction after that in the dockerfile, will be executed on that particular working directory only.

What is the use of CMD in Dockerfile?

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.


2 Answers

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.

like image 126
David Maze Avatar answered Sep 18 '22 14:09

David Maze


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.

like image 43
Shashank V Avatar answered Sep 20 '22 14:09

Shashank V