DEV Community 👩‍💻👨‍💻

K Dhanesh
K Dhanesh

Posted on • Updated on

Python Script to get the Docker Containers status

Used docker-py SDK

Installed using pip command

pip install docker-py
Enter fullscreen mode Exit fullscreen mode
Python Script
import os
import datetime
from docker import Client

cli = Client(base_url='unix://var/run/docker.sock')
containers = cli.containers()
for container in range(len(containers)):
 for key in containers[container]:
Enter fullscreen mode Exit fullscreen mode
# Output
/etcd -name etcd0 -advertise-client-urls http://localhost:2379,http://localhost:4001 -listen-client-urls, -initial-advertise-peer-urls http://localhost:2380 -listen-peer-urls -initial-cluster-token etcd-cluster-1 -initial-cluster etcd0=http://localhost:2380 -initial-cluster-state new
[{'IP': '', 'PrivatePort': 2379, 'PublicPort': 2379, 'Type': 'tcp'}, {'IP': '', 'PrivatePort': 2380, 'PublicPort': 2380, 'Type': 'tcp'}, {'IP': '', 'PrivatePort': 4001, 'PublicPort': 4001, 'Type': 'tcp'}, {'PrivatePort': 7001, 'Type': 'tcp'}]
Up 4 days
{'NetworkMode': 'default'}
{'Networks': {'bridge': {'IPAMConfig': None, 'Links': None, 'Aliases': None, 'NetworkID': '6e5820ead0cfa5c34baee87737f99e5b2b05601d02435760631a914815e0fcb8', 'EndpointID': 'efe94e05a72f0197f891bb49ec6b4882d8da72fc10819cdc38831bbf145001df', 'Gateway': '', 'IPAddress': '', 'IPPrefixLen': 16, 'IPv6Gateway': '', 'GlobalIPv6Address': '', 'GlobalIPv6PrefixLen': 0, 'MacAddress': '02:42:ac:11:00:02', 'DriverOpts': None}}}
[{'Type': 'bind', 'Source': '/usr/share/ca-certificates', 'Destination': '/etc/ssl/certs', 'Mode': '', 'RW': True, 'Propagation': 'rprivate'}]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Life is too short to browse without dark mode