From 8d0d08725d9bed14f6aea3cb17923ee24f4cac40 Mon Sep 17 00:00:00 2001 From: AprilNEA Date: Sun, 26 Mar 2023 16:56:05 +0800 Subject: [PATCH 1/3] feat: add Dockerfile for docker deployment support --- Dockerfile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..e60534f9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM node:18 + +WORKDIR /app + +ENV OPENAI_API_KEY="" +ENV CODE="" + +COPY . ./ + +RUN yarn && yarn build + +EXPOSE 3000 + +CMD ["yarn","start"] From 2645540721a457b8772730e65ff16c86da45108f Mon Sep 17 00:00:00 2001 From: AprilNEA Date: Mon, 27 Mar 2023 13:49:26 +0800 Subject: [PATCH 2/3] perf: build in stages to reduce container size --- Dockerfile | 40 +++++++++++++++++++++++++++++++++++----- next.config.js | 4 ++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index e60534f9..314d6c36 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,44 @@ -FROM node:18 +FROM node:18-alpine AS base +FROM base AS deps + +RUN apk add --no-cache libc6-compat + +WORKDIR /app + +COPY package.json yarn.lock* package-lock.json* ./ + +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + else echo "Lockfile not found." && exit 1; \ + fi + +FROM base AS builder + +RUN apk update && apk add --no-cache git + +ENV OPENAI_API_KEY="" +ENV CODE="" +ARG DOCKER=true + +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +RUN yarn build + +FROM base AS runner WORKDIR /app ENV OPENAI_API_KEY="" ENV CODE="" -COPY . ./ - -RUN yarn && yarn build +COPY --from=builder /app/public ./public +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static +COPY --from=builder /app/.next/server ./.next/server EXPOSE 3000 -CMD ["yarn","start"] +CMD ["node","server.js"] diff --git a/next.config.js b/next.config.js index eea258d3..fc164db9 100644 --- a/next.config.js +++ b/next.config.js @@ -14,4 +14,8 @@ const nextConfig = { } }; +if (process.env.DOCKER) { + nextConfig.output = 'standalone' +} + module.exports = nextConfig; From e880df6db92471dc7fe53ab61fad5155043405ec Mon Sep 17 00:00:00 2001 From: AprilNEA Date: Mon, 27 Mar 2023 13:59:23 +0800 Subject: [PATCH 3/3] docs: add instructions for docker deployment --- README.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index de57e4bb..d7ca656d 100644 --- a/README.md +++ b/README.md @@ -125,16 +125,30 @@ Please ask ChatGPT with prompt: how to deploy nextjs project with pm2 and yarn on my ubuntu server, the build command is `yarn build`, the start command is `yarn start`, the project must start with env var named `OPENAI_API_KEY`, the port is 3000, use ngnix ``` -### Docker Deployment -请直接询问 ChatGPT,使用下列 Prompt: +### 容器部署 Docker Deployment + +1. 首先拉取镜像 +```shell +docker pull aprilnea/chatgpt-next-web ``` -如何使用 docker 部署 nextjs 项目到 ubuntu 服务器上,项目编译命令为 yarn build,启动命令为 yarn start,启动时需要设置环境变量为 OPENAI_API_KEY,端口为 3000,使用 ngnix 做反向代理 +2. 运行 +```shell +docker run -d -p 3000:3000 -e OPEN_API_KEY="" -e CODE="" aprilnea/chatgpt-next-web +``` +请在适当位置替换你的 `OPEN_API-KEY` 和 `CODE` + + +1. First, pull the image +```shell +docker pull aprilnea/chatgpt-next-web ``` -Please ask ChatGPT with prompt: -``` -how to deploy nextjs project with docker on my ubuntu server, the build command is `yarn build`, the start command is `yarn start`, the project must start with env var named `OPENAI_API_KEY`, the port is 3000, use ngnix +2. Run the container +```shell +docker run -d -p 3000:3000 -e OPEN_API_KEY="" -e CODE="" aprilnea/chatgpt-next-web ``` +Please replace `OPEN_API_KEY` and `CODE` with your own credentials at the appropriate locations. + ## 截图 Screenshots