简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM