The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
[Fix] Litellm non root docker Model Hub Table fix (#16282)
yuneng-jiang5d158775b1f1
diff --git a/docker/Dockerfile.non_root b/docker/Dockerfile.non_rootindex 4178724e6e4..6be115ba71b 100644--- a/docker/Dockerfile.non_root+++ b/docker/Dockerfile.non_root@@ -8,16 +8,36 @@ ARG LITELLM_RUNTIME_IMAGE=cgr.dev/chainguard/python:latest-devFROM $LITELLM_BUILD_IMAGE AS builderWORKDIR /app-# Install build dependencies+# Install build dependencies including Node.js for UI buildUSER root-RUN apk add --no-cache build-base bash \+RUN apk add --no-cache build-base bash nodejs npm \&& pip install --no-cache-dir --upgrade pip build# Copy project filesCOPY . .+# Set LITELLM_NON_ROOT flag for build time+ENV LITELLM_NON_ROOT=true+# Build Admin UI-RUN chmod +x docker/build_admin_ui.sh && ./docker/build_admin_ui.sh+RUN mkdir -p /tmp/litellm_ui && \+ cd ui/litellm-dashboard && \+ if [ -f "../../enterprise/enterprise_ui/enterprise_colors.json" ]; then \+ cp ../../enterprise/enterprise_ui/enterprise_colors.json ./ui_colors.json; \+ fi && \+ npm install && \+ npm run build && \+ cp -r ./out/* /tmp/litellm_ui/ && \+ cd /tmp/litellm_ui && \+ for html_file in *.html; do \+ if [ "$html_file" != "index.html" ] && [ -f "$html_file" ]; then \+ folder_name="${html_file%.html}" && \+ mkdir -p "$folder_name" && \+ mv "$html_file" "$folder_name/index.html"; \+ fi; \+ done && \+ cd /app/ui/litellm-dashboard && \+ rm -rf ./out# Build package and wheel dependenciesRUN rm -rf dist/* && python -m build && \@@ -42,6 +62,7 @@ COPY --from=builder /app/docker/supervisord.conf /etc/supervisord.confCOPY --from=builder /app/schema.prisma /app/schema.prismaCOPY --from=builder /app/dist/*.whl .COPY --from=builder /wheels/ /wheels/+COPY --from=builder /tmp/litellm_ui /tmp/litellm_ui# Install package from wheel and dependenciesRUN pip install *.whl /wheels/* --no-index --find-links=/wheels/ \@@ -56,7 +77,6 @@ RUN pip uninstall jwt -y && \pip uninstall PyJWT -y && \pip install PyJWT==2.9.0 --no-cache-dir-# --- Prisma Handling for Non-Root User ---# Set Prisma cache directoriesENV PRISMA_BINARY_CACHE_DIR=/nonexistentENV NPM_CONFIG_CACHE=/.npm@@ -68,25 +88,20 @@ RUN pip install --no-cache-dir prisma && \# Create directories and set permissions for non-root userRUN mkdir -p /nonexistent /.npm && \- chown -R nobody:nogroup /app && \- chown -R nobody:nogroup /nonexistent /.npm && \+ chown -R nobody:nogroup /app /tmp/litellm_ui /nonexistent /.npm && \PRISMA_PATH=$(python -c "import os, prisma; print(os.path.dirname(prisma.__file__))") && \chown -R nobody:nogroup $PRISMA_PATH && \LITELLM_PKG_MIGRATIONS_PATH="$(python -c 'import os, litellm_proxy_extras; print(os.path.dirname(litellm_proxy_extras.__file__))' 2>/dev/null || echo '')/migrations" && \[ -n "$LITELLM_PKG_MIGRATIONS_PATH" ] && chown -R nobody:nogroup $LITELLM_PKG_MIGRATIONS_PATH-# --- OpenShift Compatibility: Apply Red Hat recommended pattern ----# Get paths for directories that need write access at runtime+# OpenShift compatibilityRUN PRISMA_PATH=$(python -c "import os, prisma; print(os.path.dirname(prisma.__file__))") && \LITELLM_PROXY_EXTRAS_PATH=$(python -c "import os, litellm_proxy_extras; print(os.path.dirname(litellm_proxy_extras.__file__))" 2>/dev/null || echo "") && \- # Set group ownership to 0 (root group) for OpenShift compatibility && \- chgrp -R 0 $PRISMA_PATH && \+ chgrp -R 0 $PRISMA_PATH /tmp/litellm_ui && \[ -n "$LITELLM_PROXY_EXTRAS_PATH" ] && chgrp -R 0 $LITELLM_PROXY_EXTRAS_PATH || true && \- # Mirror owner permissions to group (g=u) as recommended by Red Hat && \- chmod -R g=u $PRISMA_PATH && \+ chmod -R g=u $PRISMA_PATH /tmp/litellm_ui && \[ -n "$LITELLM_PROXY_EXTRAS_PATH" ] && chmod -R g=u $LITELLM_PROXY_EXTRAS_PATH || true && \- # Ensure directories are writable by group && \- chmod -R g+w $PRISMA_PATH && \+ chmod -R g+w $PRISMA_PATH /tmp/litellm_ui && \[ -n "$LITELLM_PROXY_EXTRAS_PATH" ] && chmod -R g+w $LITELLM_PROXY_EXTRAS_PATH || true# Switch to non-root user@@ -94,14 +109,14 @@ USER nobody# Set HOME for prisma generate to have a writable directoryENV HOME=/app++# Set LITELLM_NON_ROOT flag for runtime+ENV LITELLM_NON_ROOT=true+RUN prisma generate-# --- End of Prisma Handling ---EXPOSE 4000/tcp-# Set entrypoint and commandENTRYPOINT ["/app/docker/prod_entrypoint.sh"]-# Append "--detailed_debug" to the end of CMD to view detailed debug logs-# CMD ["--port", "4000", "--detailed_debug"]-CMD ["--port", "4000"]+CMD ["--port", "4000"]\ No newline at end of filediff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex f62fec176b4..bed3d218df4 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -903,6 +903,20 @@ try:ui_path = os.path.join(current_dir, "_experimental", "out")litellm_asset_prefix = "/litellm-asset-prefix"+ # For non-root Docker, use the pre-built UI from /tmp/litellm_ui+ # Support both "true" and "True" for case-insensitive comparison+ if os.getenv("LITELLM_NON_ROOT", "").lower() == "true":+ non_root_ui_path = "/tmp/litellm_ui"++ # Check if the UI was built and exists at the expected location+ if os.path.exists(non_root_ui_path) and os.listdir(non_root_ui_path):+ verbose_proxy_logger.info(f"Using pre-built UI for non-root Docker: {non_root_ui_path}")+ verbose_proxy_logger.info(f"UI files found: {len(os.listdir(non_root_ui_path))} items")+ ui_path = non_root_ui_path+ else:+ verbose_proxy_logger.error(f"UI not found at {non_root_ui_path}. UI will not be available.")+ verbose_proxy_logger.error(f"Path exists: {os.path.exists(non_root_ui_path)}, Has content: {os.path.exists(non_root_ui_path) and bool(os.listdir(non_root_ui_path))}")+# Only modify files if a custom server root path is setif server_root_path and server_root_path != "/":# Iterate through files in the UI directory@@ -962,17 +976,22 @@ try:app.mount("/ui", StaticFiles(directory=ui_path, html=True), name="ui")# Handle HTML file restructuring- for filename in os.listdir(ui_path):- if filename.endswith(".html") and filename != "index.html":- # Create a folder with the same name as the HTML file- folder_name = os.path.splitext(filename)[0]- folder_path = os.path.join(ui_path, folder_name)- os.makedirs(folder_path, exist_ok=True)-- # Move the HTML file into the folder and rename it to 'index.html'- src = os.path.join(ui_path, filename)- dst = os.path.join(folder_path, "index.html")- os.rename(src, dst)+ # Skip this for non-root Docker since it's done at build time+ # Support both "true" and "True" for case-insensitive comparison+ if os.getenv("LITELLM_NON_ROOT", "").lower() != "true":+ for filename in os.listdir(ui_path):+ if filename.endswith(".html") and filename != "index.html":+ # Create a folder with the same name as the HTML file+ folder_name = os.path.splitext(filename)[0]+ folder_path = os.path.join(ui_path, folder_name)+ os.makedirs(folder_path, exist_ok=True)++ # Move the HTML file into the folder and rename it to 'index.html'+ src = os.path.join(ui_path, filename)+ dst = os.path.join(folder_path, "index.html")+ os.rename(src, dst)+ else:+ verbose_proxy_logger.info("Skipping runtime HTML restructuring for non-root Docker (already done at build time)")except Exception:passdiff --git a/tests/proxy_unit_tests/test_proxy_server.py b/tests/proxy_unit_tests/test_proxy_server.pyindex 1f634d7b9a5..1f4bf806c16 100644--- a/tests/proxy_unit_tests/test_proxy_server.py+++ b/tests/proxy_unit_tests/test_proxy_server.py@@ -2299,3 +2299,75 @@ def test_get_timeout_from_request():}timeout = LiteLLMProxyRequestSetup._get_timeout_from_request(headers)assert timeout == 90.5+++ "ui_exists, ui_has_content",+ [+ (True, True), # UI path exists and has content+ (True, False), # UI path exists but is empty+ (False, False), # UI path doesn't exist+ ],+)+def test_non_root_ui_path_logic(monkeypatch, tmp_path, ui_exists, ui_has_content):+ """+ Test the non-root Docker UI path detection logic.++ Tests that when LITELLM_NON_ROOT is set to "true":+ - If UI path exists and has content, it should be used+ - If UI path doesn't exist or is empty, proper error logging occurs+ """+ import tempfile+ import shutil+ from unittest.mock import MagicMock++ # Create a temporary directory to act as /tmp/litellm_ui+ test_ui_path = tmp_path / "litellm_ui"++ if ui_exists:+ test_ui_path.mkdir(parents=True, exist_ok=True)+ if ui_has_content:+ # Create some dummy files to simulate built UI+ (test_ui_path / "index.html").write_text("<html></html>")+ (test_ui_path / "app.js").write_text("console.log('test');")++ # Mock the environment variable and os.path operations+ monkeypatch.setenv("LITELLM_NON_ROOT", "true")++ # Create a mock logger to capture log messages+ mock_logger = MagicMock()++ # We need to reimport or reload the relevant code section+ # Since this is module-level code, we'll test the logic directly+ ui_path = None+ non_root_ui_path = str(test_ui_path)++ # Simulate the logic from proxy_server.py lines 909-920+ if os.getenv("LITELLM_NON_ROOT", "").lower() == "true":+ if os.path.exists(non_root_ui_path) and os.listdir(non_root_ui_path):+ mock_logger.info(f"Using pre-built UI for non-root Docker: {non_root_ui_path}")+ mock_logger.info(f"UI files found: {len(os.listdir(non_root_ui_path))} items")+ ui_path = non_root_ui_path+ else:+ mock_logger.error(f"UI not found at {non_root_ui_path}. UI will not be available.")+ mock_logger.error(f"Path exists: {os.path.exists(non_root_ui_path)}, Has content: {os.path.exists(non_root_ui_path) and bool(os.listdir(non_root_ui_path))}")++ # Verify behavior based on test parameters+ if ui_exists and ui_has_content:+ # UI should be found and used+ assert ui_path == non_root_ui_path+ assert mock_logger.info.call_count == 2+ mock_logger.info.assert_any_call(f"Using pre-built UI for non-root Docker: {non_root_ui_path}")+ # Verify the second info call mentions the number of items+ info_calls = [call[0][0] for call in mock_logger.info.call_args_list]+ assert any("UI files found:" in call and "items" in call for call in info_calls)+ assert mock_logger.error.call_count == 0+ else:+ # UI should not be found, error should be logged+ assert ui_path is None+ assert mock_logger.error.call_count == 2+ mock_logger.error.assert_any_call(f"UI not found at {non_root_ui_path}. UI will not be available.")+ # Verify the second error call has path existence info+ error_calls = [call[0][0] for call in mock_logger.error.call_args_list]+ assert any("Path exists:" in call for call in error_calls)+ assert mock_logger.info.call_count == 0