In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use CMD instructions in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use CMD instructions in Linux".
There can be only one CMD instruction in Dockerfile. If more than one is listed, only the last CMD of the CMD will take effect. The main purpose of CMD is to provide default values for running the container.
Docker is either a virtual machine, a container is a process, and the CMD instruction is used to specify the default startup command of the container main process. When you start (run) a container, you can specify a new command to replace the default command in the mirror settings.
Executable files can be included or omitted. The format of the CMD instruction is similar to that of RUN, but there are also two formats:
Shell format: CMD exec format: CMD [executable file "," parameter 1 "," parameter 2 ".] Parameter list format: CMD ["Parameter 1", "Parameter 2".]. After the ENTRYPOINT instruction is specified, specify the specific parameters with CMD.
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 default command for the image.
If you leave a message at the bottom of the article, you can listen to the course and receive all the video tutorials such as Qianfeng HTML5, UI interaction design, PHP, Java+ cloud data, big data development, VR/AR/Unity game development, Python artificial intelligence, Linux cloud computing, full stack software testing, network security and so on.
Docker is not a virtual machine, and there is no concept of backend service in the container. Don't expect to start a program to the background like this:
CMD systemctl start nginx
This line is understood by Docker as:
CMD ["sh"- c"systemctl start nginx"]
For the container, its startup program is the application process of the container, and the container exists for the main process. When the main process exits, the container loses the meaning of existence and thus exits. Other auxiliary processes are not the things it needs to care about.
As in the example above, the main process is sh, so when the service nginx start command ends, sh ends, and sh exits as the main process, which naturally exits the container.
The right thing to do is to execute the executable file nginx directly and turn off the background daemon to make the program run in the foreground.
CMD ["nginx", "- g", "daemon off;"]
ENTRYPOINT instruction
The purpose of ENTRYPOINT, like CMD, is to specify the startup program and parameters of the container.
ENTRYPOINT can also be replaced at run time, but it is slightly more cumbersome than CMD and needs to be specified by the parameter entrypoint of docker run.
The format of ENTRYPOINT is the same as the RUN instruction format, which is also divided into exec format and shell format.
When ENTRYPOINT is specified, the meaning of CMD changes. Instead of running its command directly, it passes the contents of CMD as parameters to the ENTRYPOINT instruction, that is, when actually executed, it will become:
"
Why do you still have ENTRYPOINT when you have CMD?
Does this kind of "" bring us any benefits?
Let's take a look at some scenes.
Scenario 1: let the mirror be used like a command
CMD mode
FROM centos
RUN yum update\
& & yum install-y curl
CMD ["curl", "- s", "http://ip.cn"]
After building the image, run the container
# docker run-rm centos-echo-ip-cmd
An error will be reported if you execute the following command
# docker run-rm centos-echo-ip-cmd-I
We can see the error report, executable file not found. As we said earlier, the image name is followed by command, and the runtime replaces the default value of CMD. So the-I here is not added after the original curl-s http://ip.cn. Instead, it replaced the original CMD and became CMD ["- I"], and-I was not a command at all, so the executable file could not be found.
So we should use the ENTRYPOINT method.
FROM centos
RUN yum install-y curl
ENTRYPOINT ["curl", "- s", "http://ip.cn"]"
After building the image again, run the container
# docker run-rm centos-echo-ip-entrypoint
# docker run-rm centos-echo-ip-entrypoint-I
In this case, the final instruction becomes ENTRYPOINT ["curl", "- s", "http://ip.cn","-I "]
Scenario 2: preparation before the application runs
To start the container is to start the main process, but sometimes it takes some preparation before starting the main process.
Example in the official image redis:
FROM alpine:3.4
RUN addgroup-S redis & & adduser-S-G redis redis
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 6379
CMD ["redis-server"]
You can see that the redis user is created for the redis service, and finally ENTRYPOINT is specified as the docker-entrypoint.sh script.
#! / bin/sh
# allow the container to be started with `--user`
If ["$1" = 'redis-server'-a "$(id-u)" =' 0']; then
Chown-R redis.
Exec gosu redis "$0"$@"
Fi
Exec "$@"
The content of the script is based on the content of the CMD. If it is redis-server, switch to the redis user identity to start the server, otherwise it will still be executed using the root identity. For example:
$docker run-it redis id
Uid=0 (root) gid=0 (root) groups=0 (root)
And the ENTRYPOINT instruction will not be overwritten by the RUN instruction, while the CMD instruction will be overwritten by the RUN instruction.
The above is all the contents of the article "how to use CMD instructions in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.