Skip to content

Commit f8f9a52

Browse files
committed
添加本地docker部署方式
1 parent 6b87b63 commit f8f9a52

File tree

6 files changed

+832
-2
lines changed

6 files changed

+832
-2
lines changed

Dockerfile

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Use a Node.js 18 Alpine base image for the backend builder
2+
# 使用 Node.js 18 Alpine 基础镜像作为后端构建器
3+
FROM node:18-alpine AS backend-builder
4+
5+
WORKDIR /app
6+
7+
# Copy package.json first to leverage Docker's caching
8+
# 首先复制 package.json 以利用 Docker 的缓存
9+
COPY server/package.json ./server/
10+
# Install production dependencies
11+
# 安装生产依赖
12+
RUN cd server && npm install --production --no-package-lock --no-audit
13+
14+
# Copy only the server.js file
15+
# 仅复制 server.js 文件
16+
COPY server/server.js ./server/
17+
18+
# Frontend build stage
19+
# 前端构建阶段
20+
FROM node:18-alpine AS frontend-builder
21+
22+
WORKDIR /app
23+
24+
# Copy frontend dependencies and configuration files
25+
# 复制前端依赖和配置文件
26+
COPY package.json package-lock.json* ./
27+
# Install dependencies
28+
# 安装依赖
29+
RUN npm ci --no-audit || npm install --no-audit
30+
31+
# Copy source code and configuration files
32+
# 复制源代码和配置文件
33+
COPY vite.config.js ./
34+
COPY client/ ./client/
35+
36+
# Build the frontend
37+
# 构建前端
38+
RUN npm run build:docker
39+
40+
# Second stage: Minimal image
41+
# 第二阶段:极小镜像
42+
FROM alpine:3.16
43+
44+
# Install minimal Node.js and Nginx
45+
# 安装最小化版本的 Node.js 和 Nginx
46+
RUN apk add --no-cache nodejs nginx && \
47+
mkdir -p /app/server /app/client /run/nginx && \
48+
# Clean up apk cache
49+
# 清理 apk 缓存
50+
rm -rf /var/cache/apk/*
51+
52+
# Copy server files and static files
53+
# 复制服务器文件和静态文件
54+
COPY --from=backend-builder /app/server/node_modules /app/server/node_modules
55+
COPY --from=backend-builder /app/server/*.js /app/server/
56+
# Copy built frontend files from the frontend build stage
57+
# 从前端构建阶段复制构建好的文件,而不是复制 dist 目录
58+
COPY --from=frontend-builder /app/dist/ /app/client/
59+
60+
# Optimized Nginx configuration
61+
# 优化的 Nginx 配置
62+
RUN cat > /etc/nginx/nginx.conf <<'EOF'
63+
worker_processes 1;
64+
worker_rlimit_nofile 512;
65+
events {
66+
worker_connections 128;
67+
multi_accept off;
68+
}
69+
http {
70+
include mime.types;
71+
default_type application/octet-stream;
72+
73+
# Optimization settings
74+
# 优化设置
75+
sendfile on;
76+
tcp_nopush on;
77+
tcp_nodelay on;
78+
keepalive_timeout 15;
79+
types_hash_max_size 1024;
80+
client_max_body_size 1M;
81+
client_body_buffer_size 128k;
82+
83+
# Disable access logs to reduce I/O
84+
# 禁用访问日志以减少 I/O
85+
access_log off;
86+
error_log /dev/null;
87+
# Disable unnecessary features
88+
# 禁用不需要的功能
89+
server_tokens off;
90+
91+
# Map to handle WebSocket upgrade detection
92+
# 映射处理 WebSocket 升级检测
93+
map $http_upgrade $connection_upgrade {
94+
default upgrade;
95+
'' close;
96+
}
97+
98+
server {
99+
listen 80;
100+
server_name localhost;
101+
102+
# Main location block - handles both HTTP and WebSocket
103+
# 主位置块 - 处理 HTTP 和 WebSocket
104+
location / {
105+
# Check if this is a WebSocket upgrade request
106+
# 检查是否为 WebSocket 升级请求
107+
if ($http_upgrade = "websocket") {
108+
proxy_pass http://127.0.0.1:8088;
109+
break;
110+
}
111+
112+
# For WebSocket requests, proxy to Node.js backend
113+
# 对于 WebSocket 请求,代理到 Node.js 后端
114+
proxy_http_version 1.1;
115+
proxy_set_header Upgrade $http_upgrade;
116+
proxy_set_header Connection $connection_upgrade;
117+
proxy_set_header Host $host;
118+
proxy_set_header X-Real-IP $remote_addr;
119+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
120+
proxy_set_header X-Forwarded-Proto $scheme;
121+
122+
# For regular HTTP requests, serve static files
123+
# 对于常规 HTTP 请求,提供静态文件
124+
root /app/client;
125+
index index.html;
126+
try_files $uri $uri/ /index.html;
127+
}
128+
}
129+
}
130+
EOF
131+
132+
EXPOSE 80
133+
134+
# Set low memory environment variables and remove unsupported options
135+
# 设置低内存环境变量,去除不支持的选项
136+
137+
# Run in the foreground and combine commands to reduce the number of processes
138+
# 使用前台运行并合并命令减少进程数
139+
CMD ["sh", "-c", "node --expose-gc --unhandled-rejections=strict /app/server/server.js & nginx -g 'daemon off;'"]

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
2. 打开 Cloudflare Workers 控制台,选择“从 GitHub 导入”,并选择你 fork 的仓库进行部署。
1616
> 本项目已内置自动同步 workflow,fork 后无需任何操作,主仓库的更新会自动同步到你的 fork 仓库,Cloudflare 也会自动重新部署,无需手动维护。
1717
18-
### 方法三:本地开发部署
18+
### 方法三:Docker 一键部署(推荐自托管)
19+
20+
```bash
21+
docker run -d --name nodecrypt -p 80:80 shua1/nodecrypt
22+
```
23+
24+
访问 http://localhost:80
25+
26+
### 方法四:本地开发部署
1927
克隆项目并安装依赖后,使用 `npm run dev` 启动开发服务器。
2028
使用 `npm run deploy` 部署到 Cloudflare Workers。
2129

README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ Click the button below for one-click deployment to Cloudflare Workers:
1515
2. Open the Cloudflare Workers console, select "Import from GitHub," and choose your forked repository for deployment.
1616
> This project has built-in auto-sync workflow. After forking, no action is required. Updates from the main repository will automatically sync to your fork, and Cloudflare will automatically redeploy without manual maintenance.
1717
18-
### Method 3: Local Development Deployment
18+
### Method 3: Docker One-Click Deployment (Recommended for Self-hosting)
19+
20+
```bash
21+
docker run -d --name nodecrypt -p 80:80 shua1/nodecrypt
22+
```
23+
24+
Access http://localhost:80
25+
26+
### Method 4: Local Development Deployment
1927
After cloning the project and installing dependencies, use `npm run dev` to start the development server.
2028
Use `npm run deploy` to deploy to Cloudflare Workers.
2129

server/package-lock.json

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "nodecrypt-server",
3+
"version": "1.0.0",
4+
"description": "NodeCrypt Server",
5+
"main": "server.js",
6+
"author": "Zoltan Hajdu",
7+
"license": "ISC",
8+
"dependencies": {
9+
"ws": "^7.3.1",
10+
"bufferutil": "^4.0.1",
11+
"utf-8-validate": "^5.0.2"
12+
}
13+
}

0 commit comments

Comments
 (0)