this dockerfile rocks now

This commit is contained in:
2025-08-01 00:02:24 -04:00
parent 27d47764f8
commit 5480e2921b
2 changed files with 35 additions and 9 deletions

View File

@@ -18,27 +18,52 @@ RUN --mount=type=cache,target=/root/.local/share/pnpm/store/v3 \
COPY . .
# Use a second cache mount for the Vinxi build artifacts.
# This tells Docker to mount a persistent cache directory to a build-cache location.
# The `pnpm run build` command will now use this cache for incremental builds.
# We'll assume a `.vinxi` or `.cache` directory for this example.
RUN --mount=type=cache,target=/app/.vinxi \
pnpm run build
# === STAGE 2: Production Final Image ===
# === STAGE 2: Generate Production-only Files ===
FROM node:22 AS production_builder
RUN corepack enable && corepack prepare pnpm@latest --activate
# Install jq to process the package.json file.
# Note: jq is not in the base image, so we must install it.
RUN apt-get update && apt-get install -y jq
WORKDIR /app
# Copy the original lockfile and package.json from the builder stage.
COPY --from=builder /app/package.json ./
COPY --from=builder /app/pnpm-lock.yaml ./
# Create a new, production-only package.json file.
# We use 'jq' to remove the 'devDependencies' key from the original package.json.
RUN jq 'del(.devDependencies)' package.json > package.prod.json
# Now, generate a production-only pnpm-lock.yaml
# We run `pnpm install` again, but with `--prod`, and it will
# generate a new lockfile based on the stripped-down dependencies.
RUN --mount=type=cache,target=/root/.local/share/pnpm/store/v3 pnpm install --prod
RUN --mount=type=cache,target=/root/.local/share/pnpm/store/v3 pnpm prune --prod
# === STAGE 3: Final Production Image ===
FROM node:22-alpine
WORKDIR /app
# Enable corepack and install only production dependencies
# Enable corepack
RUN corepack enable && corepack prepare pnpm@latest --activate
COPY package.json pnpm-lock.yaml ./
# This install can also use the cache mount to avoid re-downloading
# Copy the production-only files from the production_builder stage.
COPY --from=production_builder /app/package.prod.json ./package.json
COPY --from=production_builder /app/pnpm-lock.yaml ./pnpm-lock.yaml
# Now pnpm install will be very fast, as the cache is not invalidated by devDependency changes.
# It uses the production-only package.json and lockfile.
RUN --mount=type=cache,target=/root/.local/share/pnpm/store/v3 \
pnpm install --frozen-lockfile --prod
# Copy the compiled assets and lockfile from the builder stage
# Copy the compiled assets from the original builder stage.
COPY --from=builder /app/.output ./.output
COPY --from=builder /app/package.json ./package.json
EXPOSE 3000

View File

@@ -1,6 +1,7 @@
{
"name": "games",
"type": "module",
"version": "0.0.0",
"scripts": {
"dev": "vinxi dev",
"build": "vinxi build",