-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRun_docker.py
50 lines (40 loc) · 1.11 KB
/
Run_docker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import docker
import os
import tarfile
import time
import platform
import pathlib
import threading
from multiprocessing import Pool
import multiprocessing
import io
import re
client = docker.from_env()
image = "python:3.9-bullseye"
local_mem = os.getcwd() + "/test_area"
command_list = ["print('Hello World')"]
def process(nums):
global client,image,local_mem,command_list
cont_name = ("Test_container_" + str(nums))
print("Preparing to run ", cont_name)
client.containers.run(image,name=cont_name, detach=True, tty=True)
print("Container started: ", cont_name)
PW_container = client.containers.get(cont_name)
PW_container.exec_run(cmd=command_list)
PW_container.stop()
print("Container stopped: ", cont_name)
time.sleep(2)
PW_container.remove()
def run_container():
global client,image,local_mem,command_list
multiprocessing.freeze_support()
nums = [1,2,3]
process_num = len(nums)
if process_num > 10:
process_num = 10
pool = Pool(processes=process_num)
pool.map(process,nums)
pool.close()
pool.join()
return local_mem