Skip to content

Instantly share code, notes, and snippets.

@geberl
Created April 30, 2019 06:47
Show Gist options
  • Save geberl/bfb70c8ed20e4a3f49af46baac33c380 to your computer and use it in GitHub Desktop.
Save geberl/bfb70c8ed20e4a3f49af46baac33c380 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Documentation:
- Docker Engine API: https://docs.docker.com/develop/sdk/
- Docker SDK for Python: https://docker-py.readthedocs.io/en/stable/
"""
import docker
import platform
def list_containers(client_obj):
all_containers = client_obj.containers.list()
for container in all_containers:
container_name = container.attrs['Name'][1:] # cut beginning /
container_create_datetime = container.attrs['Created']
container_image = container.attrs['Config']['Image']
print('%s - %s - %s' % (container_create_datetime, container_name, container_image))
if __name__ == '__main__':
if platform.system() == 'Darwin':
client = docker.from_env() # works on MacOS but may not work on linux (if environment variables not set)
else:
client = docker.DockerClient(base_url='unix:///var/run/docker.sock')
list_containers(client_obj=client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment