Skip to content

Commit 3b334ae

Browse files
committed
feat: add Docker deployment support
- Add Dockerfile based on Debian bookworm-slim with all dependencies - Add fake-systemctl to replace systemd in container environment - Supports xray, nginx, fail2ban, cron service management - Handles systemctl is-active --quiet syntax - Silently ignores unknown services (e.g. iptables) - Add docker-entrypoint.sh with process management - Watchdog auto-restarts crashed services every 30s - Falls back to daemon mode after interactive menu exits - Supports idleleo/start/shell running modes - Add docker-compose.yml with host network and data volumes - Add .dockerignore to exclude unnecessary files from build context - Add DOCKER.md deployment guide in 6 languages (zh, en, fr, ru, fa, ko) - Add Docker deployment section to all README.md files refactor: redesign Docker as pre-built image with env var config - Rewrite Dockerfile to pre-install Xray and Nginx at build time - Xray installed via official install-release.sh - Nginx downloaded from project's custom build releases - Config templates copied from VLESS_reality/tls/xtls directories - Rewrite docker-entrypoint.sh to generate config from env vars - Supports reality/tls/xtls modes via MODE env var - Auto-generates UUID and Reality keys if not provided - Uses jq to modify JSON templates, envsubst for Nginx config - Starts services directly (exec xray), no systemd needed - Remove fake-systemctl (no longer needed) - Add nginx-tls.conf template for TLS mode - Simplify docker-compose.yml with env var configuration - Update all DOCKER.md docs (6 languages) for new approach - Update all README.md Docker sections (6 languages) Revert "refactor: redesign Docker as pre-built image with env var config" This reverts commit a2b9981.
1 parent bcd8717 commit 3b334ae

17 files changed

Lines changed: 1444 additions & 0 deletions

.dockerignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.github/
2+
.git/
3+
.gitignore
4+
.editorconfig
5+
LICENSE
6+
README.md
7+
DOCKER.md
8+
*.md
9+
po/
10+
Dockerfile
11+
docker-compose.yml
12+
docker-entrypoint.sh
13+
fake-systemctl
14+
.dockerignore
15+
translate.py
16+
translate_readme.py

