Skip to content

Commit

Permalink
deploy: 46d3557
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-grodek-dsai committed Nov 3, 2023
1 parent 73f24aa commit 140a746
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 59 deletions.
8 changes: 8 additions & 0 deletions _sources/api/ds_pycontain.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ ds_pycontain.docker_containers module
:undoc-members:
:show-inheritance:

ds_pycontain.python_dockerized_repl module
----------------------------------------------------

.. automodule:: ds_pycontain.python_dockerized_repl
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

Expand Down
25 changes: 1 addition & 24 deletions _sources/code_documentation.md.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
# Code documentation

**ds_pycontain** is a python package which provides an abstraction over the docker API.

Supported functionality covers:
- Building docker images from Dockerfiles
- Pulling docker images from dockerhub (or similar)
- Running docker containers to execute a one-off command
- Running docker containers to execute a long-running process and communicate with it


```python
from ds_pycontain import DockerContainer, DockerImage, get_docker_client

client = get_docker_client()

# This will fetch the image from dockerhub if it is not already present
# with the "latest" tag. Then container is started and commands are run
with DockerContainer(DockerImage.from_tag("alpine")) as container:
ret_code, output = container.run("touch /animal.txt")
assert ret_code == 0

ret_code, output = container.run("ls /")
assert ret_code == 0
assert cast(bytes, output).find(b"animal.txt") >= 0
```
This is the documentation for the code of the project API.

```{toctree}
---
Expand Down
5 changes: 2 additions & 3 deletions _sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
`deepsense.ai <https://deepsense.ai>`_ - pycontain
====================================================================================================================

Documentation for **ds_pycontain** python package to work with docker containers and images.

Example use case you might consider is to isolate python code execution generated by untrusted LLM by running it in a docker container.
Documentation for **ds_pycontain** python package to work with docker containers and images, as well as providing python REPL running in a container.

This package makes it a bit easier to:

* Build docker images from Dockerfiles or in-memory string.
* Pull docker images from dockerhub (or similar).
* Run docker container to execute a one-off command.
* Run docker container to execute a long-running process and communicate with it.
* Run python code in a docker container and communicate with it.


.. toctree::
Expand Down
1 change: 1 addition & 0 deletions _sources/licenses.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ Licenses
=====================

List of automatically detected licenses of all detected python packages with `pip-licenses`.
They are all projects that are used in the project development.

.. include:: licenses_table.rst
90 changes: 89 additions & 1 deletion _sources/project_overview.md.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,94 @@
# Project overview

**ds_pycontain** is a python package which provides an abstraction over the docker API and provide Python REPL running in a docker container.

Supported functionality covers:
- Building docker images from Dockerfiles
- Pulling docker images from dockerhub (or similar)
- Running docker containers to execute a one-off command
- Running docker containers to execute a long-running process and communicate with it
- Run python commands in a container and get the result.

## Motivation

Main motivation is to allow to orchestrate running unsafe code or commands in isolated environment.
The docker API is quite complicated and not well documented or typed.
This project aims to provide a higher level abstraction over the docker API.

Main motivation is to allow to orchestrate running unsafe code or commands in isolated environment.
What is also provided is **a python REPL running in a docker container**.

This might be useful to improve security for execution of LLM models/agents generated code, which generally should not be trusted.

## Example code snippets

### Execute commands in container running in the background:

Below is a short snippet showcasing how to run docker container in the background and execute commands in it.

```python
from ds_pycontain import DockerContainer, DockerImage, get_docker_client

client = get_docker_client()

# This will fetch the image from dockerhub if it is not already present
# with the "latest" tag. Then container is started and commands are run
with DockerContainer(DockerImage.from_tag("alpine")) as container:
ret_code, output = container.run("touch /animal.txt")
assert ret_code == 0

ret_code, output = container.run("ls /")
assert ret_code == 0
assert cast(bytes, output).find(b"animal.txt") >= 0
```

### Docker images

Images can be pulled from dockerhub or built from dockerfile.

```python
from ds_pycontain import DockerImage

# pull or use alpine:latest
image = DockerImage.from_tag("alpine")
# use provided tag to pull/use the image
image = DockerImage.from_tag("python", tag="3.9-slim")
# use this dockerfile to build a new local image
image = DockerImage.from_dockerfile("example/Dockerfile")
# you can provide a directory path which contains Dockerfile, set custom image name
image = DockerImage.from_dockerfile("path/to/dir_with_Dockerfile/", name="cow")
```

### Python REPL running in docker container

Running Python code in docker container is rather easy with this package.

```python
from ds_pycontain.python_dockerized_repl import PythonContainerREPL

# To start python REPL in container it is easy,
# just be aware that it will take some time to start the container
# and ports might be allocated by OS, so use different port/retry
# if you get error.
repl = PythonContainerREPL(port=7121)

# You can run python commands in the container
# and it will keep state between commands.
out1 = repl.exec("x = [1, 2, 3]")
assert out1 == ""
# Eval returns string representation of the python command
# as it would be in python REPL:
out2 = repl.eval("len(x)")
assert out2 == "3"

# Exec returns captured standard output (stdout)
# so it won't return anything in this case:
out3 = repl.exec("len(x)")
assert out3 == ""
# but exec with print works:
out4 = repl.exec("print(len(x))")
assert out4 == "3\n"

# You can also get error messages if code is wrong:
err = repl.exec("print(x")
assert "SyntaxError" in err
```
91 changes: 91 additions & 0 deletions api/ds_pycontain.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<li class="toctree-l3 current"><a class="current reference internal" href="#">ds_pycontain code documentation</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#submodules">Submodules</a></li>
<li class="toctree-l4"><a class="reference internal" href="#module-ds_pycontain.docker_containers">ds_pycontain.docker_containers module</a></li>
<li class="toctree-l4"><a class="reference internal" href="#module-ds_pycontain.python_dockerized_repl">ds_pycontain.python_dockerized_repl module</a></li>
<li class="toctree-l4"><a class="reference internal" href="#module-ds_pycontain">Module contents</a></li>
</ul>
</li>
Expand Down Expand Up @@ -405,6 +406,96 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Permalink to this
</dl>
</dd></dl>

