CODE/2022-09-30

Understanding the Dockerfile CMD Instruction

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.
COMPANION ESSAYS / 思想脉络与互照篇目
同卷深读2026-05-22 ·

Installing Python 3 Alongside System Python on CentOS 7

同属「系统工程卷」脉络衍生思考与工程推演推演阅读 →
跨界互照2026-08-22 ·

我的书架:03|金融经管:经济运行、金融体系与企业治理

我的书架:03|金融经管:经济运行、金融体系与企业治理

跨领域对照思辨 · 从「思想随笔」视界互为印证推演阅读 →
RELATED INDEX / 相关阅读索引
柯航hang.ke·© 2014—2026
CC BY-NC 4.0·