Skip to content

Commit

Permalink
build(docker): setup production build
Browse files Browse the repository at this point in the history
  • Loading branch information
Callenowy committed Jan 29, 2024
1 parent 6c0107b commit f66ce81
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
36 changes: 36 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
90 changes: 90 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
FROM node:20-alpine AS base

# Install dependencies only when needed
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 npm pkg set scripts.postinstall="echo no-postinstall"
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi


# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .


# Environment variables must be present at build time
# https://github.com/vercel/next.js/discussions/14030
ARG NEXT_PUBLIC_HOST
ENV NEXT_PUBLIC_HOST=${NEXT_PUBLIC_HOST}
ARG DB_PATH
ENV DB_PATH=${DB_PATH}
ARG APP_PEPPER
ENV APP_PEPPER=${APP_PEPPER}
ARG NEXTAUTH_SECRET
ENV NEXTAUTH_SECRET=${NEXTAUTH_SECRET}
ARG NEXTAUTH_URL
ENV NEXTAUTH_URL=${NEXTAUTH_URL}

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1
RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
USER nextjs

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

ARG NEXT_PUBLIC_HOST
ENV NEXT_PUBLIC_HOST=${NEXT_PUBLIC_HOST}
ARG DB_PATH
ENV DB_PATH=${DB_PATH}
ARG APP_PEPPER
ENV APP_PEPPER=${APP_PEPPER}
ARG NEXTAUTH_SECRET
ENV NEXTAUTH_SECRET=${NEXTAUTH_SECRET}
ARG NEXTAUTH_URL
ENV NEXTAUTH_URL=${NEXTAUTH_URL}

# EXPOSE 3000

# ENV PORT 3000
# # set hostname to localhost
# ENV HOSTNAME "0.0.0.0"

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "server.js"]
26 changes: 26 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3'

services:
next-app:
container_name: the_stack_app
build:
context: .
dockerfile: Dockerfile
args:
NEXT_PUBLIC_HOST: ${NEXT_PUBLIC_HOST}
DB_PATH: ${DB_PATH}
APP_PEPPER: ${APP_PEPPER}
NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}
NEXTAUTH_URL: ${NEXTAUTH_URL}
volumes:
- ./src:/app/src
- ./public:/app/public
restart: always
ports:
- 3000:3000
networks:
- the_stack_network

networks:
the_stack_network:
external: true
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"start": "cross-env NODE_ENV=production node .next/standalone/server.js",
"lint": "next lint",
"format": "prettier --write \"**/*.{ts,tsx,js,jsx,md}\"",
"format:check": "prettier --list-different \"**/*.{ts,tsx,js,jsx,md}\"",
Expand Down Expand Up @@ -65,6 +65,7 @@
"@vitest/coverage-v8": "^1.2.1",
"@vitest/ui": "^1.2.1",
"autoprefixer": "^10.0.1",
"cross-env": "^7.0.3",
"drizzle-kit": "^0.20.13",
"eslint": "^8",
"eslint-config-next": "14.0.4",
Expand Down

0 comments on commit f66ce81

Please sign in to comment.