Solve the problem that docker run or docker restart will automatically exit when starting the image

Solve the problem that docker run or docker restart will automatically exit when starting the image

Execute the command: docker run --name centos8 -d centos /bin/bash. Use docker ps to view the running container and cannot find centos8.

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 there must be a foreground process. If the command run by docker is not one that is always hung (eg top, ping), it will automatically exit. In the above code, -d centos is the specified command that needs to be executed. When the command is executed or the application terminates, the container will automatically stop.

Workaround

Run as a foreground process

The running program will be run as a foreground process. If the container needs to start multiple processes at the same time, you only need to suspend one of them to the foreground.

For example, for the centos container mentioned above, you only need to modify the startup command to start in interactive mode:

docker run --name centos8 -it centos /bin/bash

Or a web container:

service php5-fpm start && nginx -g "daemon off;"

Tips

Add a program like tail top that can run in the foreground to 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 something: create an infinite loop and continuously output anything, so that the container will not think that there is nothing to do and commit suicide.

docker run -d centos /bin/bash -c "while true; do echo hello world; sleep 1; done"

Supplementary knowledge: When starting a container in docker, the if command in the sh script called by CMD reports an unexpected symbol if[[. Solution

Recently, I wrote an image through Dockerfile. The sh script (start.sh) was called in CMD to start the container. When it was started through docker run, the startup failed. I performed docker logs container ID and found that the error was: Unexpected symbol if [[

I successfully executed start.sh directly in the external Linux system. I entered the container through docker exec and called start.sh in the container without any errors. All the programs that needed to be started in the command were successfully started. There is no problem in executing the script directly, but an error occurs through docker run, which is puzzling.

The error 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 checking and comparing, because my start.sh also has an if statement before the error statement, but no error is reported, I found that the difference between the two places is that the first if uses a single []. If the code is changed to [], it will succeed. The modification is as follows:

if [ -e /home/dc/testnn-aaa.zip ];then

The above article on solving the problem of automatic exit when starting the image with docker run or docker restart is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to create Apache image using Dockerfile
  • Docker image access to local elasticsearch port operation
  • Multi-service image packaging operation of Dockerfile under supervisor
  • How to create your own Docker image and upload it to Dockerhub
  • How to change the domestic image source for Docker
  • Docker pull image and tag operation pull | tag
  • Use nexus as a private library to proxy docker to upload and download images
  • Docker image creation, uploading, pulling and deployment operations (using Alibaba Cloud)
  • Docker image analysis tool dive principle analysis

<<:  Summary of the use of MySQL date and time functions

>>:  HTML form tag tutorial (1):

Recommend

Django+mysql configuration and simple operation database example code

Step 1: Download the mysql driver cmd enters the ...

Detailed analysis of replication in Mysql

1.MySQL replication concept It means transferring...

Solution to Docker image downloading too slowly

Docker image download is stuck or too slow I sear...

How MLSQL Stack makes stream debugging easier

Preface A classmate is investigating MLSQL Stack&...

CentOS7 deploys version 19 of docker (simple, you can follow it)

1. Install dependency packages [root@localhost ~]...

How to change the CentOS server time to Beijing time

1. I purchased a VPS and CentOS system, and found...

Solve the problem of docker log mounting

The key is that the local server does not have wr...

How to install and configure ftp server in CentOS8.0

After the release of CentOS8.0-1905, we tried to ...

Navicat connects to MySQL8.0.11 and an error 2059 occurs

mistake The following error occurs when connectin...

Detailed explanation of the use of React list bar and shopping cart components

This article example shares the specific code of ...

Tips on setting HTML table borders

For many people who are new to HTML, table <ta...

Implementation of docker view container log command

Why should we read the log? For example, if the c...

Eight common SQL usage examples in MySQL

Preface MySQL continued to maintain its strong gr...