| 63 | return infos |
| 64 | |
| 65 | def start_container(self) -> None: |
| 66 | if not self.client: self.client = self.init_docker() |
| 67 | existing_container = None |
| 68 | for container in self.client.containers.list(all=True): |
| 69 | if container.name == self.name: |
| 70 | existing_container = container |
| 71 | break |
| 72 | |
| 73 | if existing_container: |
| 74 | if existing_container.status != 'running': |
| 75 | PrintStyle.standard(f"Starting existing container: {self.name} for safe code execution...") |
| 76 | if self.logger: self.logger.log(type="info", content=f"Starting existing container: {self.name} for safe code execution...") |
| 77 | |
| 78 | existing_container.start() |
| 79 | self.container = existing_container |
| 80 | time.sleep(2) # this helps to get SSH ready |
| 81 | |
| 82 | else: |
| 83 | self.container = existing_container |
| 84 | # PrintStyle.standard(f"Container with name '{self.name}' is already running with ID: {existing_container.id}") |
| 85 | else: |
| 86 | PrintStyle.standard(f"Initializing docker container {self.name} for safe code execution...") |
| 87 | if self.logger: self.logger.log(type="info", content=f"Initializing docker container {self.name} for safe code execution...") |
| 88 | |
| 89 | self.container = self.client.containers.run( |
| 90 | self.image, |
| 91 | detach=True, |
| 92 | ports=self.ports, # type: ignore |
| 93 | name=self.name, |
| 94 | volumes=self.volumes, # type: ignore |
| 95 | ) |
| 96 | # atexit.register(self.cleanup_container) |
| 97 | PrintStyle.standard(f"Started container with ID: {self.container.id}") |
| 98 | if self.logger: self.logger.log(type="info", content=f"Started container with ID: {self.container.id}") |
| 99 | time.sleep(5) # this helps to get SSH ready |