Skip to content

Commit a7245db

Browse files
build(Dockerfile): fixes the build time setup
1 parent 0f7c373 commit a7245db

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ [email protected]
3131
.gitignore
3232
proxy_server_config_2.yaml
3333
litellm/proxy/secret_managers/credentials.json
34+
hosted_config.yaml

Dockerfile

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
1+
# Base image for building
2+
ARG LITELLM_BUILD_IMAGE=python:3.9
13

24
# Runtime image
35
ARG LITELLM_RUNTIME_IMAGE=python:3.9-slim
46
# Builder stage
57
FROM $LITELLM_BUILD_IMAGE as builder
68

7-
@@ -35,8 +34,12 @@ RUN pip wheel --no-cache-dir --wheel-dir=/wheels/ -r requirements.txt
9+
# Set the working directory to /app
10+
WORKDIR /app
11+
12+
# Install build dependencies
13+
RUN apt-get clean && apt-get update && \
14+
apt-get install -y gcc python3-dev && \
15+
rm -rf /var/lib/apt/lists/*
16+
17+
RUN pip install --upgrade pip && \
18+
pip install build
19+
20+
# Copy the current directory contents into the container at /app
21+
COPY . .
22+
23+
# Build the package
24+
RUN rm -rf dist/* && python -m build
25+
26+
# There should be only one wheel file now, assume the build only creates one
27+
RUN ls -1 dist/*.whl | head -1
28+
29+
# Install the package
30+
RUN pip install dist/*.whl
31+
32+
# install dependencies as wheels
33+
RUN pip wheel --no-cache-dir --wheel-dir=/wheels/ -r requirements.txt
834

935
# Runtime stage
1036
FROM $LITELLM_RUNTIME_IMAGE as runtime
@@ -17,7 +43,8 @@ RUN ls -la /app
1743

1844
# Copy the built wheel from the builder stage to the runtime stage; assumes only one wheel file is present
1945
COPY --from=builder /app/dist/*.whl .
20-
@@ -45,9 +48,17 @@ COPY --from=builder /wheels/ /wheels/
46+
COPY --from=builder /wheels/ /wheels/
47+
2148
# Install the built wheel using pip; again using a wildcard if it's the only file
2249
RUN pip install *.whl /wheels/* --no-index --find-links=/wheels/ && rm -f *.whl && rm -rf /wheels
2350

litellm/proxy/proxy_server.py

+5
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,11 @@ async def startup_event():
11031103
# check if master key set in environment - load from there
11041104
master_key = litellm.get_secret("LITELLM_MASTER_KEY", None)
11051105

1106+
### CONNECT TO DB ###
1107+
# check if DATABASE_URL in environment - load from there
1108+
if os.getenv("DATABASE_URL", None) is not None and prisma_client is None:
1109+
prisma_setup(database_url=os.getenv("DATABASE_URL"))
1110+
11061111
### LOAD CONFIG ###
11071112
worker_config = litellm.get_secret("WORKER_CONFIG")
11081113
print_verbose(f"worker_config: {worker_config}")

litellm/proxy/utils.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,25 @@ def __init__(self, database_url: str, proxy_logging_obj: ProxyLogging):
253253
print_verbose(
254254
"LiteLLM: DATABASE_URL Set in config, trying to 'pip install prisma'"
255255
)
256-
## init logging object
256+
## init logging object
257257
self.proxy_logging_obj = proxy_logging_obj
258-
os.environ["DATABASE_URL"] = database_url
259-
# Save the current working directory
260-
original_dir = os.getcwd()
261-
# set the working directory to where this script is
262-
abspath = os.path.abspath(__file__)
263-
dname = os.path.dirname(abspath)
264-
os.chdir(dname)
265258

266-
try:
267-
subprocess.run(["prisma", "generate"])
268-
subprocess.run(
269-
["prisma", "db", "push", "--accept-data-loss"]
270-
) # this looks like a weird edge case when prisma just wont start on render. we need to have the --accept-data-loss
271-
finally:
272-
os.chdir(original_dir)
259+
if os.getenv("DATABASE_URL", None) is None: # setup hasn't taken place
260+
os.environ["DATABASE_URL"] = database_url
261+
# Save the current working directory
262+
original_dir = os.getcwd()
263+
# set the working directory to where this script is
264+
abspath = os.path.abspath(__file__)
265+
dname = os.path.dirname(abspath)
266+
os.chdir(dname)
267+
268+
try:
269+
subprocess.run(["prisma", "generate"])
270+
subprocess.run(
271+
["prisma", "db", "push", "--accept-data-loss"]
272+
) # this looks like a weird edge case when prisma just wont start on render. we need to have the --accept-data-loss
273+
finally:
274+
os.chdir(original_dir)
273275
# Now you can import the Prisma Client
274276
from prisma import Prisma # type: ignore
275277

0 commit comments

Comments
 (0)