Skip to content

Commit 713c829

Browse files
committed
chore(docker): docker ignore and img with model embedded
1 parent b8cd413 commit 713c829

File tree

7 files changed

+158
-77
lines changed

7 files changed

+158
-77
lines changed

.dockerignore

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Python cache files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
.pytest_cache/
8+
.coverage
9+
htmlcov/
10+
.tox/
11+
.nox/
12+
.hypothesis/
13+
14+
# Virtual environments
15+
env/
16+
venv/
17+
ENV/
18+
.env
19+
.venv
20+
env.bak/
21+
venv.bak/
22+
.python-version
23+
24+
# Distribution / packaging
25+
build/
26+
develop-eggs/
27+
dist/
28+
downloads/
29+
eggs/
30+
.eggs/
31+
lib/
32+
lib64/
33+
parts/
34+
sdist/
35+
var/
36+
wheels/
37+
*.egg-info/
38+
.installed.cfg
39+
*.egg
40+
41+
# IDE files
42+
.idea/
43+
.vscode/
44+
*.swp
45+
*.swo
46+
.DS_Store
47+
48+
# Jupyter Notebook
49+
.ipynb_checkpoints
50+
51+
# Local development files
52+
.env.local
53+
.env.development.local
54+
.env.test.local
55+
.env.production.local
56+
57+
# Log files
58+
*.log
59+
logs/
60+
61+
# Git
62+
*.git
63+
*.gitignore
64+
*.gitmodules
65+
*.gitconfig
66+
*.gitignore
67+
*.gitmodules
68+
*.gitconfig
69+
70+
# Github
71+
.github/
72+
73+
# Kubernetes
74+
.kubernetes/
75+
76+
# Tests
77+
tests/

.github/workflows/publish-image-with-model.yml

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -66,65 +66,9 @@ jobs:
6666
- name: Install dependencies
6767
run: poetry install
6868

69-
- name: Download model
69+
- name: Download and build
7070
run: |
71-
if [ "${{ github.event.inputs.model_size }}" == "small" ]; then
72-
make download-model-small
73-
elif [ "${{ github.event.inputs.model_size }}" == "medium" ]; then
74-
make download-model-medium
75-
elif [ "${{ github.event.inputs.model_size }}" == "large" ]; then
76-
make download-model-large
77-
fi
78-
79-
- name: Create Dockerfile with embedded model
80-
run: |
81-
cat > Dockerfile.with-model << EOF
82-
# Build stage
83-
FROM python:3.10-slim AS builder
84-
85-
WORKDIR /app
86-
87-
# Install a specific version of Poetry that supports --no-dev
88-
RUN pip install poetry==2.1.1
89-
90-
# Copy project files
91-
COPY pyproject.toml poetry.lock* README.md ./
92-
COPY babeltron ./babeltron
93-
94-
# Configure poetry to not use a virtual environment
95-
RUN poetry config virtualenvs.create false \\
96-
&& poetry install --without dev --no-interaction --no-ansi
97-
98-
# Final stage
99-
FROM python:3.10-slim
100-
101-
WORKDIR /app
102-
103-
# Copy Python dependencies from builder stage
104-
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
105-
COPY --from=builder /usr/local/bin /usr/local/bin
106-
107-
# Copy application code
108-
COPY --from=builder /app/babeltron ./babeltron
109-
110-
# Copy the downloaded model
111-
COPY models /models
112-
113-
# Copy and set permissions on the entrypoint script BEFORE changing user
114-
COPY docker-entrypoint.sh /app/docker-entrypoint.sh
115-
RUN chmod +x /app/docker-entrypoint.sh
116-
117-
ENV PYTHONPATH=/app
118-
ENV MODEL_PATH=/models
119-
120-
# Create a non-root user and switch to it
121-
RUN useradd -m appuser
122-
USER appuser
123-
124-
EXPOSE 8000
125-
126-
ENTRYPOINT ["/app/docker-entrypoint.sh"]
127-
EOF
71+
make docker-build-with-model MODEL_SIZE="${{ github.event.inputs.model_size }}"
12872
12973
- name: Extract metadata for Docker
13074
id: meta

Dockerfile.with-model

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
FROM python:3.10-slim AS builder
2+
3+
WORKDIR /app
4+
5+
# Install a specific version of Poetry that supports --no-dev
6+
RUN pip install poetry==2.1.1
7+
8+
# Copy project files
9+
COPY pyproject.toml poetry.lock* README.md ./
10+
COPY babeltron ./babeltron
11+
12+
# Configure poetry to not use a virtual environment
13+
RUN poetry config virtualenvs.create false \
14+
&& poetry install --without dev --no-interaction --no-ansi
15+
16+
# Final stage
17+
FROM python:3.10-slim
18+
19+
WORKDIR /app
20+
21+
# Copy Python dependencies from builder stage
22+
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
23+
COPY --from=builder /usr/local/bin /usr/local/bin
24+
25+
# Copy application code
26+
COPY --from=builder /app/babeltron ./babeltron
27+
28+
# Copy the downloaded model
29+
COPY models /models
30+
31+
# Copy and set permissions on the entrypoint script BEFORE changing user
32+
COPY docker-entrypoint.sh /app/docker-entrypoint.sh
33+
RUN chmod +x /app/docker-entrypoint.sh
34+
35+
ENV PYTHONPATH=/app
36+
ENV MODEL_PATH=/models
37+
38+
# Create a non-root user and switch to it
39+
RUN useradd -m appuser
40+
USER appuser
41+
42+
EXPOSE 8000
43+
44+
ENTRYPOINT ["/app/docker-entrypoint.sh"]

