Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CMD and ENTRYPOINT

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use CMD and ENTRYPOINT". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

CMD

There are three forms of CMD instructions:

CMD ["executable", "param1", "param2"] (exec form, which is the preferred form)

CMD ["param1", "param2"] (as the default parameter for ENTRYPOINT)

CMD command param1 param2 (shell form)

There can be only one CMD instruction in Dockerfile. If there is more than one CMD, only the last CMD will take effect.

The main purpose of CMD is to provide default values for the executing container. These default values can include or omit executables. The ENTRYPOINT directive must be specified when the executable is omitted.

Note: if CMD is used to provide default parameters for ENTRYPOINT instructions, both CMD and ENTRYPOINT instructions should be specified in JSON array format.

Note: the exec form will be parsed to an JSON array, which means that you must use double quotation marks instead of single quotation marks for words.

Note: unlike the shell form, the exec form does not call the command shell. This means that normal shell processing will not work. For example, CMD ["echo", "$HOME"] does not perform variable substitution on $HOME. If you want the shell processing to take effect, either use the shell form or execute the shell directly, for example: CMD ["sh", "- c", "echo $HOME"]. When you use the exec form and execute shell directly, just like the shell form, the environment variable extension is performed by shell, not docker.

When using the shell or exec format, CMD sets the command to execute when running the mirror.

If you use the shell form of CMD, it will be executed in / bin/sh-c:

FROM ubuntuCMD echo "This is a test." | wc-

If you don't want to run as shell, you must represent the command as an array of JSON and give the full path to the executable. It is recommended that you give priority to the array form of the CMD command. Any additional parameters must be represented separately as strings in the array:

FROM ubuntuCMD ["/ usr/bin/wc", "--help"]

If you want the container to run the same executable file each time, you should consider using ENTRYPOINT in conjunction with CMD.

If the user specifies the parameter of docker run, the default value specified in CMD will be overridden by the parameter of run.

Note: don't confuse RUN with CMD. RUN actually runs a command and submits the result; CMD does nothing at build time, but specifies the expected command for the mirror.

ENTRYPOINT

There are two forms of ENTRYPOINT:

ENTRYPOINT ["executable", "param1", "param2"] (exec form, which is the preferred form)

ENTRYPOINT command param1 param2 (shell form)

ENTRYPOINT allows you to configure a container that will run as an executable.

That's all for "how to use CMD and ENTRYPOINT". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report