The Dockerfile
CMD instruction supplies a default command or default arguments when a container starts. It does not execute during the image build; use RUN for build-time commands.Exec form
docker_snippet
Docker · 2 lines|
| FROM busybox |
| CMD ["/bin/echo", "This is the default"] |
Build and run:
bash_snippet
Bash · 2 lines|
| docker build -t cmd-demo . |
| docker run --rm cmd-demo |
Arguments after the image name replace the default
CMD:bash_snippet
Bash · 1 line|
| docker run --rm cmd-demo /bin/echo "Custom command" |
Docker options such as
--rm appear before the image name and do not replace CMD.Shell form
docker_snippet
Docker · 1 line|
| CMD echo "Hello" |
On Linux, shell form normally runs through
/bin/sh -c. Use exec form when you do not need shell expansion or pipelines, especially for predictable signal handling.Combining CMD and ENTRYPOINT
An exec-form
CMD can provide default arguments to an exec-form ENTRYPOINT:docker_snippet
Docker · 2 lines|
| ENTRYPOINT ["/bin/echo"] |
| CMD ["Hello"] |
docker run image Goodbye replaces the default argument and runs /bin/echo Goodbye. Only the last CMD in a Dockerfile takes effect.✦ 卷终 · 柯航存卷 ✦