Difference [Posts] Dockerfile in the CMD and ENTRYPOINT

Dockerfile difference in ENTRYPOINT with the CMD

HTTPS: // it.baiked.com/system/docker/1975.html 

simple difference

 

Dockerfile There are two boot configuration, CMD and ENTRYPOINT, configuration commands can be executed automatically when the vessel started Dockerfile in, but what difference does both, then summarized as follows:

First look at the CMD, there are three formats of configuration, are:

  • CMD [ "executable", "param1", "param2"] (exec format, the format recommended)
  • CMD [ "param1", "param2"] (as ENTRYPOINT default parameters)
  • CMD command param1 param2 (shell format)

In use, should pay attention to the problem:

  1. When contains an executable file, execute the command specified as the default container, this is the main purpose;
  2. When that does not contain an executable file, it must provide ENTRYPOINT configuration, CMD as the default parameters;
  3. In only one CMD Dockerfile onset, if a plurality appears, the last onset;

And ENTRYPOINT, there are two formats configured, namely:

  • ENTRYPOINT [ "executable", "param1", "param2"] (exec format is recommended)
  • ENTRYPOINT command param1 param2 (shell format)

CMD and ENTRYPOINT format is the difference between the two configurations:

  • exec format this format is recommended when using this mode, you do not need to shell process, so that the executable program Docker applications become PID 1 process container, Unix can receive signals, such as the implementation of  docker  when the stop can receive SIGTERM.
  • shell format will always invoke a shell process, become the / bin / sh -c sub-command, Unix executable program can not respond to the signal.

Note: When using the shell ENTRYPOINT format, ignore CMD and run the incoming parameters, if you want to replace the default ENTRYPOINT command, you need to specify --entrypoint parameters in the implementation of docker run.

When using the CMD and ENTRYPOINT summarized as follows:

  • In Dockerfile, the CMD and should specify at least one EntryPoint;
  • When Docker as an executable program, you should use ENTRYPOINT be configured;
  • ENTRYPOINT CMD can be used as default parameters or as the default command Docker;
  • CMD can be covered docker run parameters passed;
  • After docker run the incoming parameters will be appended to the ENTRYPOINT, provided that the use of exec format.

Finally, we look at an example:

ENTRYPOINT use exec format, ENTRYPOINT the argument always used, and CMD additional parameters can be dynamically replaced when the container starts.

Dockerfile example, the following fragment:

ENTRYPOINT ["/bin/echo", "Hello"] 
CMD ["world"]

When activated by the container docker run -it [image], output:

$ docker run -it [image]
Hello world

If -it [image] CloudMan initiated by docker run, the output is:

$ docker run -it [image] CloudMan
Hello CloudMan

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11271590.html