Node.js › Docker & Deployment

What is the difference between the CMD and ENTRYPOINT instructions in a Dockerfile for a Node.js application?

Intermediate

Answer

ENTRYPOINT defines the fixed, primary command that always runs when the container starts, while CMD provides default arguments to that entrypoint (or, if ENTRYPOINT isn't set, CMD itself becomes the full command) that can be easily overridden at 'docker run' time -- for a Node app, a common pattern uses ENTRYPOINT for the node binary itself and CMD for the specific script to run, letting a user override just the script without needing to override the whole command.
ENTRYPOINT ["node"]
CMD ["server.js"]

# Runs 'node server.js' by default
# docker run myapp                actually runs: node server.js
# docker run myapp worker.js       overrides CMD: node worker.js
Real-world example A Docker image built to run either the main API server or a separate background worker uses ENTRYPOINT ["node"] with CMD ["server.js"] as the default, letting the exact same image run the worker instead simply by overriding the command at deploy time with 'worker.js'.

Common follow-ups: What happens if both ENTRYPOINT and CMD are specified using the shell form rather than the exec (array) form?;Why is the exec (array) form generally preferred for both instructions?