Skip to content

Commit

Permalink
feat: support Docker remote host
Browse files Browse the repository at this point in the history
  • Loading branch information
vndee committed Jul 9, 2024
1 parent 2bb3940 commit bc5411c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ with SandboxSession(lang="python", keep_template=True) as session:

For other languages usage, please refer to the [examples](examples/code_runner.py).

You can also use [remote Docker host](https://docs.docker.com/config/daemon/remote-access/) as below:

```python
from llm_sandbox import SandboxSession

with SandboxSession(
mage="python:3.9.19-bullseye",
keep_template=True,
lang="python",
docker_host="tcp://<your_host>:<port>",
docker_tls_verify=True,
docker_cert_path="path/to/cert/dir"
) as session:
result = session.run("print('Hello, World!')")
print(result)
```


### API Reference

#### `SandboxSession`
Expand Down Expand Up @@ -131,7 +149,7 @@ Here is a list of things you can do to contribute:
- [x] Add support for C++.
- [x] Add support for Go.
- [ ] Add support for Ruby.
- [ ] Add remote Docker host support.
- [x] Add remote Docker host support.
- [ ] Add remote Kubernetes cluster support.
- [ ] Release version 1.0.0.

Expand Down
30 changes: 29 additions & 1 deletion llm_sandbox/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def __init__(
lang: str = SupportedLanguage.PYTHON,
keep_template: bool = False,
verbose: bool = True,
docker_host: Optional[str] = None,
docker_tls_verify: Optional[bool] = None,
docker_cert_path: Optional[str] = None,
):
"""
Create a new sandbox session
Expand All @@ -37,6 +40,9 @@ def __init__(
:param lang: Language of the code
:param keep_template: if True, the image and container will not be removed after the session ends
:param verbose: if True, print messages
:param docker_host: The URL of the Docker host. For example, tcp://localhost:1234.
:param docker_tls_verify: Whether to verify the server's certificate or not.
:param docker_cert_path: Path to the directory containing the CA certificate, client certificate, and client key.
"""
if image and dockerfile:
raise ValueError("Only one of image or dockerfile should be provided")
Expand All @@ -50,7 +56,7 @@ def __init__(
image = DefaultImage.__dict__[lang.upper()]

self.lang: str = lang
self.client: docker.DockerClient = docker.from_env()
self.client: docker.DockerClient = self._create_docker_client(docker_host, docker_tls_verify, docker_cert_path)
self.image: Union[Image, str] = image
self.dockerfile: Optional[str] = dockerfile
self.container: Optional[Container] = None
Expand All @@ -59,6 +65,28 @@ def __init__(
self.is_create_template: bool = False
self.verbose = verbose

@staticmethod
def _create_docker_client(docker_host: Optional[str], docker_tls_verify: Optional[bool], docker_cert_path: Optional[str]) -> docker.DockerClient:
"""
Create a new Docker client
:param docker_host: The URL of the Docker host. For example, tcp://localhost:1234.
:param docker_tls_verify: Whether to verify the server's certificate or not.
:param docker_cert_path: Path to the directory containing the CA certificate, client certificate, and client key.
:return: Docker client
"""
if docker_host:
tls_config = None
if docker_tls_verify and docker_cert_path:
tls_config = docker.tls.TLSConfig(
client_cert=(os.path.join(docker_cert_path, "cert.pem"), os.path.join(docker_cert_path, "key.pem")),
ca_cert=os.path.join(docker_cert_path, "ca.pem"),
verify=docker_tls_verify
)

return docker.DockerClient(base_url=docker_host, tls=tls_config)

return docker.from_env()

def open(self):
warning_str = (
"Since the `keep_template` flag is set to True the docker image will not be removed after the session ends "
Expand Down

0 comments on commit bc5411c

Please sign in to comment.