Skip to content

Commit f5f85cb

Browse files
committed
feat(deploy): Optimize docker build time
1 parent eabed02 commit f5f85cb

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

ai_service/Dockerfile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
FROM python:3.11-slim
1+
# Thay vì FROM python:3.11-slim, ta dùng image base đã build sẵn
2+
FROM asia-southeast1-docker.pkg.dev/lucky-union-472503-c7/cloud-run-source-deploy/ai-base:v1
3+
24
WORKDIR /srv/app
5+
6+
# --- TỐI ƯU CHO CÁC THƯ VIỆN PHÁT SINH ---
7+
# Copy requirements.txt lại một lần nữa
38
COPY requirements.txt ./
4-
RUN pip install --upgrade pip
9+
10+
# Chạy pip install lại.
11+
# Mẹo: Vì torch đã có trong base image, pip sẽ lướt qua cực nhanh.
12+
# Dòng này chỉ có tác dụng nếu sau này bạn thêm thư viện mới (nhẹ) vào file requirements.
513
RUN pip install --no-cache-dir -r requirements.txt
14+
615
COPY . .
16+
17+
# --- KHỞI CHẠY ---
718
ENV PYTHONUNBUFFERED=1
8-
EXPOSE 8000
19+
920
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

ai_service/Dockerfile.base

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Dockerfile.base
2+
FROM python:3.11-slim
3+
4+
WORKDIR /srv/app
5+
6+
# Copy file danh sách thư viện vào
7+
COPY requirements.txt ./
8+
9+
# Cài đặt các thư viện nặng một lần duy nhất ở đây
10+
# Thêm --no-cache-dir để giảm dung lượng image: không lưu cache các thư viện đã cài đặt
11+
RUN pip install --upgrade pip && \
12+
pip install --no-cache-dir -r requirements.txt

ai_service/app/services/streaming_service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ async def generate_test_streaming_response(question: str) -> AsyncGenerator[str,
4343
"\nChúng tôi cam kết cung cấp thông tin chính xác, đáng tin cậy và phù hợp với quy định pháp luật hiện hành."
4444
]
4545

46+
# Sẽ chạy như này để tạo từng chunk về cho client
47+
# for chunk in client.models.generate_content_stream(
48+
# model=MODEL_NAME,
49+
# contents=req.message,
50+
# config=types.GenerateContentConfig(
51+
# system_instruction=build_system_instruction(req.name),
52+
# temperature=0.7
53+
# ),
54+
# ):
55+
# if getattr(chunk, "text", None):
56+
# print(f"Streaming chunk: {chunk.text}")
57+
# yield chunk.text
58+
4659
for fake_content in fake_chunks:
4760
yield create_content_chunk(fake_content)
4861
await asyncio.sleep(0.5) # Streaming delay

ai_service/build-base.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Chạy bằng cách sử dụng: gcloud builds submit --config build-base.yaml
2+
3+
steps:
4+
- name: 'gcr.io/cloud-builders/docker'
5+
args: [
6+
'build',
7+
'-t', 'asia-southeast1-docker.pkg.dev/lucky-union-472503-c7/cloud-run-source-deploy/ai-base:v1',
8+
'-f', 'Dockerfile.base',
9+
'.'
10+
]
11+
images:
12+
- 'asia-southeast1-docker.pkg.dev/lucky-union-472503-c7/cloud-run-source-deploy/ai-base:v1'

0 commit comments

Comments
 (0)