Can I show a restart policy for a running Docker container?

When I create containers, I specify a restart policy, but this is not shown in docker ps, and it does not appear in any format line.

Does anyone know how to see the restart policy of running container (s)?

+6
source share
4 answers

Yes, you can use docker inspectone that has a format json, and you just need to request it.

Here is the relevant docker checkout output for a running container zen_easley. Note to change the container name as appropriate for your environment.

  • docker inspect zen_easley
"HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": true,

You can simply run the following command to get the same output.

$ docker inspect -f "{{ .HostConfig.RestartPolicy }}"  zen_easley
{no 0}

, RestartPolicy Name, MaximumRetryCount , 0 - ,

, Name, , .Name :

docker inspect -f "{{ .HostConfig.RestartPolicy.Name }}"  zen_easley
no
+14

docker inspect.

:

docker inspect --format '{{.HostConfig.RestartPolicy.Name}}' <container-id>

(, , ):

docker inspect --format '{{json .HostConfig.RestartPolicy}}' <container-id>
+7

I made this little script to check all containers and their policies:

#!/usr/bin/env bash
#Script to check the restart policy of the containers

readarray -t CONTAINERS < <(docker ps -a | grep -v NAMES | awk '{print $NF}')

for item in "${CONTAINERS[@]}"; do

    #Hard-Bash way
    #data=$(docker inspect "${item}" | grep -A 1 RestartPolicy | awk -F '"' '{print $4}' | tail -n 1)

    #Docker-pr0 way
    data=$(docker inspect -f "{{ .HostConfig.RestartPolicy.Name }}" "${item}")

    echo "Container: ${item} / RestartPolicy: ${data}"
done

Hope this helps someone!

+2
source

For one line of code:

docker ps|grep -v CON|awk '{print $1}'|while read line; do  docker inspect -f "{{ .HostConfig.RestartPolicy.Name }}" $line |xargs echo $line ;done
-one
source

Source: https://habr.com/ru/post/1016155/


All Articles