2222
2323import os
2424from pathlib import Path
25- import subprocess
2625
2726from ansys .systemcoupling .core .syc_version import SYC_VERSION_DOT , normalize_version
27+ from ansys .systemcoupling .core .util .logging import LOG
2828
2929_MPI_VERSION_VAR = "FLUENT_INTEL_MPI_VERSION"
3030_MPI_VERSION = "2021"
@@ -41,15 +41,22 @@ def _image_tag(version: str) -> str:
4141
4242def start_container (
4343 mounted_from : str , mounted_to : str , network : str , port : int , version : str
44- ) -> None :
44+ ) -> object :
4545 """Start a System Coupling container.
4646
4747 Parameters
4848 ----------
4949 port : int
5050 gPRC server local port, mapped to the same port in container.
51+
52+ Returns
53+ -------
54+ object
55+ The container instance (``Container`` object from Python docker library).
5156 """
52- args = ["-m" , "cosimgui" , f"--grpcport=0.0.0.0:{ port } " ]
57+ import docker
58+
59+ LOG .debug ("Starting System Coupling docker container..." )
5360
5461 if version :
5562 image_tag = _image_tag (version )
@@ -58,47 +65,45 @@ def start_container(
5865
5966 mounted_from = str (Path (mounted_from ).absolute ())
6067
61- run_args = [
62- "docker" ,
63- "run" ,
64- "-d" ,
65- "--rm" ,
66- "-p" ,
67- f"{ port } :{ port } " ,
68- "-v" ,
69- f"{ mounted_from } :{ mounted_to } " ,
70- "-w" ,
71- mounted_to ,
72- "-e" ,
73- f"{ _MPI_VERSION_VAR } ={ _MPI_VERSION } " ,
74- "-e" ,
75- f"AWP_ROOT=/ansys_inc" ,
76- f"ghcr.io/ansys/pysystem-coupling:{ image_tag } " ,
77- ] + args
68+ image_name = f"ghcr.io/ansys/pysystem-coupling:{ image_tag } "
69+
70+ environment = [f"{ _MPI_VERSION_VAR } ={ _MPI_VERSION } " , f"AWP_ROOT=/ansys_inc" ]
7871
72+ # Additional environment
7973 container_user = os .getenv ("SYC_CONTAINER_USER" )
8074 if container_user :
81- idx = run_args .index ("-p" )
82- run_args .insert (idx , container_user )
83- run_args .insert (idx , "--user" )
8475 # Licensing can't log to default location if user is not the default 'root'
85- run_args . insert ( idx , f"ANSYSLC_APPLOGDIR={ mounted_to } " )
86- run_args . insert ( idx , "-e" )
76+ environment . append ( f"ANSYSLC_APPLOGDIR={ mounted_to } " )
77+ # See also "user" argument added to args below
8778
8879 license_server = os .getenv ("ANSYSLMD_LICENSE_FILE" )
8980 if license_server :
90- idx = run_args .index ("-e" )
91- run_args .insert (idx , f"ANSYSLMD_LICENSE_FILE={ license_server } " )
92- run_args .insert (idx , "-e" )
93-
81+ environment .append (f"ANSYSLMD_LICENSE_FILE={ license_server } " )
82+
83+ run_args = dict (
84+ image = image_name ,
85+ command = ["-m" , "cosimgui" , f"--grpcport=0.0.0.0:{ port } " ],
86+ detach = True ,
87+ environment = environment ,
88+ remove = True ,
89+ ports = {f"{ port } /tcp" : f"{ port } " },
90+ volumes = [f"{ mounted_from } :{ mounted_to } " ],
91+ working_dir = f"{ mounted_to } " ,
92+ )
93+
94+ # Additional args
95+ if container_user :
96+ run_args ["user" ] = container_user
9497 if network :
95- idx = run_args .index ("-p" )
96- run_args .insert (idx , network )
97- run_args .insert (idx , "--network" )
98+ run_args ["network" ] = network
9899
99- subprocess . run ( run_args )
100- return port
100+ docker_client = docker . from_env ( )
101+ return docker_client . containers . run ( ** run_args )
101102
102103
103104def create_network (name ):
104- subprocess .run (["docker" , "network" , "create" , name ])
105+ import docker
106+
107+ LOG .debug (f"Creating docker network '{ name } '" )
108+ docker_client = docker .from_env ()
109+ docker_client .networks .create (name )
0 commit comments