How to list docker containers which are running?

There is a restart policy associated with every docker container. The first step is to list all the docker containers. You can do that as follows:

docker container ls

You should then get an output as follows:

How to check restart policy of docker containers?

You can do that with docker inspect <insert docker container id here>. Which means for example for our example you would do something like this:

docker inspect bea070eba948

You will get all information about that docker container which can be overwhelming:

[
    {
        "Id": "bea070eba948b52af689bf528d45989ff97bd87e1b22cdfee15f7c0caf9dba27",
        "Created": "2022-08-26T09:32:04.874209501Z",
        "Path": "/opt/eventstore/eventstored",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 11241,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-09-01T13:32:09.695694777Z",
            "FinishedAt": "2022-09-01T13:29:22.540111592Z",
            "Health": {
                "Status": "healthy",
                "FailingStreak": 0,
                "Log": [
                    {
                        "Start": "2022-09-01T13:34:30.122485175Z",
                        "End": "2022-09-01T13:34:30.263994865Z",
                        "ExitCode": 0,
                        "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n"
                    },
                    {
                        "Start": "2022-09-01T13:34:35.275063904Z",
                        "End": "2022-09-01T13:34:35.404368252Z",
                        "ExitCode": 0,
                        "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n"
                    },
                    {
                        "Start": "2022-09-01T13:34:40.415449945Z",
                        "End": "2022-09-01T13:34:40.550987054Z",
                        "ExitCode": 0,
                        "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n"
                    },
                    {
                        "Start": "2022-09-01T13:34:45.562356473Z",
                        "End": "2022-09-01T13:34:45.681111038Z",
                        "ExitCode": 0,
                        "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n"
                    },
                    {
                        "Start": "2022-09-01T13:34:50.689468456Z",
                        "End": "2022-09-01T13:34:50.832617546Z",
                        "ExitCode": 0,
                        "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n"
                    }
                ]
            }
        },
        "Image": "sha256:efac5381e100023044a52a9def4c5bdb2b214261252091012efdbdefd920ba38",
        "ResolvConfPath": "/var/lib/docker/containers/bea070eba948b52af689bf528d45989ff97bd87e1b22cdfee15f7c0caf9dba27/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/bea070eba948b52af689bf528d45989ff97bd87e1b22cdfee15f7c0caf9dba27/hostname",
        "HostsPath": "/var/lib/docker/containers/bea070eba948b52af689bf528d45989ff97bd87e1b22cdfee15f7c0caf9dba27/hosts",
        "LogPath": "/var/lib/docker/containers/bea070eba948b52af689bf528d45989ff97bd87e1b22cdfee15f7c0caf9dba27/bea070eba948b52af689bf528d45989ff97bd87e1b22cdfee15f7c0caf9dba27-json.log",
        "Name": "/node1.eventstore",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/Users//Documents/work/docker/21.10.2/certs:/certs:rw",
                "9fb2d7769d37a33f1c7f530c37c9dcfb689a8c6eba580be3301eb5cd90faebe2:/var/lib/eventstore:rw",
                "a436d2ec32204411ecba492b1a5b73b610c51e427de0c5c6e849e4f98b1f6307:/var/log/eventstore:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "eventstoredb.local",
            "PortBindings": {
                "1113/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "1111"
                    }
                ],
                "2113/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "2111"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "always",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/8a5bb0eb8863751ef1b1ec3e48ac594e3c3e6f11e607586587c3828ffdcd7218-init/diff:/var/lib/docker/overlay2/6f6bb5d2179661f2e1c84bfe4f4b65cb5b81002de82771aed39204ac3b96d344/diff:/var/lib/docker/overlay2/5ad6775653d74051fa05c082749b5f6db40050ad7130416b5cb3ee4f7ac66b25/diff:/var/lib/docker/overlay2/c1503082a3fc668ed648bcc70e6d078611b2783c4fd15e7a18da49c521e303ae/diff:/var/lib/docker/overlay2/9a28c539bc7dca97875130b9356c6a99745616f26696acea67b8e3baf1984b9b/diff:/var/lib/docker/overlay2/f8d5c1f5f16c60e8fffc6e10837d7d54931b23bf4a8f03689ee8d86ab60537b9/diff:/var/lib/docker/overlay2/a8ef5ee6c63068cb6db823c01a61a777ae9b64defc78876e6d6e6c16b46f2c2f/diff:/var/lib/docker/overlay2/ba8e1e96c2431d51d64b5c3a8cd6b916c60310906eff26fc0bf3c8705a2d2076/diff:/var/lib/docker/overlay2/9afb116e4bdbbcf264df7a488dd9c53a57155d9aebd055852e3f274dd39a0ba1/diff:/var/lib/docker/overlay2/7001ba357bb61e6b56774ab1ea36c6bbd2ef343491dd157dbba79662786da5b5/diff:/var/lib/docker/overlay2/e43d28d89ecb8d7ccd4e9ea0f581f4a2fd634fb09893b664137df51a179a7653/diff",
                "MergedDir": "/var/lib/docker/overlay2/8a5bb0eb8863751ef1b1ec3e48ac594e3c3e6f11e607586587c3828ffdcd7218/merged",
                "UpperDir": "/var/lib/docker/overlay2/8a5bb0eb8863751ef1b1ec3e48ac594e3c3e6f11e607586587c3828ffdcd7218/diff",
                "WorkDir": "/var/lib/docker/overlay2/8a5bb0eb8863751ef1b1ec3e48ac594e3c3e6f11e607586587c3828ffdcd7218/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users//Documents/work/docker/21.10.2/certs",
                "Destination": "/certs",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "9fb2d7769d37a33f1c7f530c37c9dcfb689a8c6eba580be3301eb5cd90faebe2",
                "Source": "/var/lib/docker/volumes/9fb2d7769d37a33f1c7f530c37c9dcfb689a8c6eba580be3301eb5cd90faebe2/_data",
                "Destination": "/var/lib/eventstore",
                "Driver": "local",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "a436d2ec32204411ecba492b1a5b73b610c51e427de0c5c6e849e4f98b1f6307",
                "Source": "/var/lib/docker/volumes/a436d2ec32204411ecba492b1a5b73b610c51e427de0c5c6e849e4f98b1f6307/_data",
                "Destination": "/var/log/eventstore",
                "Driver": "local",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "bea070eba948",
            "Domainname": "",
            "User": "eventstore",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "1112/tcp": {},
                "1113/tcp": {},
                "2112/tcp": {},
                "2113/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "EVENTSTORE_CLUSTER_SIZE=3",
                "EVENTSTORE_RUN_PROJECTIONS=All",
                "EVENTSTORE_DISCOVER_VIA_DNS=false",
                "EVENTSTORE_ENABLE_EXTERNAL_TCP=True",
                "EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true",
                "EVENTSTORE_ADVERTISE_HOST_TO_CLIENT_AS=127.0.0.1",
                "EVENTSTORE_INT_IP=172.30.240.11",
                "EVENTSTORE_ADVERTISE_HTTP_PORT_TO_CLIENT_AS=2111",
                "EVENTSTORE_ADVERTISE_TCP_PORT_TO_CLIENT_AS=1111",
                "EVENTSTORE_GOSSIP_SEED=172.30.240.12:2113,172.30.240.13:2113",
                "EVENTSTORE_TRUSTED_ROOT_CERTIFICATES_PATH=/certs/ca",
                "EVENTSTORE_CERTIFICATE_FILE=/certs/node1/node.crt",
                "EVENTSTORE_CERTIFICATE_PRIVATE_KEY_FILE=/certs/node1/node.key",
                "EVENTSTORE_DISABLE_EXTERNAL_TCP_TLS=True",
                "affinity:container==506d32118bcd78c495c26e138bd47f8c2a7a09322cc9a24d0ed2419813f8b390",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "ASPNETCORE_URLS=http://+:80",
                "DOTNET_RUNNING_IN_CONTAINER=true",
                "LANGUAGE=en_US:en",
                "DEBIAN_FRONTEND=noninteractive",
                "ACCEPT_EULA=Y"
            ],
            "Cmd": null,
            "Healthcheck": {
                "Test": [
                    "CMD-SHELL",
                    "curl --fail --insecure https://node1.eventstore:2113/health/live || exit 1"
                ],
                "Interval": 5000000000,
                "Timeout": 5000000000,
                "Retries": 24
            },
            "Image": "eventstore/eventstore:21.10.2-buster-slim",
            "Volumes": {
                "/certs": {},
                "/var/lib/eventstore": {},
                "/var/log/eventstore": {}
            },
            "WorkingDir": "/opt/eventstore",
            "Entrypoint": [
                "/opt/eventstore/eventstored"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "c8e628d9d0bf1ecee578c8f765da4d972c6757492d4413f08edcfae5f7aae3da",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "21102",
                "com.docker.compose.project.config_files": "docker-compose.yaml",
                "com.docker.compose.project.working_dir": "/Users//Documents/work/docker/21.10.2",
                "com.docker.compose.service": "node1.eventstore",
                "com.docker.compose.version": "1.29.2"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "b92fb5032ddce833e8bd46f5059a7eec96677ee9c435c444941fc14c2d04e003",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "1112/tcp": null,
                "1113/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "1111"
                    }
                ],
                "2112/tcp": null,
                "2113/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "2111"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/b92fb5032ddc",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "eventstoredb.local": {
                    "IPAMConfig": {
                        "IPv4Address": "172.30.240.11"
                    },
                    "Links": null,
                    "Aliases": [
                        "bea070eba948",
                        "node1.eventstore"
                    ],
                    "NetworkID": "b5b789d16d59f504159492e24ccf89cc3c19be47b1f25100b768fcc53e613e81",
                    "EndpointID": "e183f6724970d74658d472ed54688c8e886acfc07300b88ed21763085976f780",
                    "Gateway": "172.30.240.1",
                    "IPAddress": "172.30.240.11",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:1e:f0:0b",
                    "DriverOpts": null
                }
            }
        }
    }
]

This is a lot of information and we are not interested in that. The information provided is in JSON if ever you are need to parse it programmatically this will come in handy. For our case I would just paste it somewhere and search for “RestartPolicy”. This is what it likely looks like:

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

The above shows that it will always restart and we need to change that.

How to update docker container restart policy?

That is easy you need to run docker update –restart=no <insert container id here> which in our case becomes:

docker update --restart=no bea070eba948

Then if you inspect the container again you should see the following:

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

The policy has now been updated from Always to no.