What is the CMD instruction good for in Docker?
Experience Level: Senior
Tags: Docker
Answer
The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.
If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.
The CMD instruction has three forms:
Form 1 (exec form, this is the preferred form)
CMD ["executable","param1","param2"]
Form 2 (as default parameters to ENTRYPOINT)
CMD ["param1","param2"]
Form 3 (shell form)
CMD command param1 param2
Related Docker job interview questions
How do ENTRYPOINT and CMD instructions interact?
Docker SeniorHow many CMD instructions can be in a Dockerfile?
Docker SeniorWhat is the EXPOSE instruction good for in Docker?
Docker SeniorWhat is the VOLUME instruction good for in Docker?
Docker SeniorCan the WORKDIR instruction resolve environment variables in Docker?
Docker Senior