2017년 8월 2일 수요일

Docker API for Python

Before running docker client written by python code,
You have to enable Docker remote API on Docker node(Server side).

Enabling Docker Remote API (on Server side)

Read the web page:
  https://www.ivankrizsan.se/2016/05/18/enabling-docker-remote-api-on-ubuntu-16-04/

Modify docker.service file.
  file path: /lib/systemd/system/docker.service

  ## Caution: docker version 17.03 or earlyer
  ExecStart=/usr/bin/docker daemon -H fd:// -H tcp://0.0.0.0:4243
  ## Caution: docker version 17.06 or later
  ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:4243

Run following command:
  $ systemctl daemon-reload

  $ systemctl restart docker.service


Install Python API (on client side)

  $ RUN pip install docker


Write Docker Client with Python code

Example Code:
import docker

my_client = docker.DockerClient(base_url='tcp://10.10.108.181:4243')

my_services = my_client.services.list()
idx = 0
for svc in my_services:
    print("[%d]  service             : %s" % (idx, svc))
    print("[%d]  service.id          : %s" % (idx, svc.id))
    print("[%d]  service.name        : %s" % (idx, svc.name))
    print("")
    idx += 1

my_svc = my_client.services.get("my-web")
print("service.id          : %s" % my_svc.id)
print("service.name        : %s" % my_svc.name)
print("service.version     : %s" % my_svc.version)
print("service.attrs       : %s" % my_svc.attrs)


my_images = my_client.images.list()
idx = 0
for img in my_images:
    print("[%d]  img                 : %s" % (idx, img))
    print("[%d]  img.id              : %s" % (idx, img.id))
    print("[%d]  img.attrs           : %s" % (idx, img.attrs))
    print("")
    idx += 1

my_nodes = my_client.nodes.list()
idx = 0
for node in my_nodes:
    print("[%d]  nodes               : %s" % (idx, node))
    print("[%d]  nodes.attrs         : %s" % (idx, node.attrs))
    print("[%d]  nodes.attrs[Status][Addr] : %s" % (idx, node.attrs["Status"]["Addr"]))
    print("")
    idx += 1

my_networks = my_client.networks.list()
idx = 0
for net in my_networks:
    print("[%d]  network             : %s" % (idx, net))
    print("[%d]  network.name        : %s" % (idx, net.name))
    print("[%d]  network.attrs       : %s" % (idx, net.attrs))
    print("")
    idx += 1

net = my_client.networks.get("management")
print("network       : %s" % net)
print("network.name  : %s" % net.name)
print("network.attrs : %s" % net.attrs)

net = my_client.networks.get("gq73i6y2jn9ydc3m44xf7e7ko")
print("network       : %s" % net)
print("network.name  : %s" % net.name)
print("network.attrs : %s" % net.attrs)
print("Subnet        : %s" % net.attrs['IPAM']['Config'][0]['Subnet'])
print("Gateway       : %s" % net.attrs['IPAM']['Config'][0]['Gateway'])
print("Driver        : %s" % net.attrs['IPAM']['Driver'])
print("Containers.keys  : %s" % net.attrs['Containers'].keys())
print("Containers.values: %s" % net.attrs['Containers'].values())
container_list = net.attrs['Containers'].values()
print("IPv4Address       : %s" % container_list[0]["IPv4Address"])
cidr = container_list[0]["IPv4Address"]
ip_addr = cidr.split('/')

print("ip_addr : %s" % ip_addr[0])

You can get detail information from:
  http://docker-py.readthedocs.io/en/stable/client.html

댓글 없음:

댓글 쓰기