Skip to content

Commit

Permalink
Merge branch 'standalone'
Browse files Browse the repository at this point in the history
  • Loading branch information
andriilive committed Sep 23, 2024
2 parents 28e2010 + 60afb9b commit 282c988
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 11 deletions.
72 changes: 72 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
FROM node:18-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 \
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 .. .

# 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 yarn build

# If using npm comment out above and use below instead
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

# 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

USER nextjs

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"]
61 changes: 54 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
[![StandWithUkraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg)](https://github.com/vshymanskyy/StandWithUkraine)  
[![Github Badge](https://img.shields.io/github/followers/digitalandyeu?label=@digitalandyeu&style=social)](https://www.github.com/digitalandyeu)

Next.js box starter for EasyPanel. Supports all Next.js features. See also [Next.js with Docker for EasyPanel](https://github.com/digitalandyeu/next-with-docker)
Next.js box starter for EasyPanel. Supports all Next.js features. See also [Next.js with Docker for EasyPanel guide](https://gist.github.com/andriilive/38e0c30ad0e68e01d0bfc5250998de50)

- Supports `public` directory and **images optimization**
- Choose between **node.js server** or `standalone` build
![preview.png](preview.png)

## Node.js Server (Recommended)
- **Supports all Next.js features**
- Auto **git-deployments** and **CI/CD** and more
- Automatic **SSL** and certificates management

Choose between `node.js` server or `standalone` build

### Node.js Server

Next.js can be deployed to any hosting provider that supports Node.js. Ensure your `package.json` file has the following
scripts:
Expand All @@ -23,7 +28,7 @@ scripts:
}
```

## Standalone setup
### Standalone setup (Recommended)

To use this box as a standalone app, you need to add a `next.config.js` file to the root of your project with the following content:

Expand All @@ -36,20 +41,62 @@ const nextConfig = {
export default nextConfig;
```

Modify the npm scripts in `package.json` to use `node ./.next/standalone/server.js` instead of `next start` and copy the `public` and `static` directories to the `.next/standalone` directory:
Modify the npm scripts in `package.json` to use for standalone build:

```json
{
"dev": "next dev",
"build": "next build",
"lint": "next lint",
"start": "node ./.next/standalone/server.js",
"start": "node ./.next/standalone/server.js"
}
```

Add the following scripts to `package.json` to copy the public and static directories to the standalone build:

```json
{
"postbuild": "npm-run-all -s export:*",
"export:public": "cp -r ./public ./.next/standalone",
"export:static": "cp -r ./.next/static ./.next/standalone/.next"
}
```

## Local docker development

```bash
docker build -t nextjs-easypanel -f ./Dockerfile .
docker run -p 3000:3000 nextjs-easypanel
```

## EasyPanel configuration

Create a new box from GitHub repository.

### Deployment Script

Note, the npm ci command is used to install dependencies. This command is faster and more reliable than npm install.

```bash
cd /code
npm ci
npm run build
supervisorctl restart nodejs-server
```

### Processes script

Create a new process to run npm start.

Name: `nodejs-server` process
Directory: `/code` (or `/code/.next/standalone`)
Command: `npm run start`

### Node.js

- **Node.js version**: 18
- Yarn / pnpm: **Disabled**

---

This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "nextjs-easypanel-box",
"name": "nextjs-easypanel",
"version": "0.1.0",
"private": true,
"homepage": "https://github.com/digitalandyeu/nextjs-easypanel-box",
"homepage": "https://github.com/digitalandyeu/nextjs-easypanel",
"license": "MIT",
"scripts": {
"dev": "next dev",
Expand Down
Binary file added preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 282c988

Please sign in to comment.