others-how to solve docker hang or not responsive when using docker ps commands

Problem

When we use docker from command line , sometimes, the docker command hang indefinitely without any response, Including docker info, docker help, docker version, docker ps, and docker images.

I can only quit the command by input ctrl+C.

Environment

  • Docker: Server Version: 19.03.13

Reason

When we use docker info/docker version/docker ps/docker images, we are actually connecting the docker daemon service from docker client command, just as follows:

image-20201201202257959

And from the official docker document, you can see that the docker daemon is just a service that provide restful web service from socket:

Docker Engine Components Flow

The dockerd and docker can be described as follows:

The Docker daemon

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

The Docker client

The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

When your docker command hangs unexpectedly, there maybe such problems in your system:

  • Your computer is full of docker images, has exceeded the capacity of your computer. If the docker images are out of date, please remove it from your system
  • The docker daemon process is stuck for some reason, you can check your /var/log/syslog for the real reason

Solution

There are variety of solutions to solve this problem:

Solution #1:

The key point of this solution is remove all the docker images and logs from the system:

Remove the docker images that are not used any more

docker system prune -a

If the above command not work (hang), please use this:

# get the root dir of docker images
docker info |grep 'Docker Root Dir' 
sudo rm -rf <the docker root image directory path>

# if the above not work, just remove the default directory of docker images
# you should make a backup before this command
sudo rm -rf /var/lib/docker

# remove runtime files of docker(you should make a backup before this command)
sudo rm -rf /var/run/docker

You can try docker info now, if still not work, you can uninstall and re-install the docker like this(centos example):

sudo yum remove docker docker-common docker-selinux docker-engine 

Install docker again:

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
    
 sudo yum install docker-ce docker-ce-cli containerd.io

Start docker:

sudo systemctl start docker

Check your docker installation:

sudo docker run hello-world

Solution #2:

If solution one not work for you, you can do as follows:

  1. Create a new file /etc/systemd/system/docker.service.d/docker.conf with the following contents, to remove the -H argument that is used when starting the daemon by default.

    [Service]
    ExecStart=
    ExecStart=/usr/bin/dockerd
    
  2. Create a new file /etc/docker/daemon.json with the following contents:

    {
        "debug": true,
        "hosts": ["tcp://127.0.0.1:2375"]
    }
    
  3. Reload the systemctl service

    sudo systemctl daemon-reload
    sudo systemctl stop docker.service
    sudo systemctl start docker.service
    

Solution #3:

Reboot your system if your docker version is too old(less then 13).

-->