From ecf11390814f7624d40558808ea4d592801ad297 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Fri, 8 Dec 2023 13:00:47 -1000 Subject: [PATCH] feat: Improve build system to reduce Docker image to 312 MB --- Dockerfile | 29 +++++++++++++++++------------ build.ts | 7 ++++++- bun.lockb | Bin 386592 -> 386592 bytes entrypoint.sh | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 13 deletions(-) create mode 100755 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index e8dcf77b..48e2eb8a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ # use the official Bun image # see all versions at https://hub.docker.com/r/oven/bun/tags -FROM oven/bun:1.0.14-alpine as base +FROM oven/bun:1.0.15-alpine as base WORKDIR /usr/src/app -RUN apk add vips-dev +RUN apk add vips # Required for Prisma to work COPY --from=node:18-alpine /usr/local/bin/node /usr/local/bin/node @@ -16,20 +16,28 @@ RUN cd /temp/dev && bun install --frozen-lockfile # install with --production (exclude devDependencies) RUN mkdir -p /temp/prod -COPY package.json bun.lockb /temp/prod/ +COPY . /temp/prod/ RUN cd /temp/prod && bun install --frozen-lockfile --production. +# Generate Prisma +RUN bunx prisma generate + +# Build Vite in pages +WORKDIR /temp/prod +RUN bunx --bun vite build pages + +# Build the project +RUN bun run build.ts + # copy production dependencies and source code into final image FROM base AS release # Create app directory RUN mkdir -p /app -COPY --from=install /temp/prod/node_modules /app/node_modules -COPY . /app +COPY --from=install /temp/prod/dist /app/dist +COPY --from=install /temp/prod/pages /app/pages +COPY entrypoint.sh /app -# Build Vite in pages -WORKDIR /app -RUN bunx --bun vite build pages LABEL org.opencontainers.image.authors "Gaspard Wierzbinski (https://cpluspatch.dev)" LABEL org.opencontainers.image.source "https://github.com/lysand-org/lysand" @@ -38,11 +46,8 @@ LABEL org.opencontainers.image.licenses "AGPL-3.0" LABEL org.opencontainers.image.title "Lysand Server" LABEL org.opencontainers.image.description "Lysand Server docker image" -# CD to app -WORKDIR /app -RUN bunx prisma generate # CD to app WORKDIR /app ENV NODE_ENV=production # Run migrations and start the server -ENTRYPOINT [ "bun", "migrate", "&&", "bun start" ] +ENTRYPOINT [ "./entrypoint.sh" "start" ] diff --git a/build.ts b/build.ts index d6d233b8..6467a466 100644 --- a/build.ts +++ b/build.ts @@ -1,5 +1,10 @@ +// Delete dist directory +import { rm } from "fs/promises"; + +await rm("./dist", { recursive: true }); + await Bun.build({ - entrypoints: ["./index.ts"], + entrypoints: ["./index.ts", "./prisma.ts", "./cli.ts"], outdir: "./dist", target: "bun", splitting: true, diff --git a/bun.lockb b/bun.lockb index fe46aaa71b9305723fc86e59570dc7df7c5a0c20..a757b99e158a28df4ba1eec6f9b4a0885254ff9f 100755 GIT binary patch delta 33 pcmZ2*Mts2;@rD-07N#xCM}M(1#u@4v=oz-3`Nh2b%rBOZ*8uBF4r~Ab delta 33 mcmZ2*Mts2;@rD-07N#xCM}M(1F@Qn)nP1G?&-`Kuc?|&BW(^?# diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 00000000..7b518ea0 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +# This script is a wrapper for the main server, CLI and Prisma binaries. +# Commands: +# - `start`: Starts the server +# - `cli`: Starts the CLI, sends all arguments to it +# - `prisma`: Execute a Prisma command, sends + +# Exit immediately if a command exits with a non-zero status. +set -e + +# Parse first argument +case "$1" in + "start") + # Start the server + exec bun run ./dist/index.js + ;; + "cli") + # Start the CLI + shift 1 + exec bun run ./dist/cli.js "$@" + ;; + "prisma") + # Proxy all Prisma commands + # Use output of dist/prisma.js to get the env variable + shift 1 + # Set DATABASE_URL env variable to the output of bun run ./dist/prisma.js + export DATABASE_URL=$(bun run ./dist/prisma.js) + # Execute the Prisma binary + exec bunx prisma "$@" + ;; + *) + # Run custom commands + exec "$@" + ;; +esac +``` \ No newline at end of file