62 lines
1.9 KiB
Docker
62 lines
1.9 KiB
Docker
FROM node:22-alpine AS base
|
|
|
|
# Stage 1: Install dependencies
|
|
FROM base AS deps
|
|
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
|
|
RUN apk add --no-cache libc6-compat
|
|
WORKDIR /app
|
|
|
|
# Install dependencies based on the preferred package manager
|
|
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
|
|
RUN \
|
|
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
|
|
elif [ -f package-lock.json ]; then npm ci; \
|
|
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
|
|
else echo "Lockfile not found." && exit 1; \
|
|
fi
|
|
|
|
|
|
# Stage 2: Rebuild the source code only when needed
|
|
FROM base AS builder
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
|
|
# Environment variables needed during build time
|
|
ARG INSTAGRAM_ACCESS_TOKEN
|
|
ENV INSTAGRAM_ACCESS_TOKEN=$INSTAGRAM_ACCESS_TOKEN
|
|
|
|
RUN \
|
|
if [ -f yarn.lock ]; then yarn build; \
|
|
elif [ -f package-lock.json ]; then npm run build; \
|
|
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
|
|
else npm run build; \
|
|
fi
|
|
|
|
|
|
# Stage 3: Production image, copy all the files and run nitro
|
|
FROM base AS runner
|
|
WORKDIR /app
|
|
|
|
ENV NODE_ENV=production
|
|
# Set host to 0.0.0.0 to ensure it's accessible outside the container
|
|
ENV HOST=0.0.0.0
|
|
ENV PORT=3000
|
|
|
|
RUN addgroup --system --gid 1001 nodejs
|
|
RUN adduser --system --uid 1001 nitrojs
|
|
|
|
# Copy the build output from the builder stage
|
|
# TanStack Start/Nitro builds into the .output directory
|
|
COPY --from=builder --chown=nitrojs:nodejs /app/.output ./.output
|
|
|
|
USER nitrojs
|
|
|
|
EXPOSE 3000
|
|
|
|
# Health check to ensure the server is responding
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
|
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/ || exit 1
|
|
|
|
# server.mjs is created by nitro build
|
|
CMD ["node", ".output/server/index.mjs"] |