The difference between the cmd command and the entrypoint command in the Dockerfile of the docker series

1. cmd:

What cmd gives is the default executable of a container. That is, after the container is started, the command executed by default. The point is this "default". It means that if docker run does not specify any execution command or there is no entrypoint in the dockerfile, then the default execution command specified by cmd will be used for execution. At the same time, it also explains the meaning of entrypoint from the side, which is the real command to be executed after the container starts.

The CMD instruction has three forms:
 
CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)

Usage 1: Form with square brackets

form with square brackets. At this time, the command is not in any shell terminal environment. If we want to execute the shell, we must add the shell to the parameters in the square brackets. This usage is like an exec function in C language, which means that we want to execute a process. If a non-shell method is used, the above example should be modified to:

FROM centos
 
CMD ["/bin/bash", "-c", "echo 'hello cmd!'"]

Note: If the form of square brackets is used, the first parameter must be the full path of the command. Moreover, a dockerfile can only have at most one cmd, if there are multiple, only the last one will take effect. The official website recommends this method.

Of course, the above all reflect the "default" behavior of cmd. If we specify a command or have an entrypoint at run time, cmd will be overwritten. Still the image above. The run command has changed:
insert image description here
As you can see, the final container executes the command after the run command, not the command defined in cmd.

Usage 2: shell form, that is, the form without square brackets

Then the command is executed under "/bin/sh -c" by default. For example, the following dockerfile:

FROM centos
 
CMD echo "hello cmd!"

run:
insert image description here

Note: If the form of square brackets is used, the first parameter must be the full path of the command. Moreover, a dockerfile can only have at most one cmd, if there are multiple, only the last one will take effect.

Two, entry point:

The entrypoint is orthodoxly used to define the execution body after the container is started. In fact, we can also understand from the name that this is the "entry" of the container.
There are two usages: command line and shell.

ENTRYPOINT has two forms:
 
ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
ENTRYPOINT command param1 param2 (shell form)

The first type: command line mode, that is, with square brackets

It is consistent with the square bracket form of cmd, but here it seems to be executed in the shell environment, which is different from cmd. If there is something after the run command, then all the latter will be used as parameters of the entrypoint. If there is no extra thing after run, but cmd has, then the entire content of cmd will be used as the parameter of entrypoint, which is also the second usage of cmd. This is also the entrypoint that is said on the Internet will not be covered. Of course, if you want to overwrite it in run, there is a way, just use –entrypoint.

For example:
dockerfile is:

FROM centos
 
CMD ["p in cmd"]
ENTRYPOINT ["echo"]

If run does not take parameters:
insert image description here
If run takes parameters:
insert image description here
Moreover, in the bracket form of entrypoint, the command is run in the shell environment, otherwise the echo here cannot be executed.

The second type: shell mode

In this mode, any parameters of run and cmd cannot be passed into entrypoint. The official website recommends the first usage.
For example:

FROM centos
 
CMD ["p in cmd"]
ENTRYPOINT echo

insert image description here

3. Summary:

Generally, the square bracket form of entrypoint is used as the default execution command after the docker container is started, and the invariant part is placed in it. The variable part, such as command parameters, can be provided in the form of cmd to provide the default version, that is, there are no parameters in run The default parameters used when . If we want to use default parameters, just run directly, otherwise we want to use other parameters, just add parameters in run.

Guess you like

Origin blog.csdn.net/u014212540/article/details/130621158