</section>
<section id="module-ds_pycontain.python_dockerized_repl">
<span id="ds-pycontain-python-dockerized-repl-module"></span><h2>ds_pycontain.python_dockerized_repl module<a class="headerlink" href="#module-ds_pycontain.python_dockerized_repl" title="Permalink to this heading"></a></h2>
<dl class="py class">
<dt class="sig sig-object py" id="ds_pycontain.python_dockerized_repl.PythonContainerREPL">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PythonContainerREPL</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">port</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">7123</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">image</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.12)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference internal" href="#ds_pycontain.docker_containers.DockerImage" title="ds_pycontain.docker_containers.DockerImage"><span class="pre">DockerImage</span></a><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">base_image</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">'python:3.11-alpine3.18'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Dict" title="(in Python v3.12)"><span class="pre">Dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.12)"><span class="pre">Any</span></a><span class="p"><span class="pre">]</span></span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ds_pycontain.python_dockerized_repl.PythonContainerREPL" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <a class="reference external" href="https://docs.python.org/3/library/functions.html#object" title="(in Python v3.12)"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a></p>
<p>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.</p>
<p>It assumes specific docker image is used which runs langchain python runner
server and it communicates by HTTP requests.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>port</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><em>int</em></a>) – </p></li>
<li><p><strong>image</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.12)"><em>Optional</em></a><em>[</em><a class="reference internal" href="#ds_pycontain.docker_containers.DockerImage" title="ds_pycontain.docker_containers.DockerImage"><em>DockerImage</em></a><em>]</em>) – </p></li>
<li><p><strong>base_image</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><em>str</em></a>) – </p></li>
<li><p><strong>kwargs</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Dict" title="(in Python v3.12)"><em>Dict</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><em>str</em></a><em>, </em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.12)"><em>Any</em></a><em>]</em>) – </p></li>
</ul>
</dd>
</dl>
<dl class="py method">
<dt class="sig sig-object py" id="ds_pycontain.python_dockerized_repl.PythonContainerREPL.__del__">
<span class="sig-name descname"><span class="pre">__del__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/constants.html#None" title="(in Python v3.12)"><span class="pre">None</span></a></span></span><a class="headerlink" href="#ds_pycontain.python_dockerized_repl.PythonContainerREPL.__del__" title="Permalink to this definition"></a></dt>
<dd><p>Closes container and removes it.</p>
<dl class="field-list simple">
<dt class="field-odd">Return type<span class="colon">:</span></dt>
<dd class="field-odd"><p>None</p>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="ds_pycontain.python_dockerized_repl.PythonContainerREPL.eval">
<span class="sig-name descname"><span class="pre">eval</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a></span></span><a class="headerlink" href="#ds_pycontain.python_dockerized_repl.PythonContainerREPL.eval" title="Permalink to this definition"></a></dt>
<dd><p>Evaluate code and return result as string.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>code</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><em>str</em></a>) – code to evaluate.</p>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p>result as string.</p>
</dd>
<dt class="field-odd">Return type<span class="colon">:</span></dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)">str</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="ds_pycontain.python_dockerized_repl.PythonContainerREPL.exec">
<span class="sig-name descname"><span class="pre">exec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a></span></span><a class="headerlink" href="#ds_pycontain.python_dockerized_repl.PythonContainerREPL.exec" title="Permalink to this definition"></a></dt>
<dd><p>Execute code and return stdout.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>code</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><em>str</em></a>) – code to execute.</p>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p>result as string.</p>
</dd>
<dt class="field-odd">Return type<span class="colon">:</span></dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)">str</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="ds_pycontain.python_dockerized_repl.PythonContainerREPL.run">
<span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">command</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.12)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><span class="pre">str</span></a></span></span><a class="headerlink" href="#ds_pycontain.python_dockerized_repl.PythonContainerREPL.run" title="Permalink to this definition"></a></dt>
<dd><p>Run command and returns anything printed.
Timeout, if provided, is not currently supported and will be ignored.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>command</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)"><em>str</em></a>) – command to run.</p></li>
<li><p><strong>timeout</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.12)"><em>Optional</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><em>int</em></a><em>]</em>) – timeout in seconds.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p>result from REPL as output.</p>
</dd>
<dt class="field-odd">Return type<span class="colon">:</span></dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.12)">str</a></p>
</dd>
</dl>
</dd></dl>

</dd></dl>

</section>
<section id="module-ds_pycontain">
<span id="module-contents"></span><h2>Module contents<a class="headerlink" href="#module-ds_pycontain" title="Permalink to this heading"></a></h2>
Expand Down
Loading

0 comments on commit 140a746

Please sign in to comment.