Docker & Containers (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is the difference between `CMD` and `ENTRYPOINT` in Docker?
junior LevelMost AskedCode ExampleDocker & Containers
In Simple Words:
ENTRYPOINT: Defines the core executable that will always run when the container starts. It turns the container into an executable CLI tool.CMD: Provides default arguments or parameters to theENTRYPOINT. IfENTRYPOINTis not defined,CMDacts as the default command.- Overriding behavior:
CMDis easily overridden by passing arguments at the end ofdocker run.ENTRYPOINTcannot be overridden unless you explicitly pass the--entrypointflag.
dockerfile
# Pattern: Combining ENTRYPOINT and CMD
ENTRYPOINT ["curl"]
CMD ["https://api.github.com"]
# Running without args: runs 'curl https://api.github.com'
$ docker run my-curl-tool
# Overriding CMD with custom URL: runs 'curl https://example.com'
$ docker run my-curl-tool https://example.com