From 01251a86bbfbcde0eca8bd3afcfc75b5eb7db06d Mon Sep 17 00:00:00 2001 From: askiiart <90910526+askiiart@users.noreply.github.com> Date: Thu, 15 Dec 2022 10:31:03 -0600 Subject: [PATCH] Add containers() method --- docker_wrapper.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/docker_wrapper.py b/docker_wrapper.py index e092874..1316e94 100644 --- a/docker_wrapper.py +++ b/docker_wrapper.py @@ -13,7 +13,6 @@ class Docker: """ if '\n' not in raw_info: raise(NoContainersError('A Docker container is required to run this program. Please create a docker container and try again.')) - raw_info = getoutput('docker ps') # Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces) header = raw_info[:raw_info.find('\n')+1] header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'), @@ -44,14 +43,28 @@ class Docker: return info - def container_list(raw_info=getoutput('docker container list')): + def containers(raw_info=getoutput('docker container list')): + """ + :return: A list of docker containers + """ + # Remove header + raw_info = raw_info[raw_info.find('\n')+1:] + info = {} + + # Find container names + containers = [] + for line in raw_info.split('\n'): + containers.append(line.strip()[line.strip().rfind(' ')+1:]) + + return containers + + def container_info(raw_info=getoutput('docker container list')): """ Gets info about all the Docker containers :return: Nested dict of containers info """ if '\n' not in raw_info: raise(NoContainersError('A Docker container is required to run this program. Please create a docker container and try again.')) - raw_info = getoutput('docker container list') # Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces) header = raw_info[:raw_info.find('\n')+1] header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'),