Skip to content

Commit 64a893a

Browse files
authored
feat(sandbox/ollama): allow updating ollama from within the sandbox (#48)
Move ollama binary to /sandbox/bin/ (writable at runtime) so it can be replaced without rebuilding the container image. Add update-ollama scriptthat downloads a specific version or latest, and support OLLAMA_UPDATE=1 env var for auto-update on startup.
1 parent 88812df commit 64a893a

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

sandboxes/ollama/Dockerfile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@ USER root
1717
RUN apt-get update && apt-get install -y --no-install-recommends zstd \
1818
&& rm -rf /var/lib/apt/lists/*
1919

20-
# Install Ollama
21-
RUN curl -fsSL https://ollama.com/install.sh | sh
20+
# Install Ollama into /sandbox/bin so it lives on a writable path and can be
21+
# updated at runtime (the sandbox policy makes /usr read-only).
22+
RUN mkdir -p /sandbox/bin && \
23+
curl -fsSL https://ollama.com/install.sh | sh && \
24+
mv /usr/local/bin/ollama /sandbox/bin/ollama && \
25+
chown -R sandbox:sandbox /sandbox/bin
2226

2327
# Copy sandbox policy
2428
COPY policy.yaml /etc/openshell/policy.yaml
2529

26-
# Copy entrypoint script
30+
# Copy entrypoint and update scripts
2731
COPY entrypoint.sh /usr/local/bin/entrypoint
28-
RUN chmod +x /usr/local/bin/entrypoint
32+
COPY update-ollama.sh /sandbox/bin/update-ollama
33+
RUN chmod +x /usr/local/bin/entrypoint /sandbox/bin/update-ollama
2934

3035
# Set environment variables for OpenShell provider discovery
36+
# /sandbox/bin comes first so the writable ollama binary is preferred
3137
ENV OLLAMA_HOST=http://127.0.0.1:11434 \
3238
NPM_CONFIG_PREFIX=/sandbox/.npm-global \
33-
PATH="/sandbox/.npm-global/bin:/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin"
39+
PATH="/sandbox/bin:/sandbox/.npm-global/bin:/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin"
3440

3541
# Configure npm to install globals into a writable directory
3642
# (the sandbox policy makes /usr read-only, so the default /usr/lib/node_modules fails)
@@ -40,7 +46,7 @@ RUN mkdir -p /sandbox/.npm-global && \
4046
# Add environment variables to .bashrc for interactive shells
4147
RUN echo 'export OLLAMA_HOST=http://127.0.0.1:11434' >> /sandbox/.bashrc && \
4248
echo 'export NPM_CONFIG_PREFIX=/sandbox/.npm-global' >> /sandbox/.bashrc && \
43-
echo 'export PATH="/sandbox/.npm-global/bin:$PATH"' >> /sandbox/.bashrc && \
49+
echo 'export PATH="/sandbox/bin:/sandbox/.npm-global/bin:$PATH"' >> /sandbox/.bashrc && \
4450
chown sandbox:sandbox /sandbox/.bashrc
4551

4652
USER sandbox

sandboxes/ollama/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ docker build -t openshell-ollama .
2626
openshell sandbox create --from ollama
2727
```
2828

29+
### Update Ollama inside the sandbox
30+
31+
```bash
32+
update-ollama
33+
```
34+
35+
Or auto-update on startup:
36+
37+
```bash
38+
openshell sandbox create --from ollama -e OLLAMA_UPDATE=1
39+
```
40+

sandboxes/ollama/entrypoint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ set -euo pipefail
99
# Export OLLAMA_HOST for OpenShell provider discovery
1010
export OLLAMA_HOST="${OLLAMA_HOST:-http://127.0.0.1:11434}"
1111

12+
# Update Ollama if requested
13+
if [ "${OLLAMA_UPDATE:-0}" = "1" ]; then
14+
echo "[ollama] Updating to latest version..."
15+
update-ollama
16+
fi
17+
1218
# Start Ollama server in background
1319
echo "[ollama] Starting Ollama server..."
1420
nohup ollama serve > /tmp/ollama.log 2>&1 &

sandboxes/ollama/policy.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ network_policies:
4545
- { host: github.com, port: 443 }
4646
- { host: objects.githubusercontent.com, port: 443 }
4747
- { host: raw.githubusercontent.com, port: 443 }
48+
- { host: release-assets.githubusercontent.com, port: 443 }
4849
binaries:
4950
- { path: /usr/bin/curl }
5051
- { path: /bin/bash }
5152
- { path: /usr/bin/sh }
53+
- { path: /sandbox/bin/ollama }
5254
- { path: /usr/local/bin/ollama }
5355
- { path: /usr/bin/ollama }
5456

sandboxes/ollama/update-ollama.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# Update Ollama inside the sandbox.
6+
# Usage: update-ollama [VERSION]
7+
# update-ollama # install latest
8+
# update-ollama 0.18.1 # install specific version
9+
set -euo pipefail
10+
11+
OLLAMA_BIN="/sandbox/bin/ollama"
12+
VERSION="${1:-}"
13+
14+
ARCH=$(uname -m)
15+
case "$ARCH" in
16+
x86_64) ARCH="amd64" ;;
17+
aarch64) ARCH="arm64" ;;
18+
arm64) ARCH="arm64" ;;
19+
*) echo "Unsupported architecture: $ARCH"; exit 1 ;;
20+
esac
21+
22+
CURRENT=$("$OLLAMA_BIN" --version 2>&1 | grep -oP 'version is \K[0-9.]+' || echo "unknown")
23+
24+
if [ -n "$VERSION" ]; then
25+
URL="https://github.com/ollama/ollama/releases/download/v${VERSION}/ollama-linux-${ARCH}.tar.zst"
26+
echo "Current version: ${CURRENT}"
27+
echo "Downloading ollama v${VERSION} for linux/${ARCH}..."
28+
else
29+
URL="https://ollama.com/download/ollama-linux-${ARCH}.tar.zst"
30+
echo "Current version: ${CURRENT}"
31+
echo "Downloading latest ollama for linux/${ARCH}..."
32+
fi
33+
34+
TMPDIR=$(mktemp -d)
35+
trap 'rm -rf "$TMPDIR"' EXIT
36+
37+
curl -fsSL "$URL" -o "$TMPDIR/ollama.tar.zst"
38+
tar --zstd -xf "$TMPDIR/ollama.tar.zst" -C "$TMPDIR"
39+
40+
mv "$TMPDIR/bin/ollama" "$OLLAMA_BIN"
41+
chmod +x "$OLLAMA_BIN"
42+
43+
NEW=$("$OLLAMA_BIN" --version 2>&1 | grep -oP 'version is \K[0-9.]+' || echo "unknown")
44+
45+
echo "Updated: ${CURRENT} -> ${NEW}"
46+
echo "Restart the Ollama server to use the new version:"
47+
echo " pkill ollama; nohup ollama serve > /tmp/ollama.log 2>&1 &"

0 commit comments

Comments
 (0)