DOCKER.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Docker 部署指南
2+
3+
简体中文 | [English](/languages/en/DOCKER.md) | [Français](/languages/fr/DOCKER.md) | [Русский](/languages/ru/DOCKER.md) | [فارسی](/languages/fa/DOCKER.md) | [한국어](/languages/ko/DOCKER.md)
4+
5+
本文档介绍如何使用 Docker 部署 Xray 一键脚本。
6+
7+
## 前提条件
8+
9+
* 已安装 Docker 和 Docker Compose
10+
* 服务器具备公网 IP
11+
* 安装 Reality 协议:需准备符合 Xray 要求的目标域名
12+
* 安装 TLS 版本:需准备域名并添加 A 记录
13+
14+
## 快速启动
15+
16+
### 1. 克隆仓库
17+
18+
```bash
19+
git clone https://github.com/hello-yunshu/Xray_bash_onekey.git
20+
cd Xray_bash_onekey
21+
```
22+
23+
### 2. 构建并启动容器
24+
25+
```bash
26+
docker compose up -d
27+
```
28+
29+
### 3. 进入交互式安装菜单
30+
31+
```bash
32+
docker attach xray-onekey
33+
```
34+
35+
首次运行时,容器会自动启动安装脚本,按照提示完成配置即可。
36+
37+
## 运行模式
38+
39+
容器支持以下运行模式:
40+
41+
| 模式 | 说明 | 命令 |
42+
|------|------|------|
43+
| `idleleo`(默认) | 启动服务并进入交互式管理菜单 | `docker compose up -d` |
44+
| `start` | 仅启动服务(守护模式) | 修改 `docker-compose.yml` 中的 `command: start` |
45+
| `shell` | 启动服务并进入 Shell | `docker exec -it xray-onekey bash` |
46+
47+
## 管理操作
48+
49+
### 进入管理菜单
50+
51+
```bash
52+
docker exec -it xray-onekey idleleo
53+
```
54+
55+
### 查看服务状态
56+
57+
```bash
58+
docker exec -it xray-onekey systemctl status xray
59+
docker exec -it xray-onekey systemctl status nginx
60+
```
61+
62+
### 重启服务
63+
64+
```bash
65+
docker exec -it xray-onekey systemctl restart xray
66+
docker exec -it xray-onekey systemctl restart nginx
67+
```
68+
69+
### 查看客户端配置
70+
71+
```bash
72+
docker exec -it xray-onekey cat /etc/idleleo/info/xray_info.inf
73+
```
74+
75+
### 查看日志
76+
77+
```bash
78+
docker exec -it xray-onekey cat /var/log/xray/access.log
79+
docker exec -it xray-onekey cat /var/log/xray/error.log
80+
```
81+
82+
## 使用 docker run(替代 docker compose)
83+
84+
```bash
85+
docker build -t xray-onekey .
86+
87+
docker run -d --name xray-onekey \
88+
--network host \
89+
--cap-add NET_ADMIN \
90+
-e TZ=Asia/Shanghai \
91+
-v xray-conf:/etc/idleleo/conf \
92+
-v xray-cert:/etc/idleleo/cert \
93+
-v xray-info:/etc/idleleo/info \
94+
-v xray-logs:/var/log/xray \
95+
-v acme-data:/root/.acme.sh \
96+
-it xray-onekey
97+
```
98+
99+
## 数据持久化
100+
101+
容器使用 Docker Volume 保存数据,重建容器后配置不会丢失:
102+
103+
| Volume | 容器路径 | 说明 |
104+
|--------|---------|------|
105+
| `xray-conf` | `/etc/idleleo/conf` | Xray 和 Nginx 配置文件 |
106+
| `xray-cert` | `/etc/idleleo/cert` | SSL 证书文件 |
107+
| `xray-info` | `/etc/idleleo/info` | 连接信息和状态文件 |
108+
| `xray-logs` | `/var/log/xray` | Xray 日志文件 |
109+
| `acme-data` | `/root/.acme.sh` | acme.sh 证书签发数据 |
110+
111+
## 自定义证书
112+
113+
`xray.crt``xray.key` 文件放入证书 Volume 对应的宿主机路径。使用 `docker volume inspect xray-cert` 查看宿主机路径。
114+
115+
## 网络配置
116+
117+
容器默认使用 `network_mode: host`,即直接使用宿主机网络。这对 Xray 代理服务至关重要:
118+
119+
* Reality 模式需要看到真实客户端 IP
120+
* TLS 模式需要直接绑定 443/80 端口
121+
* 避免额外的 NAT 转发性能损耗
122+
123+
## 注意事项
124+
125+
* 容器内使用 `fake-systemctl` 替代 systemd,`systemctl` 命令可正常使用
126+
* 防火墙建议在宿主机层面管理,而非容器内
127+
* 容器内置看门狗(watchdog),每 30 秒检查服务状态,异常时自动重启
128+
* 证书自动续签在容器内可正常工作(需确保 80 端口可访问)
129+
* 如需使用 fail2ban,可通过管理菜单安装
130+
131+
## 故障排查
132+
133+
### 容器无法启动
134+
135+
```bash
136+
docker logs xray-onekey
137+
```
138+
139+
### 服务未运行
140+
141+
```bash
142+
docker exec -it xray-onekey systemctl status xray
143+
docker exec -it xray-onekey systemctl start xray
144+
```
145+
146+
### 重新进入安装菜单
147+
148+
```bash
149+
docker exec -it xray-onekey idleleo
150+
```
151+
152+
### 完全重置
153+
154+
```bash
155+
docker compose down
156+
docker volume rm xray-conf xray-cert xray-info xray-logs acme-data
157+
docker compose up -d
158+
```

