@@ -27,6 +27,7 @@ class Container:
2727
2828 def __init__ (self , json_dict : dict ) -> None :
2929 self .id = json_dict .get ("Id" , None )
30+ self .short_id = self .id [:12 ] if self .id else None
3031 self .name = json_dict .get ("Name" , None )
3132 ports = json_dict .get ("NetworkSettings" , None )
3233 if ports :
@@ -48,18 +49,17 @@ def __init__(self, json_dict: dict) -> None:
4849 self .short_id = self .id [:12 ] if self .id else None
4950 self .attrs = json_dict
5051 self .tags = json_dict .get ("RepoTags" , None )
51- self .name = self .tags [0 ] if self .tags else None
52+ # Docker images may be prefixed with the registry URL
53+ self .name = self .tags [0 ].split ("/" )[- 1 ] if self .tags else None
5254
5355 def get_all_containers (self ) -> dict :
5456 """Returns the current list of containers."""
55- if not self .containers :
56- self ._update_containers ()
57+ self ._update_containers ()
5758 return self .containers
5859
5960 def get_all_images (self ) -> dict :
6061 """Returns the current list of images."""
61- if not self .images :
62- self ._update_images ()
62+ self ._update_images ()
6363 return self .images
6464
6565 def get_projects (self ) -> dict :
@@ -182,14 +182,12 @@ def exec_run(self, container_id: str, command: str) -> tuple:
182182 Return exit code and stdout.
183183 """
184184 try :
185- print ("AKOAKO === ENTERING EXEC_RUN" )
186185 result = subprocess .run (
187186 ["docker" , "exec" , container_id , * command ],
188187 check = True ,
189188 capture_output = True ,
190189 text = True ,
191190 )
192- print ("AKOAKO === EXITING EXEC_RUN" )
193191 return result .returncode , result .stdout
194192 except subprocess .CalledProcessError as ex :
195193 raise RuntimeError (
@@ -199,20 +197,22 @@ def exec_run(self, container_id: str, command: str) -> tuple:
199197 def run_container (
200198 self ,
201199 image_id : str ,
202- command : str ,
200+ command : list [ str ] ,
203201 parsed_volumes : dict ,
204202 gpus : list [int | str ] | None = None ,
205203 ) -> bool :
206204 """Run a command in a container from an image."""
207205 # Convert the parsed_volumes into a list of strings in proper argument format,
208206 # `-v host_path:container_path:mode`.
209207 if not parsed_volumes :
210- parsed_volumes = []
208+ volume_args = []
211209 else :
212- parsed_volumes = [
213- f"-v { host_path } :{ volume_info ['bind' ]} :{ volume_info ['mode' ]} "
214- for host_path , volume_info in parsed_volumes .items ()
215- ]
210+ volume_args = []
211+ for host_path , volume_info in parsed_volumes .items ():
212+ volume_args .append ("-v" )
213+ volume_args .append (
214+ f"{ host_path } :{ volume_info ['bind' ]} :{ volume_info ['mode' ]} "
215+ )
216216
217217 if gpus :
218218 gpus_str = "," .join (gpus )
@@ -225,7 +225,7 @@ def run_container(
225225 "docker" ,
226226 "run" ,
227227 "--rm" ,
228- * parsed_volumes ,
228+ * volume_args ,
229229 * ([gpus_option ] if gpus_option else []),
230230 image_id ,
231231 * command ,
@@ -245,7 +245,9 @@ def run_container(
245245 return result .stdout , result .stderr
246246
247247 except subprocess .CalledProcessError as ex :
248- raise RuntimeError (f"{ ex .stderr } " ) from ex
248+ raise RuntimeError (
249+ f"Error running command: `{ ' ' .join (cmd_list )} `. \n \n { ex .stderr } "
250+ ) from ex
249251
250252 def docker_buildx (
251253 self ,
@@ -303,6 +305,8 @@ def docker_buildx(
303305 if return_code != 0 :
304306 raise RuntimeError ("Error while building Docker image" , logs )
305307
308+ # Update self.images
309+ self ._update_images ()
306310 # Get image object
307311 image = self .get_image (tag )
308312 return image
@@ -316,7 +320,10 @@ def _update_containers(self) -> None:
316320 text = True ,
317321 check = True ,
318322 )
319- container_ids = result .stdout .strip ().split ("\n " )
323+ if not result .stdout :
324+ container_ids = []
325+ else :
326+ container_ids = result .stdout .strip ().split ("\n " )
320327
321328 # Check if theres any cleaned up containers.
322329 for container_id in self .containers :
@@ -347,7 +354,10 @@ def _update_images(self) -> None:
347354 text = True ,
348355 check = True ,
349356 )
350- images = image_ids .stdout .strip ().split ("\n " )
357+ if not image_ids .stdout :
358+ images = []
359+ else :
360+ images = image_ids .stdout .strip ().split ("\n " )
351361
352362 # Clean up deleted images.
353363 for image in self .images :
0 commit comments