Exemple #1
0
 def exec_in_container(
     self,
     container_name_or_id: str,
     command: Union[List[str], str],
     interactive=False,
     detach=False,
     env_vars: Optional[Dict[str, Optional[str]]] = None,
     stdin: Optional[bytes] = None,
     user: Optional[str] = None,
     workdir: Optional[str] = None,
 ) -> Tuple[bytes, bytes]:
     env_file = None
     cmd = self._docker_cmd()
     cmd.append("exec")
     if interactive:
         cmd.append("--interactive")
     if detach:
         cmd.append("--detach")
     if user:
         cmd += ["--user", user]
     if workdir:
         cmd += ["--workdir", workdir]
     if env_vars:
         env_flag, env_file = Util.create_env_vars_file_flag(env_vars)
         cmd += env_flag
     cmd.append(container_name_or_id)
     cmd += command if isinstance(command, List) else [command]
     LOG.debug("Execute in container cmd: %s", cmd)
     result = self._run_async_cmd(cmd, stdin, container_name_or_id)
     Util.rm_env_vars_file(env_file)
     return result
Exemple #2
0
 def run_container(self,
                   image_name: str,
                   stdin=None,
                   **kwargs) -> Tuple[bytes, bytes]:
     cmd, env_file = self._build_run_create_cmd("run", image_name, **kwargs)
     LOG.debug("Run container with cmd: %s", cmd)
     result = self._run_async_cmd(cmd, stdin,
                                  kwargs.get("name") or "", image_name)
     Util.rm_env_vars_file(env_file)
     return result
Exemple #3
0
 def create_container(self, image_name: str, **kwargs) -> str:
     cmd, env_file = self._build_run_create_cmd("create", image_name, **kwargs)
     LOG.debug("Create container with cmd: %s", cmd)
     try:
         container_id = run(cmd)
         # Note: strip off Docker warning messages like "DNS setting (--dns=127.0.0.1) may fail in containers"
         container_id = container_id.strip().split("\n")[-1]
         return container_id.strip()
     except subprocess.CalledProcessError as e:
         if "Unable to find image" in to_str(e.stdout):
             raise NoSuchImage(image_name, stdout=e.stdout, stderr=e.stderr)
         raise ContainerException(
             "Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
         )
     finally:
         Util.rm_env_vars_file(env_file)