Dockerfile Directives

ENTRYPOINT vs CMD in Dockerfile: Differences and Best Practices

Understand the fundamental differences between ENTRYPOINT and CMD directives and how to combine them for flexible container CLIs.

Quick Docker Solution
Safe CommandIntermediate
ENTRYPOINT ["node", "server.js"]
CMD ["--port", "3000"]
When to use this scenario:You are writing a Dockerfile and need to know whether to use ENTRYPOINT or CMD to start your application.

Step-by-Step Execution Guide

1Exec Form (Always Recommended)
ENTRYPOINT ["executable", "param1", "param2"]

JSON array syntax does not invoke a shell wrapper, allowing the process to receive SIGTERM signals properly (PID 1).

2Shell Form (Avoid if possible)
CMD executable param1 param2

Runs command as "/bin/sh -c executable", which prevents signals from reaching your app.

3Recommended Combination Pattern
ENTRYPOINT ["python", "main.py"]
CMD ["--serve"]

ENTRYPOINT defines the fixed executable, while CMD provides default arguments that users can override at "docker run".

Critical Docker Pitfalls & Precautions

  • β€’If you override CMD when running "docker run image:tag custom_arg", only CMD is replaced; ENTRYPOINT remains active.
  • β€’To completely override ENTRYPOINT, use "docker run --entrypoint /bin/sh image:tag".
Frequently Asked Questions

ENTRYPOINT vs CMD Explained - Frequently Asked Questions

Common questions about container isolation, signal handling, and runtime behavior.

Use ENTRYPOINT ["node", "index.js"] or CMD ["node", "index.js"] in exec form (JSON array). Combining them allows passing default port or config flags.