chore: update project dependencies and build configuration

- Remove bun.lockb from version control
- Add comprehensive docker-build.sh script for optimized Docker builds
- Update Dockerfile with multi-stage build and improved resource management
- Add winston logging dependencies to package.json
- Enhance Docker image build process with resource constraints and caching
This commit is contained in:
jango-blockchained
2025-02-04 20:14:13 +01:00
parent f8bbe4af6f
commit 849b080aba
6 changed files with 637 additions and 20 deletions

View File

@@ -1,36 +1,64 @@
# Use Bun as the base image
FROM oven/bun:1.0.25 as builder
# Use Bun as the base image with specific platform for better optimization
FROM --platform=linux/amd64 oven/bun:1.2.2-slim as builder
# Set working directory
WORKDIR /app
# Copy only package files first for better layer caching
COPY package.json bun.lockb ./
# Install only the minimal dependencies needed and clean up in the same layer
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
curl \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean \
&& rm -rf /var/cache/apt/*
# Install dependencies with production flag
RUN bun install --frozen-lockfile --production
# Set build-time environment variables
ENV NODE_ENV=production \
NODE_OPTIONS="--max-old-space-size=2048" \
BUN_INSTALL_CACHE=false \
BUN_INSTALL_VERBOSE=true
# Copy source code
COPY . .
# Copy only package files first
COPY package.json ./
COPY tsconfig*.json ./
# Build TypeScript
RUN bun run build
# Install ALL dependencies (including devDependencies) for build
RUN bun install --no-cache \
--no-progress \
&& rm -rf ~/.bun/install/cache
# Create production image
FROM oven/bun:1.0.25-slim
# Copy source files and build
COPY src ./src
RUN bun build ./src/index.ts --target=bun --minify --outdir=./dist
# Create a smaller production image
FROM --platform=linux/amd64 oven/bun:1.2.2-slim as runner
# Set production environment variables
ENV NODE_ENV=production \
NODE_OPTIONS="--max-old-space-size=1024" \
BUN_INSTALL_CACHE=false
# Create a non-root user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 bunjs
WORKDIR /app
# Copy only the necessary files from builder
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package.json ./
COPY --from=builder --chown=bunjs:nodejs /app/dist ./dist
COPY --from=builder --chown=bunjs:nodejs /app/node_modules ./node_modules
COPY --chown=bunjs:nodejs package.json ./
# Set production environment
ENV NODE_ENV=production
# Switch to non-root user
USER bunjs
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:4000/health || exit 1
# Expose port
EXPOSE 4000
# Start the application
CMD ["bun", "run", "start"]
# Start the application with optimized flags
CMD ["bun", "--smol", "run", "start"]