Skip to content

Commit

Permalink
Add: docker support
Browse files Browse the repository at this point in the history
Update: README.md
  • Loading branch information
feightwywx committed Sep 27, 2022
1 parent d7fa52c commit b08945a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
57 changes: 57 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Install dependencies only when needed
FROM node:16-alpine 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; \
else echo "Lockfile not found." && exit 1; \
fi


# Rebuild the source code only when needed
FROM node:16-alpine 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 node:16-alpine 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

# 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

CMD ["node", "server.js"]
58 changes: 38 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
# homework-mis

## Getting Started
一个基于[Next.js](https://nextjs.org)的作业管理信息系统。

First, run the development server:
[Demo链接](https://homework-mis.drwf.ink)
[备用链接(Vercel)](https://homework-mis-tog4.vercel.app)

```bash
npm run dev
# or
yarn dev
```
## Get Started

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
#### 1. 安装依赖
```commandline
npm i
```

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.
#### 2. 数据库准备
本项目使用一个名为`homework_mis`的MySQL 8.0数据库。

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.
在调试之前,需要手动创建该数据库,并运行`sql/homework_mis.sql`来初始化表结构。

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
#### 3. 环境变量

## Learn More
本项目通过环境变量存储参数。你需要在项目根目录下创建一个`.env.local`文件,启动服务时Next.js会自动从中载入环境变量。`.env.local`的结构如下:

To learn more about Next.js, take a look at the following resources:
```ini
SECRET_COOKIE_PASSWORD=
MYSQL_HOST=
MYSQL_USER=
MYSQL_PORT= # 默认3306
MYSQL_PASSWORD=
```

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
#### 4. 开始调试
```commandline
npm run dev
```

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
## 通过Docker部署

## Deploy on Vercel
本项目提供了`linux/amd64``linux/amd64`[Docker镜像](https://hub.docker.com/r/dotdirewolf/hwmis-docker)

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
以下命令将会拉取镜像,启动一个名为的`hwmis`的容器,并将容器内`3000`端口映射到随机端口。记得替换成你自己的环境变量。

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
```commandline
docker pull dotdirewolf/hwmis-docker
docker run -itdP --name hwmis \
-e SECRET_COOKIE_PASSWORD= \
-e MYSQL_HOST= \
-e MYSQL_USER= \
-e MYSQL_PORT= \
-e MYSQL_PASSWORD= \
dotdirewolf/hwmis-docker
```
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ const withLess = require("next-with-less");
module.exports = withLess({
lessLoaderOptions: {
},
output: 'standalone',
...nextConfig
});

1 comment on commit b08945a

@vercel
Copy link

@vercel vercel bot commented on b08945a Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.