this dockerfile rocks now
This commit is contained in:
43
Dockerfile
43
Dockerfile
@@ -18,27 +18,52 @@ RUN --mount=type=cache,target=/root/.local/share/pnpm/store/v3 \
|
|||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Use a second cache mount for the Vinxi build artifacts.
|
# 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 \
|
RUN --mount=type=cache,target=/app/.vinxi \
|
||||||
pnpm run build
|
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
|
FROM node:22-alpine
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Enable corepack and install only production dependencies
|
# Enable corepack
|
||||||
RUN corepack enable && corepack prepare pnpm@latest --activate
|
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 \
|
RUN --mount=type=cache,target=/root/.local/share/pnpm/store/v3 \
|
||||||
pnpm install --frozen-lockfile --prod
|
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/.output ./.output
|
||||||
COPY --from=builder /app/package.json ./package.json
|
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "games",
|
"name": "games",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
"version": "0.0.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vinxi dev",
|
"dev": "vinxi dev",
|
||||||
"build": "vinxi build",
|
"build": "vinxi build",
|
||||||
|
|||||||
Reference in New Issue
Block a user