def get_image(ip): mydocker = client.DockerClient(base_url=ip + ":2375") myImage = mydocker.images.list() for image in myImage: str = image.tags.__getitem__(0).split(':') repository = str[0] tag = str[1] print(image.id+" "+image.short_id+" "+repository+" "+tag)
def getContainer(): mydocker = client.DockerClient(base_url="192.168.194.128:2375", timeout=1) try: mydocker.version() except Exception: print("error") return print("ok")
def pullImage(repository, tag, ip): mydocker = client.DockerClient(base_url=ip + ":2375") image = mydocker.images.pull(repository=repository, tag=tag) images = mydocker.images.list() image = mydocker.images.get(repository + ":" + tag) if image in images: print("pull success") print(image.id + " " + image.short_id) else: print("pull failure")
def getContainer(containerName,ip): mydocker = client.DockerClient(base_url=ip + ":2375") containerNames = [] containers = mydocker.containers.list() for container in containers: containerNames.append(container.name) if containerName in containerNames: container = mydocker.containers.get(containerName) print(container.status) else: print("error")
def deleteImage(repository, tag, ip): mydocker = client.DockerClient(base_url=ip + ":2375") targetimage = mydocker.images.get(repository + ":" + tag) image_list = mydocker.images.list() if targetimage in image_list: mydocker.images.remove(repository + ":" + tag) image_list = mydocker.images.list() if targetimage not in image_list: print("delete success") else: print("delete failure") else: print("delete failure")
def createNetwork(ip): mydocker = client.DockerClient(base_url=ip + ":2375") networks = mydocker.networks.list() name_list = [] for network in networks: name_list.append(network.name) network = None if "zhuanyong" in name_list: network = mydocker.networks.get("zhuanyong") network.remove() if "zhuanyong" in name_list: print("delete failure") else: print("delete success")
def runContainer(repository, tag, ip, port, name): mydocker = client.DockerClient(base_url=ip + ":2375") container = mydocker.containers.run(repository + ":" + tag, detach=True, ports={port + '/tcp': [port]}, name=name, network="zhuanyong") if container.status == "created": container_list = mydocker.containers.list() container = mydocker.containers.get(name) if container in container_list: if container.status == "running": print("create success") print(container.id + " " + container.short_id + " " + container.status) else: print("create failure") else: print("create failure")
def createNetwork(ip): #mydocker = client.DockerClient(base_url=ip + ":2375") mydocker = client.DockerClient(base_url=ip + ":2375") networks = mydocker.networks.list() name_list = [] for network in networks: name_list.append(network.name) if "zhuanyong" in name_list: print("create success") else: newNetwork = mydocker.networks.create(name="zhuanyong", driver="bridge") networks = mydocker.networks.list() if newNetwork in networks: print("create success") else: print("create failure")
def offContainer(containerName, ip): mydocker = client.DockerClient(base_url=ip + ":2375") containerNames = [] containers = mydocker.containers.list() for container in containers: containerNames.append(container.name) if containerName in containerNames: container = mydocker.containers.get(containerName) container.stop() container = mydocker.containers.get(containerName) if container.status == "exited": container.remove() container_list = mydocker.containers.list() if container not in container_list: print("close success") else: print("close failure") else: print("close failure") else: print("close failure")