SoFunction
Updated on 2025-04-11

Interpret whether CMD and ENTRYPOINT can be used in Dockerfile

Can CMD and ENTRYPOINT be used in Dockerfile?

In the Dockerfile,CMDandENTRYPOINTare two different instructions that can be used alone or in combination.

The CMD directive is used to specify the commands executed by default when the container starts.

It can take three forms:

  1. CMD ["executable","param1","param2"](Recommended): Use JSON array to specify commands and parameters.
  2. CMD command param1 param2: Specify commands and parameters using the command string form.
  3. CMD ["param1","param2"]: Provide default parameters to the ENTRYPOINT instruction.

The ENTRYPOINT directive is used to configure commands executed at container startup

It can also take three forms:

  1. ENTRYPOINT ["executable", "param1", "param2"](Recommended): Use JSON array to specify commands and parameters.
  2. ENTRYPOINT command param1 param2: Specify commands and parameters using the command string form.
  3. ENTRYPOINT ["param1", "param2"]: As a default parameter, used in conjunction with CMD.

whenCMDandENTRYPOINTWhen directives exist in Dockerfiles, their behaviors are combined.

Specifically:

  • If not specifiedCMD, but specifiedENTRYPOINT, it will be executed when the container startsENTRYPOINTThe specified command can be passeddocker runThe command parameters are provided.
  • If both are specifiedCMDandENTRYPOINT,butCMDThe content will beENTRYPOINTThe default parameters of the command. Can be passeddocker runCommand parameters to overrideCMDdefault parameters in .

useCMDandENTRYPOINTThe startup commands and parameters of the container can be flexibly defined as needed.

Depending on the actual needs, you can use one of the instructions alone, or combine them to meet specific needs.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.