20
20
)
21
21
22
22
LOCALNET_IMAGE_NAME = "ghcr.io/opentensor/subtensor-localnet:devnet-ready"
23
+ CONTAINER_NAME_PREFIX = "test_local_chain_"
23
24
24
25
25
26
def wait_for_node_start (process , timestamp = None ):
@@ -163,7 +164,32 @@ def try_start_docker():
163
164
print ("Docker wasn't run. Manual start may be required." )
164
165
return False
165
166
166
- container_name = f"test_local_chain_{ str (time .time ()).replace ('.' , '_' )} "
167
+ def stop_existing_test_containers ():
168
+ """Stop running Docker containers with names starting with 'test_local_chain_'."""
169
+ try :
170
+ existing_container_result = subprocess .run (
171
+ [
172
+ "docker" ,
173
+ "ps" ,
174
+ "--filter" ,
175
+ f"name={ CONTAINER_NAME_PREFIX } " ,
176
+ "--format" ,
177
+ "{{.ID}}" ,
178
+ ],
179
+ stdout = subprocess .PIPE ,
180
+ stderr = subprocess .PIPE ,
181
+ text = True ,
182
+ check = True ,
183
+ )
184
+ container_ids = existing_container_result .stdout .strip ().splitlines ()
185
+ for cid in container_ids :
186
+ if cid :
187
+ print (f"Stopping existing container: { cid } " )
188
+ subprocess .run (["docker" , "stop" , cid ], check = True )
189
+ except subprocess .CalledProcessError as e :
190
+ print (f"Failed to stop existing containers: { e } " )
191
+
192
+ container_name = f"{ CONTAINER_NAME_PREFIX } { str (time .time ()).replace ('.' , '_' )} "
167
193
168
194
# Command to start container
169
195
cmds = [
@@ -182,6 +208,8 @@ def try_start_docker():
182
208
183
209
try_start_docker ()
184
210
211
+ stop_existing_test_containers ()
212
+
185
213
# Start container
186
214
with subprocess .Popen (
187
215
cmds ,
0 commit comments