Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Fix]: Reduce docker image size by from 3.5 GB to 601 MB (~83.26% reduction) (#812)

Aman Agarwalab45bdc8255f
diff --git a/src/client/.dockerignore b/src/client/.dockerignore
new file mode 100644
index 00000000..841264b7
--- /dev/null
+++ b/src/client/.dockerignore
@@ -0,0 +1,83 @@
+# Dependencies
+node_modules
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+
+# Build outputs
+.next
+out
+
+# Environment files
+.env*.local
+.env
+
+# Development files
+.git
+.gitignore
+README.md
+*.md
+
+# IDE and editor files
+.vscode
+.idea
+*.swp
+*.swo
+
+# OS generated files
+.DS_Store
+.DS_Store?
+._*
+.Spotlight-V100
+.Trashes
+ehthumbs.db
+Thumbs.db
+
+# Test files
+coverage
+__tests__
+*.test.js
+*.test.ts
+*.spec.js
+*.spec.ts
+
+# Logs
+logs
+*.log
+lerna-debug.log*
+
+# Runtime data
+pids
+*.pid
+*.seed
+*.pid.lock
+
+# Coverage directory used by tools like istanbul
+coverage
+*.lcov
+
+# ESLint cache
+.eslintcache
+
+# Optional npm cache directory
+.npm
+
+# Optional REPL history
+.node_repl_history
+
+# Output of 'npm pack'
+*.tgz
+
+# Yarn Integrity file
+.yarn-integrity
+
+# parcel-bundler cache (https://parceljs.org/)
+.cache
+.parcel-cache
+
+# Docker files
+Dockerfile*
+docker-compose*
+
+# Documentation
+docs
\ No newline at end of file
diff --git a/src/client/Dockerfile b/src/client/Dockerfile
index 10c5af06..d882baa9 100644
--- a/src/client/Dockerfile
+++ b/src/client/Dockerfile
@@ -1,9 +1,24 @@
# Use an alpine as a base image
-FROM alpine as builder
+FROM alpine AS builder
# Installing node and npm
RUN apk add --update nodejs npm
+
+# Installing extra packages
+RUN apk add --no-cache python3 py3-pip pipx rust cargo build-base
+
+
+# Create a virtual environment
+RUN python3 -m venv /app/client/venv
+
+# Install litellm inside the virtual environment
+RUN /app/client/venv/bin/pip install --no-cache-dir litellm
+
+RUN find /app/client/venv -name '*.pyc' -delete && \
+ find /app/client/venv -name '__pycache__' -type d -exec rm -r {} +
+
+
# Set the working directory in the container
WORKDIR /app/client
@@ -18,30 +33,33 @@ RUN npm install && npm run build
FROM alpine
# Installing extra packages
-RUN apk add --no-cache nodejs npm bash openssl python3 py3-pip pipx rust cargo build-base
-
-# Create a virtual environment
-RUN python3 -m venv /app/client/venv
+RUN apk add nodejs bash openssl python3
-# Install litellm inside the virtual environment
-RUN /app/client/venv/bin/pip install --no-cache-dir litellm
# Set the working directory in the container
WORKDIR /app/client
# Copy only necessary files from the builder stage
-COPY --from=builder /app/client/.next ./.next
-COPY --from=builder /app/client/package.json ./package.json
-COPY --from=builder /app/client/package-lock.json ./package-lock.json
+
+# Copying python virtual environment
+COPY --from=builder /app/client/venv ./venv
+# Copying the standalone next.js build
+COPY --from=builder /app/client/.next/standalone ./
+# Standalone build doesn't copy public, static, prisma, scripts
COPY --from=builder /app/client/public ./public
+COPY --from=builder /app/client/.next/static ./.next/static
COPY --from=builder /app/client/prisma ./prisma
+COPY --from=builder /app/client/scripts ./scripts
+# Copying prisma client, prisma and prisma cli (.bin) from builder stage
+COPY --from=builder /app/client/node_modules/@prisma ./node_modules/@prisma
+COPY --from=builder /app/client/node_modules/.bin ./node_modules/.bin
+COPY --from=builder /app/client/node_modules/prisma ./node_modules/prisma
# Execute entrypoint script to generate NextAuth.js secret and run commands
-COPY --from=builder /app/client/scripts ./scripts
RUN chmod +x ./scripts/entrypoint.sh
# Install Prisma globally
-RUN npm install -g prisma
+# RUN npm install -g [email protected]
# Expose the port that Next.js will run on
EXPOSE ${DOCKER_PORT:-3000}
diff --git a/src/client/next.config.js b/src/client/next.config.js
index 20b04e5f..de3be89b 100644
--- a/src/client/next.config.js
+++ b/src/client/next.config.js
@@ -1,5 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
+ output: "standalone",
images: {
remotePatterns: [
{
diff --git a/src/client/package-lock.json b/src/client/package-lock.json
index bd6d4460..fc6e519b 100644
--- a/src/client/package-lock.json
+++ b/src/client/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "openlit",
- "version": "1.14.0",
+ "version": "1.14.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "openlit",
- "version": "1.14.0",
+ "version": "1.14.1",
"dependencies": {
"@anthropic-ai/sdk": "^0.21.1",
"@clickhouse/client": "^1.1.0",
diff --git a/src/client/package.json b/src/client/package.json
index 23e54b50..12582d0d 100644
--- a/src/client/package.json
+++ b/src/client/package.json
@@ -1,6 +1,6 @@
{
"name": "openlit",
- "version": "1.14.0",
+ "version": "1.14.1",
"private": true,
"scripts": {
"dev": "next dev",
diff --git a/src/client/scripts/entrypoint.sh b/src/client/scripts/entrypoint.sh
index 718e29bb..8fe35051 100755
--- a/src/client/scripts/entrypoint.sh
+++ b/src/client/scripts/entrypoint.sh
@@ -8,6 +8,7 @@ export NEXTAUTH_SECRET=$(openssl rand -base64 32)
echo "NEXTAUTH_SECRET=$NEXTAUTH_SECRET" >> /etc/environment
echo "NEXTAUTH_URL=http://localhost:${DOCKER_PORT:-3000}" >> /etc/environment
echo "SQLITE_DATABASE_URL=${SQLITE_DATABASE_URL:-file:../data/data.db}" >> /etc/environment
+echo "PATH=./node_modules/.bin:$PATH" >> /etc/environment
# Environment variables for DB config
echo "INIT_DB_USERNAME=${INIT_DB_USERNAME}" >> /etc/environment
@@ -31,4 +32,4 @@ crond &
export PORT=${DOCKER_PORT:-3000}
# Start the Next.js application
-exec node --max_old_space_size=512 $(which npm) start
+exec node --max_old_space_size=512 server.js