What is entrypoint and cmd in the context of Docker?
sobes.tech AI
Answer from AI
ENTRYPOINT defines the command that will be executed when the container starts. If CMD or command parameters are passed during docker run, they are added as arguments to this command.
CMD provides default arguments for ENTRYPOINT or is executed as a separate command if ENTRYPOINT is not defined. If parameters are passed during container startup, they override the CMD value.
Differences:
| Feature | ENTRYPOINT | CMD |
|---|---|---|
| Purpose | Defines the main command to execute | Provides default arguments or command |
| Override | Requires docker run --entrypoint |
Overridden by arguments in docker run |
Usage with ENTRYPOINT |
Arguments for ENTRYPOINT |
Default command if ENTRYPOINT is not set |
Examples:
Using ENTRYPOINT and CMD:
# Dockerfile
FROM alpine:latest
ENTRYPOINT ["echo", "Hello"]
CMD ["World!"]
Running docker run your-image-name: Executes echo Hello World!
Running docker run your-image-name Universe!: Executes echo Hello Universe!
Using only CMD:
# Dockerfile
FROM alpine:latest
CMD ["echo", "Hello World!"]
Running docker run your-image-name: Executes echo Hello World!
Running docker run your-image-name echo Hello Docker!: Overrides CMD, executes echo Hello Docker!
It is recommended to use ENTRYPOINT for the executable file and CMD for passing default parameters to this executable.