Skip to content

Commit daf09fa

Browse files
committed
Add ollama-init service to auto-pull models on startup
1 parent 5367a57 commit daf09fa

2 files changed

Lines changed: 82 additions & 21 deletions

File tree

docker-compose.homelab.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
# =============================================================================
2-
# Docker Compose - Homelab Override (OPTIONAL)
2+
# Docker Compose - Homelab Override
33
# =============================================================================
44
# This file is for homelab/CI-CD deployments using GHCR images
55
# Regular users should use docker-compose.yml directly
66
# =============================================================================
77
# Use with: docker compose -f docker-compose.yml -f docker-compose.homelab.yml up -d
88
# =============================================================================
99

10-
version: '3.8'
11-
1210
services:
1311
# ===========================================================================
1412
# Backend Service - Use GHCR Image
1513
# ===========================================================================
1614
backend:
17-
# Remove build section and use GHCR image instead
15+
# Override build with GHCR image
1816
image: ghcr.io/medevs/portfolio-backend:latest
19-
# Keep all other settings from base compose
20-
pull_policy: always # Always pull latest image
17+
build: !reset null
18+
pull_policy: always
2119

2220
# ===========================================================================
2321
# Frontend Service - Use GHCR Image
@@ -30,10 +28,17 @@ services:
3028
# ===========================================================================
3129
frontend:
3230
image: ghcr.io/medevs/portfolio-frontend:latest
31+
build: !reset null
3332
ports:
3433
- "3100:3000"
3534
environment:
3635
# Only non-NEXT_PUBLIC_* runtime vars work here
3736
- NEXT_PUBLIC_ADMIN_API_KEY=${ADMIN_API_KEY}
3837
pull_policy: always
3938

39+
# ===========================================================================
40+
# Ollama Init - Inherits from base compose
41+
# ===========================================================================
42+
# The ollama-init service from docker-compose.yml handles model pulling.
43+
# No override needed - it will pull models defined in environment vars.
44+
# ===========================================================================

docker-compose.yml

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,35 @@ services:
2121
- APP_NAME=${APP_NAME:-AI Portfolio Backend}
2222
- APP_VERSION=${APP_VERSION:-1.0.0}
2323
- DEBUG=${DEBUG:-false}
24-
24+
2525
# Server Settings
2626
- HOST=0.0.0.0
2727
- PORT=8000
28-
28+
2929
# CORS Settings (comma-separated)
3030
- CORS_ORIGINS=${CORS_ORIGINS:-http://localhost:3000,http://localhost:3001}
31-
31+
3232
# Ollama LLM Settings
3333
- OLLAMA_BASE_URL=${OLLAMA_BASE_URL:-http://ollama:11434}
3434
- OLLAMA_MODEL=${OLLAMA_MODEL:-llama3.2:3b}
35-
35+
3636
# ChromaDB Settings
3737
- CHROMA_PERSIST_DIR=/app/data/chroma_db
3838
- CHROMA_COLLECTION_NAME=${CHROMA_COLLECTION_NAME:-portfolio_docs}
39-
39+
4040
# Document Processing
4141
- UPLOAD_DIR=/app/data/documents
4242
- MAX_FILE_SIZE_MB=${MAX_FILE_SIZE_MB:-10}
4343
- ALLOWED_EXTENSIONS=${ALLOWED_EXTENSIONS:-.pdf,.md,.txt,.docx}
44-
44+
4545
# RAG Settings
4646
- CHUNK_SIZE=${CHUNK_SIZE:-500}
4747
- CHUNK_OVERLAP=${CHUNK_OVERLAP:-50}
4848
- TOP_K_RESULTS=${TOP_K_RESULTS:-3}
49-
50-
# Embedding Model
51-
- EMBEDDING_MODEL=${EMBEDDING_MODEL:-all-MiniLM-L6-v2}
52-
49+
50+
# Embedding Model (Ollama embedding model)
51+
- EMBEDDING_MODEL=${EMBEDDING_MODEL:-nomic-embed-text}
52+
5353
# Security (REQUIRED - set in .env file)
5454
- ADMIN_API_KEY=${ADMIN_API_KEY}
5555
volumes:
@@ -62,13 +62,14 @@ services:
6262
networks:
6363
- portfolio-network
6464
depends_on:
65-
- ollama
65+
ollama-init:
66+
condition: service_completed_successfully
6667
healthcheck:
6768
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
6869
interval: 30s
6970
timeout: 10s
7071
retries: 3
71-
start_period: 40s
72+
start_period: 10s
7273

7374
# ===========================================================================
7475
# Frontend Service (Next.js)
@@ -113,11 +114,11 @@ services:
113114
networks:
114115
- portfolio-network
115116
healthcheck:
116-
test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
117+
test: ["CMD-SHELL", "ollama list || exit 1"]
117118
interval: 30s
118119
timeout: 10s
119120
retries: 3
120-
start_period: 60s
121+
start_period: 30s
121122
# Optional: Set resource limits
122123
deploy:
123124
resources:
@@ -126,6 +127,62 @@ services:
126127
reservations:
127128
memory: 4G
128129

130+
# ===========================================================================
131+
# Ollama Model Initialization (Pulls required models)
132+
# ===========================================================================
133+
# This init container ensures all required Ollama models are pulled before
134+
# the backend starts. It runs once and exits successfully.
135+
# ===========================================================================
136+
ollama-init:
137+
image: curlimages/curl:latest
138+
container_name: portfolio-ollama-init
139+
depends_on:
140+
ollama:
141+
condition: service_healthy
142+
networks:
143+
- portfolio-network
144+
environment:
145+
- OLLAMA_HOST=ollama:11434
146+
- LLM_MODEL=${OLLAMA_MODEL:-llama3.2:3b}
147+
- EMBEDDING_MODEL=${EMBEDDING_MODEL:-nomic-embed-text}
148+
entrypoint: ["/bin/sh", "-c"]
149+
command:
150+
- |
151+
echo "=== Ollama Model Initialization ==="
152+
echo "Waiting for Ollama to be ready..."
153+
154+
# Wait for Ollama API
155+
until curl -sf http://$$OLLAMA_HOST/api/tags > /dev/null 2>&1; do
156+
echo "Waiting for Ollama..."
157+
sleep 2
158+
done
159+
echo "Ollama is ready!"
160+
161+
# Function to pull model if not exists
162+
pull_model() {
163+
MODEL=$$1
164+
echo "Checking model: $$MODEL"
165+
166+
# Check if model exists
167+
if curl -sf http://$$OLLAMA_HOST/api/tags | grep -q "\"name\":\"$$MODEL\""; then
168+
echo "Model $$MODEL already exists, skipping pull"
169+
else
170+
echo "Pulling model: $$MODEL (this may take a while)..."
171+
curl -X POST http://$$OLLAMA_HOST/api/pull -d "{\"name\": \"$$MODEL\"}" --no-buffer
172+
echo ""
173+
echo "Model $$MODEL pulled successfully!"
174+
fi
175+
}
176+
177+
# Pull LLM model
178+
pull_model "$$LLM_MODEL"
179+
180+
# Pull embedding model
181+
pull_model "$$EMBEDDING_MODEL"
182+
183+
echo "=== All models ready! ==="
184+
restart: "no"
185+
129186
# =============================================================================
130187
# Volumes (Data Persistence)
131188
# =============================================================================
@@ -145,4 +202,3 @@ volumes:
145202
networks:
146203
portfolio-network:
147204
driver: bridge
148-

0 commit comments

Comments
 (0)