示例#1
0
class DockerController:
    def __init__(self):
        self.client = DockerClient(base_url='tcp://10.200.10.1:2375')

    def _info(self):
        print(self.client.info())

    def show_running_containers(self):
        return self.client.containers.list()

    def build_image(self, path, tag):
        # path = os.path.join(os.getcwd(), 'files', dir_name)
        self.client.build(path=path, tag=tag)

    def show_images(self):
        return self.client.images()

    def run_container(self,
                      image: str,
                      internal_web_port: int,
                      build_name=None,
                      command=None):
        ssh_port = get_no_port_being_used()
        web_port = get_no_port_being_used()
        container = self.client.containers.run(image=image,
                                               ports={
                                                   '22/tcp':
                                                   ssh_port,
                                                   f'{internal_web_port}/tcp':
                                                   web_port
                                               },
                                               name=build_name,
                                               command=command,
                                               detach=True,
                                               pids_limit=MAX_PID)
        logging.log(logging.INFO,
                    f"ssh port is {ssh_port}, web port is {web_port}")
        container_info = {}
        container_info['id'] = container.id
        container_info['ssh_port'] = ssh_port
        container_info['web_port'] = web_port

        return container_info

    def rm_container(self, container_id: str):
        container = self.client.containers.get(container_id)
        # container.stop()
        # 直接删除
        container.remove(force=True)
        logging.log(logging.INFO, f'{container.id} has been removed')

    def exec_container(self, container_id: str, command):
        self.client.containers.get(container_id).exec_run(command)
示例#2
0
 def _build_images_from_dockerfiles(self):
     """
     Build Docker images for each local Dockerfile found in the package: self.local_docker_files
     """
     if GK_STANDALONE_MODE:
         return  # do not build anything in standalone mode
     dc = DockerClient()
     LOG.info("Building %d Docker images (this may take several minutes) ..." % len(self.local_docker_files))
     for k, v in self.local_docker_files.iteritems():
         for line in dc.build(path=v.replace("Dockerfile", ""), tag=k, rm=False, nocache=False):
             LOG.debug("DOCKER BUILD: %s" % line)
         LOG.info("Docker image created: %s" % k)
示例#3
0
 def _build_images_from_dockerfiles(self):
     """
     Build Docker images for each local Dockerfile found in the package: self.local_docker_files
     """
     if GK_STANDALONE_MODE:
         return  # do not build anything in standalone mode
     dc = DockerClient()
     LOG.info("Building %d Docker images (this may take several minutes) ..." % len(self.local_docker_files))
     for k, v in self.local_docker_files.iteritems():
         for line in dc.build(path=v.replace("Dockerfile", ""), tag=k, rm=False, nocache=False):
             LOG.debug("DOCKER BUILD: %s" % line)
         LOG.info("Docker image created: %s" % k)
示例#4
0
class DockerFileController:
    def __init__(self):
        self.client = DockerClient(base_url='tcp://127.0.0.1:2375')

    def showRunningContainers(self):
        return self.client.containers()

    def buildImage(self, path, tag):
        # path = os.path.join(os.getcwd(), 'files', dir_name)
        self.client.build(path=path, tag=tag)

    def showImages(self):
        return self.client.images()

    def runContainer(self, image, build_name, command=None):
        container = self.client.containers.run(image=image,
                                               ports={'8000/tcp': 3333},
                                               name=build_name,
                                               command=command,
                                               detach=True)

        print(container.logs)
        # print(container.short_id)
        # container = self.client.create_container(
        #     tag_name, ports=[1111, 2222],
        #     host_config=self.client.create_host_config(
        #         port_bindings={
        #             1111: 4567,
        #             2222: None
        #         }
        #     )
        # )
        # print(self.client.logs(container))
        # print(container.log)
        # print(container)

    def getContainerStatus(self, container_id):
        return self.client.containers.get(container_id).status