Iman Sugirman

Setup Nextjs Docker Deploy Ke Fly.io

26 Juli 2022

Setup nextjs dengan docker untuk Deploy ke Fly.io, karena sangat cepat dan andal

Buat Project dengan Nextjs :

yarn create next-app nextfly

Lalu ubah next.config.js

const nextConfig = { reactStrictMode: true, // config output akan menjelaskan ke nextjs bahwa akan menggunakan docker output: 'standalone', }; module.exports = nextConfig;

dan Buat file Dockerfile seperti ini

# Install dependencies only when needed # Dockerfile FROM node:16-alpine AS deps RUN apk add --no-cache libc6-compat WORKDIR /app COPY package.json yarn.lock ./ RUN yarn install --frozen-lockfile FROM node:16-alpine AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . ENV NEXT_TELEMETRY_DISABLED 1 ARG NEXT_PUBLIC_API_URL RUN yarn build FROM node:16-alpine AS runner WORKDIR /app ENV NODE_ENV production ENV NEXT_TELEMETRY_DISABLED 1 RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs COPY --from=builder /app/next.config.js ./ COPY --from=builder /app/public ./public COPY --from=builder /app/package.json ./package.json COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static USER nextjs EXPOSE 3000 ENV PORT 8080 CMD ["node", "server.js"]

dan deploy dengan perintah :

flyctl deploy