Makefile

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
.PHONY: check-poetry install test lint format help system-deps coverage coverage-html download-model download-model-small download-model-medium download-model-large serve serve-prod docker-build docker-run docker-compose-up docker-compose-down pre-commit-install pre-commit-run
1+
.PHONY: check-poetry install test lint format help system-deps coverage coverage-html download-model download-model-small download-model-medium download-model-large serve serve-prod docker-build docker-run docker-compose-up docker-compose-down pre-commit-install pre-commit-run docker-build-with-model
22

33
# Define model path variable with default value, can be overridden by environment
44
MODEL_PATH ?= ./models
5+
MODEL_SIZE ?= small
56

67
# Extract target descriptions from comments
78
help: ## Show this help message
@@ -94,6 +95,25 @@ docker-build: ## Build Docker image
9495
@echo "Building Docker image..."
9596
@docker build -t babeltron:latest .
9697

98+
docker-build-with-model: ## Build Docker image with embedded model
99+
@echo "Building Docker image with embedded $(MODEL_SIZE) model..."
100+
@if [ ! -d "$(MODEL_PATH)" ] || [ -z "$(shell ls -A $(MODEL_PATH) 2>/dev/null)" ]; then \
101+
echo "No model files found in $(MODEL_PATH) directory. Downloading..."; \
102+
if [ "$(MODEL_SIZE)" = "small" ]; then \
103+
$(MAKE) download-model-small; \
104+
elif [ "$(MODEL_SIZE)" = "medium" ]; then \
105+
$(MAKE) download-model-medium; \
106+
elif [ "$(MODEL_SIZE)" = "large" ]; then \
107+
$(MAKE) download-model-large; \
108+
else \
109+
echo "Invalid model size: $(MODEL_SIZE). Using small model."; \
110+
$(MAKE) download-model-small; \
111+
fi; \
112+
fi
113+
@echo "Building Docker image..."
114+
@docker build -t babeltron:$(MODEL_SIZE) -f Dockerfile.with-model .
115+
@echo "Docker image with $(MODEL_SIZE) model built successfully as babeltron:$(MODEL_SIZE)"
116+
97117
docker-run: ## Run Docker container with model volume mount
98118
@echo "Checking for model files..."
99119
@if [ ! -d "$(MODEL_PATH)" ] || [ -z "$(shell ls -A $(MODEL_PATH) 2>/dev/null)" ]; then \

docker-entrypoint.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ else
1313
WORKER_COUNT_INT=1
1414
fi
1515

16+
export UVICORN_ACCESS_LOG=0
1617
# If WORKER_COUNT is greater than 1, use Gunicorn
1718
if [ "$WORKER_COUNT_INT" -gt 1 ]; then
1819
echo "Starting with Gunicorn using $WORKER_COUNT_INT workers"
19-
# Set environment variable to disable Uvicorn access logs inside Gunicorn workers
20-
export UVICORN_ACCESS_LOG=0
2120
exec gunicorn babeltron.app.main:app \
2221
--workers $WORKER_COUNT_INT \
2322
--worker-class uvicorn.workers.UvicornWorker \
@@ -26,5 +25,5 @@ if [ "$WORKER_COUNT_INT" -gt 1 ]; then
2625
--error-logfile -
2726
else
2827
echo "Starting with Uvicorn (single worker)"
29-
exec UVICORN_ACCESS_LOG=0 uvicorn babeltron.app.main:app --host 0.0.0.0 --port 8000
28+
exec uvicorn babeltron.app.main:app --host 0.0.0.0 --port 8000
3029
fi

recovered_download_models.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

scripts/test-model-container.sh

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ done
2525

2626
echo "Testing Babeltron container with image tag: $IMAGE_TAG"
2727

28-
# Pull the image
29-
echo "Pulling image ghcr.io/hspedro/babeltron:$IMAGE_TAG..."
30-
docker pull ghcr.io/hspedro/babeltron:$IMAGE_TAG
28+
# Check if image exists locally, pull only if it doesn't
29+
FULL_IMAGE_NAME="ghcr.io/hspedro/babeltron:$IMAGE_TAG"
30+
if docker image inspect "$FULL_IMAGE_NAME" &>/dev/null; then
31+
echo "Image $FULL_IMAGE_NAME found locally, skipping pull"
32+
else
33+
echo "Image $FULL_IMAGE_NAME not found locally, pulling from registry..."
34+
docker pull "$FULL_IMAGE_NAME"
35+
fi
3136

3237
# Run the container
3338
echo "Starting container $CONTAINER_NAME..."
34-
docker run -d --name $CONTAINER_NAME -p 8000:8000 ghcr.io/hspedro/babeltron:$IMAGE_TAG
39+
docker run -d --name $CONTAINER_NAME -p 8000:8000 "$FULL_IMAGE_NAME"
3540

3641
# Wait for the container to be ready
3742
echo "Waiting for the container to be ready..."
3843
for i in {1..30}; do
39-
if curl -s http://localhost:8000/healthz | grep -q "ok"; then
40-
echo "Container is healthy!"
44+
if curl -s http://localhost:8000/readyz | grep -q "ready"; then
45+
echo "Container is ready!"
4146
break
4247
fi
4348

@@ -55,7 +60,7 @@ done
5560

5661
# Test translation
5762
echo "Testing translation..."
58-
RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/translate" \
63+
RESPONSE=$(curl -s -X POST "http://localhost:8000/translate" \
5964
-H "Content-Type: application/json" \
6065
-d '{
6166
"text": "Hello, how are you?",

0 commit comments

Comments
 (0)