DEV Community

loading...

Python Script to get the Docker Containers status

K Dhanesh
Infra Coder Interested in Public & Private Cloud, DC experience in Sublime IP, Equinix | AWS, Python, Ruby on Rails | Automation Using Ansible Docker Capistrano
Updated on ・1 min read

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]:
    print(containers[container][key])
Enter fullscreen mode Exit fullscreen mode
# Output
b8c2a832a4f107b86981d3b346d5627e0554312465fde358f1072b61cc940961
['/etcd']
quay.io/coreos/etcd:v2.2.1
sha256:142eb4c815648ddd1146ce61f85ee577fce794a54489873134b2d9714f396ac6
/etcd -name etcd0 -advertise-client-urls http://localhost:2379,http://localhost:4001 -listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 -initial-advertise-peer-urls http://localhost:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster-1 -initial-cluster etcd0=http://localhost:2380 -initial-cluster-state new
1618796211
[{'IP': '0.0.0.0', 'PrivatePort': 2379, 'PublicPort': 2379, 'Type': 'tcp'}, {'IP': '0.0.0.0', 'PrivatePort': 2380, 'PublicPort': 2380, 'Type': 'tcp'}, {'IP': '0.0.0.0', 'PrivatePort': 4001, 'PublicPort': 4001, 'Type': 'tcp'}, {'PrivatePort': 7001, 'Type': 'tcp'}]
{}
running
Up 4 days
{'NetworkMode': 'default'}
{'Networks': {'bridge': {'IPAMConfig': None, 'Links': None, 'Aliases': None, 'NetworkID': '6e5820ead0cfa5c34baee87737f99e5b2b05601d02435760631a914815e0fcb8', 'EndpointID': 'efe94e05a72f0197f891bb49ec6b4882d8da72fc10819cdc38831bbf145001df', 'Gateway': '172.17.0.1', 'IPAddress': '172.17.0.2', '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

Discussion (0)