ds_pycontain code documentation
Submodules
ds_pycontain.docker_containers module
- class DockerContainer(image: DockerImage, **kwargs: Any)
Bases:
object
An isolated environment for running commands, based on docker container.
Examples:
If you need to run container for a single job:
>>> container = DockerContainer(DockerImage.from_tag("alpine")) >>> status_code, logs = container.spawn_run("echo hello world")
To run a container in background and execute commands:
>>> with DockerContainer(DockerImage.from_tag("alpine")) as container: >>> status_code, logs = container.run("echo hello world")
- Parameters:
image (DockerImage) –
kwargs (Any) –
- __enter__() DockerContainer
Enters container context. It means that container is started and you can execute commands inside it.
- Return type:
- __exit__(exc_type: Optional[Type[BaseException]], exc: Optional[BaseException], traceback: Optional[TracebackType]) bool
Cleanup container on exit. :param exc_type: exception type :param exc: exception instance (unused) :param traceback: traceback object (unused) :return: True if exception was handled, False otherwise
- Parameters:
exc_type (Optional[Type[BaseException]]) –
exc (Optional[BaseException]) –
traceback (Optional[TracebackType]) –
- Return type:
- property docker_container: Container
Returns docker container object. :return: docker container object
- property name: str
Name of the container if it exists, empty string otherwise. :return: container name as string.
- run(command: Union[str, List[str]], **kwargs: Any) Tuple[int, Union[bytes, Tuple[bytes, bytes], Generator[bytes, None, None]]]
Run a script in the isolated environment which is docker container. You can send any args which docker-py exec_run accepts: https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.Container.exec_run Return is a tuple of exit code and output which is controlled by arguments: stream, socket and demux.
- spawn_run(command: Union[str, List[str]], **kwargs: Any) Tuple[int, bytes]
Run a script in the isolated environment which is docker container with the same lifetime as this function call.
You can also pass all arguments that docker client containers.run() accepts. It blocks till command is finished.
- class DockerImage(name: str)
Bases:
object
Represents a locally available docker image as a tag. You can either use existing docker image or build a new one from Dockerfile.
>>> image = DockerImage.from_tag("alpine") >>> image = DockerImage.from_tag("python", tag="3.9-slim") >>> image = DockerImage.from_dockerfile("example/Dockerfile") >>> image = DockerImage.from_dockerfile("path/to/dir_with_Dockerfile/", name="cow")
- Parameters:
name (str) –
- __repr__() str
String representation of the object. :return: string representation of the object (container name).
- Return type:
- classmethod exists(name: str) bool
Checks if the docker image exists. :param name: docker image name with tag, e.g. “alpine:latest” :return: True if docker image exists, False otherwise
- classmethod from_dockerfile(dockerfile_path: ~typing.Union[~pathlib.Path, str], name: ~typing.Union[str, ~typing.Callable[[], str]] = <function generate_random_container_tag>, **kwargs: ~typing.Any) DockerImage
Build a new image from Dockerfile given its file path.
- Parameters:
- Return type:
defaults to generate_random_container_tag() :param kwargs: additional arguments to pass to docker client images.build() :return: DockerImage object representing built image on the system. :raises ValueError: if dockerfile_path is not a valid path to Dockerfile.
- classmethod from_dockerfile_content(dockerfile_str: str, name: ~typing.Union[str, ~typing.Callable[[], str]] = <function generate_random_container_tag>, **kwargs: ~typing.Any) DockerImage
Build a new image from Dockerfile given a string with Dockerfile content.
- Parameters:
- Return type:
defaults to generate_random_container_tag() :param kwargs: additional arguments to pass to docker client images.build() :return: DockerImage object representing built image on the system.
- classmethod from_tag(repository: str, tag: str = 'latest', auth_config: Optional[Dict[str, str]] = None) DockerImage
Use image with a given repository and tag. It is going to pull it if it is not present on the system.
Examples:
>>> repository = "alpine" # (will get "latest" tag) >>> repository = "python", tag = "3.9-slim"
- Parameters:
- Returns:
DockerImage object representing pulled image on the system.
- Return type:
ds_pycontain.python_dockerized_repl module
- class PythonContainerREPL(port: int = 7123, image: Optional[DockerImage] = None, base_image: str = 'python:3.11-alpine3.18', docker_startup_timeout: float = 300.0, repl_startup_timeout: float = 3.0, **kwargs: Dict[str, Any])
Bases:
object
This class is a wrapper around the docker container that runs the python REPL server. It is used to execute python code in the container and return the results.
It assumes specific docker image is used which runs langchain python runner server and it communicates by HTTP requests.
- Parameters:
Module contents
ds_pycontain is a Python package for managing Docker containers and images.
- class DockerContainer(image: DockerImage, **kwargs: Any)
Bases:
object
An isolated environment for running commands, based on docker container.
Examples:
If you need to run container for a single job:
>>> container = DockerContainer(DockerImage.from_tag("alpine")) >>> status_code, logs = container.spawn_run("echo hello world")
To run a container in background and execute commands:
>>> with DockerContainer(DockerImage.from_tag("alpine")) as container: >>> status_code, logs = container.run("echo hello world")
- Parameters:
image (DockerImage) –
kwargs (Any) –
- __enter__() DockerContainer
Enters container context. It means that container is started and you can execute commands inside it.
- Return type:
- __exit__(exc_type: Optional[Type[BaseException]], exc: Optional[BaseException], traceback: Optional[TracebackType]) bool
Cleanup container on exit. :param exc_type: exception type :param exc: exception instance (unused) :param traceback: traceback object (unused) :return: True if exception was handled, False otherwise
- Parameters:
exc_type (Optional[Type[BaseException]]) –
exc (Optional[BaseException]) –
traceback (Optional[TracebackType]) –
- Return type:
- property docker_container: Container
Returns docker container object. :return: docker container object
- property name: str
Name of the container if it exists, empty string otherwise. :return: container name as string.
- run(command: Union[str, List[str]], **kwargs: Any) Tuple[int, Union[bytes, Tuple[bytes, bytes], Generator[bytes, None, None]]]
Run a script in the isolated environment which is docker container. You can send any args which docker-py exec_run accepts: https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.Container.exec_run Return is a tuple of exit code and output which is controlled by arguments: stream, socket and demux.
- spawn_run(command: Union[str, List[str]], **kwargs: Any) Tuple[int, bytes]
Run a script in the isolated environment which is docker container with the same lifetime as this function call.
You can also pass all arguments that docker client containers.run() accepts. It blocks till command is finished.
- class DockerImage(name: str)
Bases:
object
Represents a locally available docker image as a tag. You can either use existing docker image or build a new one from Dockerfile.
>>> image = DockerImage.from_tag("alpine") >>> image = DockerImage.from_tag("python", tag="3.9-slim") >>> image = DockerImage.from_dockerfile("example/Dockerfile") >>> image = DockerImage.from_dockerfile("path/to/dir_with_Dockerfile/", name="cow")
- Parameters:
name (str) –
- __repr__() str
String representation of the object. :return: string representation of the object (container name).
- Return type:
- classmethod exists(name: str) bool
Checks if the docker image exists. :param name: docker image name with tag, e.g. “alpine:latest” :return: True if docker image exists, False otherwise
- classmethod from_dockerfile(dockerfile_path: ~typing.Union[~pathlib.Path, str], name: ~typing.Union[str, ~typing.Callable[[], str]] = <function generate_random_container_tag>, **kwargs: ~typing.Any) DockerImage
Build a new image from Dockerfile given its file path.
- Parameters:
- Return type:
defaults to generate_random_container_tag() :param kwargs: additional arguments to pass to docker client images.build() :return: DockerImage object representing built image on the system. :raises ValueError: if dockerfile_path is not a valid path to Dockerfile.
- classmethod from_dockerfile_content(dockerfile_str: str, name: ~typing.Union[str, ~typing.Callable[[], str]] = <function generate_random_container_tag>, **kwargs: ~typing.Any) DockerImage
Build a new image from Dockerfile given a string with Dockerfile content.
- Parameters:
- Return type:
defaults to generate_random_container_tag() :param kwargs: additional arguments to pass to docker client images.build() :return: DockerImage object representing built image on the system.
- classmethod from_tag(repository: str, tag: str = 'latest', auth_config: Optional[Dict[str, str]] = None) DockerImage
Use image with a given repository and tag. It is going to pull it if it is not present on the system.
Examples:
>>> repository = "alpine" # (will get "latest" tag) >>> repository = "python", tag = "3.9-slim"
- Parameters:
- Returns:
DockerImage object representing pulled image on the system.
- Return type: