What is the difference between CMD and ENTRYPOINT in a Dockerfile?
What is the difference between CMD and ENTRYPOINT in a Dockerfile?
In a Dockerfile, both CMD
and ENTRYPOINT
are instructions used to define the command that runs when a container starts, but they serve different purposes and have distinct behaviors.
CMD
instruction sets default commands and/or parameters that can be overridden from the Docker command line interface (CLI) when the container is run.docker run <image> <command>
), the CMD
instruction will be overridden by this command.CMD
can be written in two forms:
CMD command param1 param2
CMD ["executable", "param1", "param2"]
FROM ubuntu
CMD ["echo", "Hello, World!"]
docker run <image>
will execute echo Hello, World!
. However, running docker run <image> echo Goodbye
will override the CMD
and execute echo Goodbye
instead[1][3][4][5].ENTRYPOINT
instruction configures a container to run as an executable. It sets a command that will always be executed when the container starts.CMD
, the ENTRYPOINT
command cannot be overridden by the command line arguments provided when running the container. Instead, any arguments provided will be appended to the ENTRYPOINT
command.ENTRYPOINT
can also be written in two forms:
ENTRYPOINT command param1 param2
ENTRYPOINT ["executable", "param1", "param2"]
FROM ubuntu
ENTRYPOINT ["echo"]
CMD ["Hello, World!"]
docker run <image>
will execute echo Hello, World!
. Running docker run <image> Goodbye
will execute echo Goodbye
, appending the argument to the ENTRYPOINT
command[2][3][4][5][6].middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào