Solution to the problem that Docker container cannot be stopped or killed

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1

Problem Process

A MySQL container in a certain environment cannot be stopped, killed, or rm

sudo docker ps | grep mysql to view the container

7844250860f8 mysql:5.7.22 "/.r/r docker-entr..." 41 minutes ago Up 8 minutes r-dlrel-mysql-1-66df8f33

After using docker stop / docker kill / docker rm -f and other commands, the container will automatically restart immediately

Check the container immediately. The running time is: Up Less than a second, indicating that the container started immediately.

7844250860f8 mysql:5.7.22 "/.r/r docker-entr..." 42 minutes ago Up Less than a second r-dlrel-mysql-1-66df8f33

Kill the physical process corresponding to the container and restart it automatically

How to get the physical process: 1. The State.Pid field in docker inspect is the physical process ID; 2. ps command

Check the container restart policy. The policy is no, which means it will not restart automatically.

If you need to update the restart policy of a running container, you can use this command: docker update –restart=no my-container

"RestartPolicy": {
  "Name": "no",
  "MaximumRetryCount": 0
},

The magical way programmers solve problems

Have you ever encountered this scenario:

  • I was puzzled by a problem, but as soon as I walked in front of my colleague and explained the problem clearly, I suddenly understood it.
  • The problem was obviously very simple, but there was a problem when the program was running. Then I asked a colleague to help check the basic configuration, and I suddenly realized the answer.

This time I belong to the first type. As soon as I finished asking the question, I immediately remembered: Damn, it was the container orchestration tool Rancher that was doing the scheduling, and the container would automatically restart after it crashed.

I logged into Rancher and saw that it was indeed the case, a "wrong" problem. Although this is not a problem this time, Docker does have the problem of not being able to stop, and there is a lot of information about it.

Further reading: Docker Restart Policy

During the solution process, I learned a lot about Docker Restart Policy and Bugs. This article is easy to understand: Ensuring Containers Are Always Running with Docker's Restart Policy

Here we just make a record and learn about the four Restart Policies of Docker.

no

No is the default policy, and the container will not be restarted under any circumstances

on-failure

on-failure means that if the container exit code is abnormal, it will be restarted, and if the container exit code is normal, no processing will be performed.

sudo docker run -d --name testing_restarts --restart on-failure:5 testing_restarts
85ff2f096bac9965a9b8cffbb73c1642bf7b64a2173bbd145961231861b95819

on-failure[:max-retries], max-retries indicates the maximum number of restarts.

The benefit of on-failure is that if the container terminates with a normal exit code, it will not restart.

always

Regardless of the container exit code, it will automatically restart. Here are a few scenarios:

  1. The container terminates with an abnormal status code (such as the application terminating due to insufficient memory)
  2. The container is stopped normally, and then the machine is restarted or the Docker service is restarted
  3. The container is running normally after it crashes, and then the machine is restarted or the Docker service is restarted

In the above cases, the container will always be restarted. However, if the on-failure and no policies are used, the container will not be restarted after the machine is restarted.

unless-stopped

Unless-stopped is basically the same as always, except for one scenario where unless-stopped is a little special:

If the container is stopped normally, and then the machine is restarted or the Docker service is restarted, the container will not be restarted in this case

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solution to the problem that the docker container cannot be stopped

<<:  Vue implements adding, displaying and deleting multiple images

>>:  vue-cropper plug-in realizes the encapsulation of image capture and upload component

Recommend

A brief discussion on MySQL count of rows

We are all familiar with the MySQL count() functi...

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

js to implement add and delete table operations

This article example shares the specific code of ...

jQuery achieves fade-in and fade-out effects

Before using jQuery to complete the fade-in and f...

Example code of setting label style using CSS selector

CSS Selectors Setting style on the html tag can s...

Not a Chinese specialty: Web development under cultural differences

Web design and development is hard work, so don&#...

Detailed explanation of the solution for migrating antd+react projects to vite

Antd+react+webpack is often the standard combinat...

Detailed description of HTML table border control

Only show the top border <table frame=above>...

MySQL 8.0.13 manual installation tutorial

This article shares the manual installation tutor...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...

Detailed steps to install MySql 5.7.21 in Linux

Preface The most widely used database in Linux is...