Skip to content

Commit fc0c062

Browse files
YedPoolclaude
andcommitted
Add missing Docker configuration files for Ollama container
- Added Dockerfile with Ollama + Python 3.11 setup - Added .dockerignore for build optimization - Updated cerebrium.toml and main.py for Docker deployment - Enables GitHub Actions testing of container build 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3933eff commit fc0c062

4 files changed

Lines changed: 124 additions & 46 deletions

File tree

cerebrium/.dockerignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Ignore unnecessary files for Docker build
2+
__pycache__/
3+
*.pyc
4+
*.pyo
5+
*.pyd
6+
.Python
7+
env/
8+
pip-log.txt
9+
pip-delete-this-directory.txt
10+
.tox/
11+
.coverage
12+
.coverage.*
13+
.cache
14+
nosetests.xml
15+
coverage.xml
16+
*.cover
17+
*.log
18+
.git/
19+
.mypy_cache/
20+
.pytest_cache/
21+
.hypothesis/
22+
23+
# OS files
24+
.DS_Store
25+
.DS_Store?
26+
._*
27+
.Spotlight-V100
28+
.Trashes
29+
ehthumbs.db
30+
Thumbs.db
31+
32+
# IDE files
33+
.vscode/
34+
.idea/
35+
*.swp
36+
*.swo
37+
*~

cerebrium/Dockerfile

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Custom Ollama + Voice Agent Docker Image
2+
FROM ollama/ollama:latest
3+
4+
# Install Python and system dependencies
5+
RUN apt-get update && apt-get install -y \
6+
python3.11 \
7+
python3-pip \
8+
python3.11-venv \
9+
curl \
10+
wget \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
# Create symbolic links for python commands
14+
RUN ln -s /usr/bin/python3.11 /usr/bin/python
15+
RUN ln -s /usr/bin/pip3 /usr/bin/pip
16+
17+
# Set working directory
18+
WORKDIR /app
19+
20+
# Copy Python requirements and install dependencies
21+
COPY requirements.txt .
22+
RUN pip install --no-cache-dir -r requirements.txt
23+
24+
# Copy application code
25+
COPY main.py .
26+
COPY *.py .
27+
28+
# Create startup script that runs both Ollama and our app
29+
RUN cat > /app/start.sh << 'EOF'
30+
#!/bin/bash
31+
set -e
32+
33+
echo "🚀 Starting Ollama + Voice Agent Container"
34+
35+
# Start Ollama in background
36+
echo "📦 Starting Ollama server..."
37+
ollama serve &
38+
OLLAMA_PID=$!
39+
40+
# Wait for Ollama to be ready
41+
echo "⏳ Waiting for Ollama to be ready..."
42+
for i in {1..30}; do
43+
if curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
44+
echo "✅ Ollama is ready!"
45+
break
46+
fi
47+
echo " Attempt $i/30 - waiting..."
48+
sleep 2
49+
done
50+
51+
# Download the model we need
52+
echo "📥 Downloading llama3.2:1b model..."
53+
ollama pull llama3.2:1b
54+
55+
# Start our voice agent
56+
echo "🎤 Starting Voice Agent..."
57+
python main.py
58+
59+
# Clean shutdown
60+
trap 'kill $OLLAMA_PID' EXIT
61+
wait
62+
EOF
63+
64+
RUN chmod +x /app/start.sh
65+
66+
# Expose ports
67+
EXPOSE 8600 11434
68+
69+
# Health check
70+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
71+
CMD curl -f http://localhost:11434/api/tags || exit 1
72+
73+
# Start both services
74+
CMD ["/app/start.sh"]

cerebrium/cerebrium.toml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@ max_replicas = 3
1515
cooldown = 120
1616

1717
[cerebrium.build]
18-
dockerfile = false
19-
python_packages = "./requirements.txt"
20-
include = [".", "*.py"]
21-
# Install system dependencies then download models
22-
shell_commands = [
23-
"apt-get update && apt-get install -y curl",
24-
"curl -fsSL https://ollama.com/install.sh | sh",
25-
"python main.py download-files"
26-
]
18+
dockerfile = "./Dockerfile"
19+
include = [".", "*.py", "requirements.txt", "Dockerfile"]
2720

2821
# Custom Runtime Configuration - LiveKit Agent Worker
2922
[cerebrium.runtime.custom]

cerebrium/main.py

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,9 @@
3030
# Set cache directory for models
3131
os.environ["HF_HOME"] = "/cortex/.cache/"
3232

33-
def setup_ollama():
34-
"""Start Ollama service (binary installed during build)."""
35-
try:
36-
logger.info("Starting Ollama service...")
37-
38-
# Start Ollama service in background (binary pre-installed)
39-
ollama_process = subprocess.Popen(
40-
["ollama", "serve"],
41-
stdout=subprocess.PIPE,
42-
stderr=subprocess.PIPE,
43-
text=True
44-
)
45-
46-
logger.info("✓ Ollama service started")
47-
return ollama_process
48-
49-
except Exception as e:
50-
logger.error(f"Failed to start Ollama: {e}")
51-
return None
52-
53-
def wait_for_ollama():
54-
"""Wait for Ollama to be ready."""
55-
max_attempts = 30
33+
def check_ollama_ready():
34+
"""Check if Ollama is ready (should be started by Docker container)."""
35+
max_attempts = 10
5636
for attempt in range(max_attempts):
5737
try:
5838
result = subprocess.run(
@@ -70,7 +50,7 @@ def wait_for_ollama():
7050
logger.info(f"Waiting for Ollama... ({attempt + 1}/{max_attempts})")
7151
time.sleep(2)
7252

73-
logger.error("✗ Ollama failed to start within timeout")
53+
logger.error("✗ Ollama not available")
7454
return False
7555

7656
class GmailAssistant(Agent):
@@ -205,18 +185,16 @@ async def log_usage():
205185
logger.info("✓ Build phase complete")
206186

207187
else:
208-
# Start Ollama service first
188+
# Check if Ollama is ready (should be started by Docker container)
209189
logger.info("🚀 Starting Gmail Voice Assistant Agent...")
210-
logger.info("Setting up Ollama...")
211-
212-
ollama_process = setup_ollama()
190+
logger.info("Checking Ollama availability...")
213191

214-
if ollama_process and wait_for_ollama():
215-
logger.info("✓ Ollama is ready, starting LiveKit agent worker")
192+
if check_ollama_ready():
193+
logger.info("✓ Ollama is available, starting LiveKit agent worker")
216194
else:
217-
logger.error("❌ Ollama setup failed - agent cannot function without LLM")
218-
logger.error("Please check Ollama installation and model availability")
219-
# Don't start agent if Ollama failed - it will definitely fail
195+
logger.error("❌ Ollama not available - agent cannot function without LLM")
196+
logger.error("Make sure Ollama is running in the Docker container")
197+
# Don't start agent if Ollama not available - it will definitely fail
220198
sys.exit(1)
221199

222200
# Start the LiveKit agent worker
@@ -229,10 +207,6 @@ async def log_usage():
229207
))
230208
except KeyboardInterrupt:
231209
logger.info("Shutting down...")
232-
if ollama_process:
233-
ollama_process.terminate()
234210
except Exception as e:
235211
logger.error(f"Agent failed: {e}")
236-
if ollama_process:
237-
ollama_process.terminate()
238212
raise

0 commit comments

Comments
 (0)