In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains why the docker container has been running. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "why docker containers are always running"!
When many people come into contact with docker for the first time, they have the illusion that running a container is the same as running a virtual machine. After docker run, the container has been in the running state. In fact, this understanding is completely wrong.
When container first gets up, users can specify the program to be executed when container starts through CMD,ENTRYPOINT in dockerfile, or directly followed by comand in docker run.
If the specified program is only a short task, such as echo sorry. So, after outputting "sorry", sorry,container exits. As you can see from docker inspect, the container state becomes Exited. Once in the Exited state, the following command is no longer work:
Both docker exec docker attach commands need to be in running!
It is clearly stated in the official documentation that docker start can be restarted, then it can be transferred to the running state.
However, after the container of start is started, the command of CMD will be executed again. Then it quickly finished the task and went into the Exited state to rest.
If you start a container and want to use it as a virtual machine (that is, use docker exec to run programs in the container internal environment), make sure that the container is always in the running state.
There are two situations to consider:
1. When container starts, CMD does not specify some service tasks.
2. Some important service are specified in CMD, such as web service.
In the second case, there is always a web server running in container, and as a service, this service will always run unless you actively stop it. As a result, the container will always be running, so there is no problem with executing docker exec.
In the first case, it is clear that container will Exited soon. There are at least two ways to keep it in running:
Since CMD does not specify any service, you can assume that the task specified by CMD is not important, and if it is, you can specify it through RUN when making dockerfile. So, we can modify CMD, such as
Docker run tail-f / dev/null
Among them, the program tail-f / dev/null will always block, without external signal, it will not quit, so container will always running.
After all, the above method may override CMD in dockerfile, and a better method is-- docker commit.
Docker run docker commit `docker ps-ql`docker stop `docker ps-ql`docker rm `docker ps-ql`docker run tail-f / dev/null
Where docker ps-ql is used to get the id of the recently created container. The disadvantage of this approach is that it creates an extra image, which takes up space. Considering the CI/CD situation, this shortcoming is almost unbearable.
At this point, I believe you have a deeper understanding of why the docker container has been running, might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.