Solution docker run or docker restart boot image automatically exits
- 2021-09-16 08:31:20
- OfStack
Execute the command: docker run--name centos8--d centos/bin/bash. Check the running container through docker ps and cannot find centos8.
Looking through docker ps-a, it is found that the centos8 container is already in a stopped state
[root@MiWiFi-R4A-srv server]$ docker run --name centos8 -d centos /bin/bash
a770630ca865b3c3346a321a383f302ed22af9281be8482f4f4debc59218d0d1
[root@MiWiFi-R4A-srv server]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@MiWiFi-R4A-srv server]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a770630ca865 centos "/bin/bash" 37 seconds ago Exited (0) 35 seconds ago centos8
Why quit?
This is because docker runs in the background and must have 1 foreground process. docker runs commands that are not suspended by 1 (e. g. top, ping) that automatically exit. In the code above-d centos is the specified command that needs to be executed. If the command is executed or the application is terminated, the container will automatically stop.
Solution
Run as a preceding process
The program to be run will run as a foreground process, and if the container needs to start multiple processes at the same time, only one of them needs to be suspended to the foreground.
For example, the centos container mentioned above only needs to modify the startup instruction to start interactively:
docker run --name centos8 -it centos /bin/bash
Or the Web container:
service php5-fpm start & & nginx -g "daemon off;"
A tricky way
Add a program similar to tail top, which can be run in the foreground, and continuously output log files.
service nginx start & & service php5-fpm start & & tail -f /var/log/nginx/error.log
Taking the web container mentioned above as an example, it can be written as:
service nginx start & & service php5-fpm start & & tail -f /var/log/nginx/error.log
Write your own script
When starting the centos/ubuntu container, you can do one hand and foot: do one infinite loop and continuously output any, so that the container will not think there is nothing to do and commit suicide.
docker run -d centos /bin/bash -c "while true; do echo hello world; sleep 1; done"
Additional knowledge: Solution to the unexpected symbol if [[in the sh script called by CMD when starting the container in docker
Recently, the mirror image was written through Dockerfile, and the sh script (start. sh) was called in CMD to start the container. When starting through docker run, the startup was unsuccessful, and the docker logs container ID was carried out, and the error was found to be: unexpected symbol if [[
I successfully executed start. sh directly in the external linux system call, entered the container through docker exec, and did not report an error when calling start. sh in the capacity. All the programs that need to be started in the command started successfully. Script direct execution is no problem, through docker run but error, puzzled.
The error reporting code segment in start. sh is as follows
if [[ -e /home/dc/testnn-aaa.zip ]];then
rm -rf /home/testnn-aaa
unzip -q -o -d /home testnn-aaa.zip
echo "unzip zip finished"
else
echo "dc zip not exist"
fi
After consulting and comparing, because my start. sh also has if statement before the error statement, but no error is reported, it is found that the difference between the two places is that the first if uses a single []. If the code is changed to [], it will be successful, and the modification is as follows:
if [ -e /home/dc/testnn-aaa.zip ];then