Skip to content

Commit dcd7c37

Browse files
committed
Added support for containerizing the server
1 parent d874cc3 commit dcd7c37

9 files changed

Lines changed: 237 additions & 6 deletions

File tree

.dockerignore

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Git
2+
.git
3+
.gitignore
4+
.gitattributes
5+
6+
7+
# CI
8+
.codeclimate.yml
9+
.travis.yml
10+
.taskcluster.yml
11+
12+
# Docker
13+
docker-compose.yml
14+
Dockerfile
15+
.docker
16+
.dockerignore
17+
18+
# Byte-compiled / optimized / DLL files
19+
**/__pycache__/
20+
**/*.py[cod]
21+
22+
# C extensions
23+
*.so
24+
25+
# Distribution / packaging
26+
.Python
27+
env/
28+
build/
29+
develop-eggs/
30+
dist/
31+
downloads/
32+
eggs/
33+
lib/
34+
lib64/
35+
parts/
36+
sdist/
37+
var/
38+
*.egg-info/
39+
.installed.cfg
40+
*.egg
41+
42+
# PyInstaller
43+
# Usually these files are written by a python script from a template
44+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
45+
*.manifest
46+
*.spec
47+
48+
# Installer logs
49+
pip-log.txt
50+
pip-delete-this-directory.txt
51+
52+
# Unit test / coverage reports
53+
htmlcov/
54+
.tox/
55+
.coverage
56+
.cache
57+
nosetests.xml
58+
coverage.xml
59+
60+
# Translations
61+
*.mo
62+
*.pot
63+
64+
# Django stuff:
65+
*.log
66+
67+
# Sphinx documentation
68+
docs/_build/
69+
70+
# PyBuilder
71+
target/
72+
73+
# Virtual environment
74+
.env
75+
.venv/
76+
venv/
77+
78+
# PyCharm
79+
.idea
80+
81+
# Python mode for VIM
82+
.ropeproject
83+
**/.ropeproject
84+
85+
# Vim swap files
86+
**/*.swp
87+
88+
# VS Code
89+
.vscode/

Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Multi-stage build. Build published at: https://hub.docker.com/repository/docker/synacktra/cillow
2+
3+
FROM python:3.12-slim-bookworm AS builder
4+
5+
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
6+
7+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
8+
9+
RUN apt-get update && apt-get install -y --no-install-recommends \
10+
build-essential \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
WORKDIR /cillow
14+
15+
COPY . .
16+
17+
RUN uv pip install --system --no-cache .
18+
19+
RUN rm -rf /root/.cache \
20+
&& find /usr/local -depth \
21+
-type f -name '*.pyc' -delete \
22+
-o -type d -name __pycache__ -delete
23+
24+
25+
FROM python:3.12-slim-bookworm
26+
27+
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
28+
29+
COPY --from=builder /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/
30+
COPY --from=builder /usr/local/bin/ /usr/local/bin/

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# cillow
2-
3-
<p align="center">Code Interpreter Library</p>
1+
<p align="center">
2+
<img src="https://i.imgur.com/mBdWuQP.gif" width="550px" alt="cillow logo gif">
3+
</p>
44

55
---
66

client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cillow
2+
3+
client = cillow.Client.new(host="127.0.0.1", port=5556)
4+
5+
print(client.current_environment)
6+
7+
client.run_code("""
8+
from PIL import Image, ImageDraw
9+
10+
img = Image.new('RGB', (400, 300), 'white')
11+
12+
draw = ImageDraw.Draw(img)
13+
draw.rectangle([50, 50, 150, 150], fill='blue')
14+
draw.ellipse([200, 50, 300, 150], fill='red')
15+
draw.line([50, 200, 350, 200], fill='green', width=5)
16+
17+
img.show()
18+
""")
19+
20+
client.disconnect()

docs/quickstart/using_cillow.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ client = cillow.Client.new(
8181
)
8282
```
8383

84-
### Switching Environment
84+
### Switching Python Environment
8585

8686
> Every new environment starts up in a new interpreter process and has its own namespace.
8787
8888
```python
8989
client.switch_interpreter("/path/to/python/venv")
9090
```
9191

92-
### Deleting Environment
92+
### Deleting Python Environment
9393

9494
```python
9595
client.delete_interpreter("/path/to/python/env")
@@ -105,6 +105,8 @@ By default, the `run_code()` method automatically installs the required packages
105105
client.install_requirements("package1", "package2")
106106
```
107107

108+
To disable automatic installation feature in `run_code` method, set `CILLOW_DISABLE_AUTO_INSTALL` environment variable to `1`.
109+
108110
> If `uv` is installed, it will be used to install the requirements; otherwise, it will fallback to `pip`.
109111
110112
### Running Commands

docs/sandboxing_the_server.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
This guide will show you how to securely host the Cillow server using Docker, leveraging the Cillow base image for isolated and consistent deployments.
2+
3+
---
4+
5+
### 1. **Prerequisites**
6+
- Docker installed on your system
7+
- A [`server.py`](https://github.com/synacktraa/cillow/blob/main/server.py) file to configure and run the Cillow server. This file sets up and starts the server, optionally with custom patches or configurations (see [Using Cillow guide](./quickstart/using_cillow.md)).
8+
9+
---
10+
11+
### 2. **Creating the Dockerfile**
12+
Create a `Dockerfile` in the same directory as your `server.py` file:
13+
14+
```dockerfile
15+
FROM synacktra/cillow:latest
16+
17+
# Uncomment the following line if you want cillow to use uv for package installation
18+
# COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
19+
20+
WORKDIR /app
21+
22+
# Copy your server script into the container
23+
COPY server.py .
24+
25+
# Expose the port the server will listen on
26+
EXPOSE 5556
27+
28+
# Define the command to start the server
29+
CMD ["python", "server.py"]
30+
```
31+
32+
---
33+
34+
### 3. **Building the Docker Image**
35+
Run the following command to build the Docker image. The `-t` flag tags the image as `cillow-server`:
36+
37+
```bash
38+
docker build -t cillow-server .
39+
```
40+
41+
---
42+
43+
### 4. **Running the Docker Container**
44+
Launch the container using the built image. The `--init` flag ensures proper process handling, and `-it` keeps the container interactive:
45+
46+
```bash
47+
docker run --init -it -p 5556:5556 --name cillow-server cillow-server
48+
```
49+
50+
This maps port `5556` from the container to your local machine, enabling client connections.
51+
52+
---
53+
54+
### 5. **Accessing the Sandboxed Server**
55+
Once the container is running, the Cillow server will be available at `localhost:5556`. Use `cillow.Client` to interact with the server as explained in the [Using Cillow guide](./quickstart/using_cillow.md#interacting-with-the-server).
56+
57+
---
58+
59+
### 6. **Customizing Your Server**
60+
Enhance your server with additional features by:
61+
- Adding custom patches
62+
- Configuring interpreter limits or other server parameters
63+
64+
Simply modify your `server.py`, rebuild the Docker image, and restart the container with updated settings.

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ nav:
1414
- Using Cillow: quickstart/using_cillow.md
1515
- Custom Patches: quickstart/custom_patches.md
1616
- Connecting LLMs & Frameworks: quickstart/connecting_LLM_&_Frameworks.md
17-
17+
- Sandboxing The Server: sandboxing_the_server.md
1818
- Supported Languages: supported_languages.md
1919

2020
- Cookbooks: cookbooks.md

server.Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM synacktra/cillow:latest
2+
3+
# Uncomment the following line if you want cillow to use uv for package installation
4+
# COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
5+
6+
WORKDIR /app
7+
8+
# Copy your server script into the container
9+
COPY server.py .
10+
11+
# Expose the port the server will listen on
12+
EXPOSE 5556
13+
14+
# Define the command to start the server
15+
CMD ["python", "server.py"]

server.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cillow
2+
3+
cillow.add_patches(
4+
cillow.prebuilt_patches.patch_stdout_stderr_write,
5+
cillow.prebuilt_patches.patch_matplotlib_pyplot_show,
6+
cillow.prebuilt_patches.patch_pillow_show,
7+
)
8+
9+
if __name__ == "__main__":
10+
server = cillow.Server(port=5556, max_interpreters=2, interpreters_per_client=1)
11+
server.run()

0 commit comments

Comments
 (0)