refactor(docker): switch to Node.js base image and optimize Bun installation
- Replace Bun base image with Node.js slim image - Install Bun globally using npm in both builder and runner stages - Simplify Docker build process and dependency management - Remove unnecessary environment variables and build flags - Update docker-build.sh to use BuildKit and remove lockfile before build
This commit is contained in:
27
Dockerfile
27
Dockerfile
@@ -1,9 +1,12 @@
|
|||||||
# Use Bun as the base image with specific platform for better optimization
|
# Use Node.js as base for building
|
||||||
FROM --platform=linux/amd64 oven/bun:1.2.2-slim as builder
|
FROM node:20-slim as builder
|
||||||
|
|
||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install bun
|
||||||
|
RUN npm install -g bun@1.0.25
|
||||||
|
|
||||||
# Install only the minimal dependencies needed and clean up in the same layer
|
# Install only the minimal dependencies needed and clean up in the same layer
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
ca-certificates \
|
ca-certificates \
|
||||||
@@ -14,30 +17,28 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|||||||
|
|
||||||
# Set build-time environment variables
|
# Set build-time environment variables
|
||||||
ENV NODE_ENV=production \
|
ENV NODE_ENV=production \
|
||||||
NODE_OPTIONS="--max-old-space-size=2048" \
|
NODE_OPTIONS="--max-old-space-size=2048"
|
||||||
BUN_INSTALL_CACHE=false \
|
|
||||||
BUN_INSTALL_VERBOSE=true
|
|
||||||
|
|
||||||
# Copy only package files first
|
# Copy only package files first
|
||||||
COPY package.json ./
|
COPY package.json ./
|
||||||
COPY tsconfig*.json ./
|
|
||||||
|
|
||||||
# Install ALL dependencies (including devDependencies) for build
|
# Install dependencies without lockfile
|
||||||
RUN bun install --no-cache \
|
RUN bun install --no-cache
|
||||||
--no-progress \
|
|
||||||
&& rm -rf ~/.bun/install/cache
|
|
||||||
|
|
||||||
# Copy source files and build
|
# Copy source files and build
|
||||||
COPY src ./src
|
COPY src ./src
|
||||||
|
COPY tsconfig*.json ./
|
||||||
RUN bun build ./src/index.ts --target=bun --minify --outdir=./dist
|
RUN bun build ./src/index.ts --target=bun --minify --outdir=./dist
|
||||||
|
|
||||||
# Create a smaller production image
|
# Create a smaller production image
|
||||||
FROM --platform=linux/amd64 oven/bun:1.2.2-slim as runner
|
FROM node:20-slim as runner
|
||||||
|
|
||||||
|
# Install bun in production image
|
||||||
|
RUN npm install -g bun@1.0.25
|
||||||
|
|
||||||
# Set production environment variables
|
# Set production environment variables
|
||||||
ENV NODE_ENV=production \
|
ENV NODE_ENV=production \
|
||||||
NODE_OPTIONS="--max-old-space-size=1024" \
|
NODE_OPTIONS="--max-old-space-size=1024"
|
||||||
BUN_INSTALL_CACHE=false
|
|
||||||
|
|
||||||
# Create a non-root user
|
# Create a non-root user
|
||||||
RUN addgroup --system --gid 1001 nodejs && \
|
RUN addgroup --system --gid 1001 nodejs && \
|
||||||
|
|||||||
@@ -28,9 +28,12 @@ CPU_QUOTA=$(( CPU_COUNT * 50000 )) # Allow 50% CPU usage per core
|
|||||||
|
|
||||||
echo "Building with ${BUILD_MEM}MB memory limit and CPU quota ${CPU_QUOTA}"
|
echo "Building with ${BUILD_MEM}MB memory limit and CPU quota ${CPU_QUOTA}"
|
||||||
|
|
||||||
|
# Remove any existing lockfile
|
||||||
|
rm -f bun.lockb
|
||||||
|
|
||||||
# Build with resource limits, optimizations, and timeout
|
# Build with resource limits, optimizations, and timeout
|
||||||
echo "Building Docker image..."
|
echo "Building Docker image..."
|
||||||
timeout 15m docker build \
|
DOCKER_BUILDKIT=1 docker build \
|
||||||
--memory="${BUILD_MEM}m" \
|
--memory="${BUILD_MEM}m" \
|
||||||
--memory-swap="${BUILD_MEM}m" \
|
--memory-swap="${BUILD_MEM}m" \
|
||||||
--cpu-quota="${CPU_QUOTA}" \
|
--cpu-quota="${CPU_QUOTA}" \
|
||||||
|
|||||||
Reference in New Issue
Block a user