Dockerfile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
FROM debian:bookworm-slim
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
5+
RUN apt-get update && apt-get install -y --no-install-recommends \
6+
bc \
7+
curl \
8+
dbus \
9+
git \
10+
jq \
11+
lsof \
12+
python3 \
13+
qrencode \
14+
cron \
15+
gettext \
16+
socat \
17+
nmap \
18+
iputils-ping \
19+
libpcre3 \
20+
libpcre3-dev \
21+
zlib1g \
22+
zlib1g-dev \
23+
iptables \
24+
iptables-persistent \
25+
procps \
26+
psmisc \
27+
ca-certificates \
28+
gnupg \
29+
unzip \
30+
tar \
31+
gzip \
32+
vim \
33+
netcat-openbsd \
34+
sysvinit-utils \
35+
&& rm -rf /var/lib/apt/lists/*
36+
37+
RUN groupadd -f nogroup && \
38+
id nobody >/dev/null 2>&1 || useradd -g nogroup -s /usr/sbin/nologin nobody
39+
40+
RUN mkdir -p /etc/idleleo/conf/xray \
41+
/etc/idleleo/conf/nginx \
42+
/etc/idleleo/cert \
43+
/etc/idleleo/info \
44+
/etc/idleleo/logs \
45+
/etc/idleleo/tmp \
46+
/usr/local/bin \
47+
/usr/local/etc/xray \
48+
/usr/local/nginx \
49+
/var/log/xray \
50+
/var/spool/cron/crontabs
51+
52+
COPY fake-systemctl /usr/local/bin/systemctl
53+
RUN chmod +x /usr/local/bin/systemctl
54+
55+
COPY docker-entrypoint.sh /docker-entrypoint.sh
56+
RUN chmod +x /docker-entrypoint.sh
57+
58+
WORKDIR /etc/idleleo
59+
60+
COPY . /etc/idleleo/
61+
62+
RUN ln -sf /etc/idleleo/install.sh /usr/bin/idleleo && \
63+
ln -sf /etc/idleleo/conf/xray/config.json /usr/local/etc/xray/config.json && \
64+
mkdir -p /root/.acme.sh && \
65+
chmod +x /etc/idleleo/install.sh /etc/idleleo/auto_update.sh /etc/idleleo/ssl_update.sh /etc/idleleo/fail2ban_manager.sh /etc/idleleo/file_manager.sh
66+
67+
RUN echo '* soft nofile 65536' >> /etc/security/limits.conf && \
68+
echo '* hard nofile 65536' >> /etc/security/limits.conf
69+
70+
EXPOSE 443 80
71+
72+
VOLUME ["/etc/idleleo/conf", "/etc/idleleo/cert", "/etc/idleleo/info", "/var/log/xray", "/root/.acme.sh"]
73+
74+
ENTRYPOINT ["/docker-entrypoint.sh"]
75+
CMD ["idleleo"]

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343
bash <(curl -Ss https://raw.githubusercontent.com/hello-yunshu/Xray_bash_onekey/main/install.sh)
4444
```
4545

46+
## Docker 部署
47+
48+
支持使用 Docker 部署,详见 [Docker 部署指南](/DOCKER.md)
49+
50+
```bash
51+
git clone https://github.com/hello-yunshu/Xray_bash_onekey.git
52+
cd Xray_bash_onekey
53+
docker compose up -d
54+
docker attach xray-onekey
55+
```
56+
4657
## 注意事项
4758

4859
* 不了解各项设置含义时,除必填项外请使用默认值(全程回车即可)

docker-compose.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
services:
2+
xray-onekey:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
container_name: xray-onekey
7+
restart: unless-stopped
8+
network_mode: host
9+
cap_add:
10+
- NET_ADMIN
11+
environment:
12+
- TZ=Asia/Shanghai
13+
volumes:
14+
- xray-conf:/etc/idleleo/conf
15+
- xray-cert:/etc/idleleo/cert
16+
- xray-info:/etc/idleleo/info
17+
- xray-logs:/var/log/xray
18+
- acme-data:/root/.acme.sh
19+
stdin_open: true
20+
tty: true
21+
22+
volumes:
23+
xray-conf:
24+
xray-cert:
25+
xray-info:
26+
xray-logs:
27+
acme-data:

docker-entrypoint.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/bash
2+
3+
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
4+
5+
XRAY_BIN="/usr/local/bin/xray"
6+
NGINX_BIN="/usr/local/nginx/sbin/nginx"
7+
XRAY_CONF="/etc/idleleo/conf/xray/config.json"
8+
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
9+
XRAY_PID_FILE="/var/run/xray.pid"
10+
NGINX_PID_FILE="/usr/local/nginx/logs/nginx.pid"
11+
12+
_start_cron() {
13+
if ! pgrep -f "cron" >/dev/null 2>&1; then
14+
cron
15+
fi
16+
}
17+
18+
_start_services() {
19+
if [[ -f "$XRAY_CONF" ]] && [[ -x "$XRAY_BIN" ]]; then
20+
echo "[entrypoint] Starting Xray..."
21+
systemctl start xray
22+
fi
23+
24+
if [[ -f "$NGINX_CONF" ]] && [[ -x "$NGINX_BIN" ]]; then
25+
echo "[entrypoint] Starting Nginx..."
26+
systemctl start nginx
27+
fi
28+
}
29+
30+
_stop_services() {
31+
echo "[entrypoint] Stopping services..."
32+
systemctl stop nginx 2>/dev/null
33+
systemctl stop xray 2>/dev/null
34+
}
35+
36+
_watchdog() {
37+
while true; do
38+
if [[ -f "$XRAY_CONF" ]] && [[ -x "$XRAY_BIN" ]]; then
39+
if ! systemctl -q is-active xray 2>/dev/null; then
40+
echo "[watchdog] Xray is not running, restarting..."
41+
systemctl start xray
42+
fi
43+
fi
44+
45+
if [[ -f "$NGINX_CONF" ]] && [[ -x "$NGINX_BIN" ]]; then
46+
if ! systemctl -q is-active nginx 2>/dev/null; then
47+
echo "[watchdog] Nginx is not running, restarting..."
48+
systemctl start nginx
49+
fi
50+
fi
51+
52+
sleep 30
53+
done
54+
}
55+
56+
trap '_stop_services; exit 0' SIGTERM SIGINT SIGQUIT
57+
58+
_start_cron
59+
60+
case "$1" in
61+
idleleo)
62+
if [[ -f "$XRAY_CONF" ]] && [[ -x "$XRAY_BIN" ]]; then
63+
echo "[entrypoint] Detected existing Xray configuration, starting services..."
64+
_start_services
65+
echo "[entrypoint] Services started. Launching management script..."
66+
echo "[entrypoint] Type 'exit' to return to daemon mode."
67+
bash /etc/idleleo/install.sh
68+
echo "[entrypoint] Management script exited. Entering daemon mode..."
69+
_watchdog
70+
else
71+
echo "[entrypoint] No existing configuration found. Launching install script..."
72+
bash /etc/idleleo/install.sh
73+
if [[ -f "$XRAY_CONF" ]]; then
74+
echo "[entrypoint] Installation complete. Starting services..."
75+
_start_services
76+
echo "[entrypoint] Services started. Entering daemon mode..."
77+
_watchdog
78+
else
79+
echo "[entrypoint] No configuration found. Entering daemon mode (services not started)..."
80+
_watchdog
81+
fi
82+
fi
83+
;;
84+
start)
85+
_start_services
86+
echo "[entrypoint] Services started. Entering daemon mode..."
87+
_watchdog
88+
;;
89+
shell|bash)
90+
_start_services
91+
echo "[entrypoint] Services started. Opening shell..."
92+
/bin/bash
93+
echo "[entrypoint] Shell exited. Entering daemon mode..."
94+
_watchdog
95+
;;
96+
*)
97+
_start_services
98+
echo "[entrypoint] Services started. Executing: $*"
99+
exec "$@"
100+
;;
101+
esac

0 commit comments

Comments
 (0)