Skip to main content

DeployToSynology

How to deploy NextJS to Synology NAS

  • most reliable way to deploy a Next.js app on a Synology NAS is using Docker via Synology’s Container Manager

Optimize NextJS Config

  • modify the next.config.ts file
  • turn on standalone output, which creates a tiny, self-contained production bundle
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
output: 'standalone', // <--- Crucial for Docker deployments
/* your other config options here */
};

export default nextConfig;

Create a Production Dockerfile

  • In the root directory of your Next.js project, create a file named Dockerfile (no extension)
FROM node:20-alpine AS base

# 1. Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json* yarn.lock* pnpm-lock.yaml* ./
RUN \
if [ -f package-lock.json ]; then npm ci; \
elif [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi

# 2. Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# If you have environment variables needed at BUILD TIME (like NEXT_PUBLIC_ variables),
# uncomment the line below and add them here:
# ENV NEXT_PUBLIC_API_URL=https://api.example.com
RUN npm run build

# 3. Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
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=3000
ENV HOSTNAME="0.0.0.0"

CMD ["node", "server.js"]

Build & Push the Image

  • make sure that Docker Desktop is running on your computer
  • build the image on your computer
  • export it as a .tar file and upload it to the NAS
docker build --platform linux/amd64 -t myapp:latest .
docker save -o myapp.tar myapp:latest

Deploy on Synology Container Manager

  1. Install Container Manager
  2. Import Your Image (as file from the docker folder)
  3. Run the Image
  4. Add Environment Variables as needed
DB_HOST=192.168.0.100
DB_PORT=3306
DB_NAME=myapp
DB_USER=root
DB_PASSWORD=xxxxxxxx