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

Ways to improve MongoDB performance

MongoDB is a high-performance database, but in th...

MySQL 8.0.11 Installation Tutorial under Windows

This article records the installation tutorial of...

Install Windows Server 2019 on VMware Workstation (Graphic Tutorial)

If prompted to enter a key, select [I don’t have ...

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...

How to achieve centered layout in CSS layout

1. Set the parent container to a table and the ch...

How to run multiple MySQL instances in Windows

Preface In Windows, you can start multiple MySQL ...

Example of disabling browser cache configuration in Vue project

When releasing a project, you will often encounte...

Node.js implements breakpoint resume

Table of contents Solution Analysis slice Resume ...

Methods and steps for deploying GitLab environment based on Docker

Note: It is recommended that the virtual machine ...

How to modify create-react-app's configuration without using eject

1. Why is eject not recommended? 1. What changes ...

Mysql method to copy a column of data in one table to a column in another table

mysql copy one table column to another table Some...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

Docker View the Mount Directory Operation of the Container

Only display Docker container mount directory inf...