---
title: "在 Docker 中运行（社区方案） | Wantastic Docs"
description: "在基于官方发布版本构建的 Docker 容器中运行 wantasticd：构建一个小镜像，用 genkey 认领一次，然后以后台方式运行。这不是官方镜像。"
url: "https://wantastic.app/zh/docs/install-docker"
category: "Install wantasticd"
reading_time: "5 分钟"
---

# 在 Docker 中运行（社区方案）

在基于官方发布版本构建的 Docker 容器中运行 wantasticd：构建一个小镜像，用 genkey 认领一次，然后以后台方式运行。这不是官方镜像。

> **Outcome:** 你自行构建的容器运行 wantasticd，将认领信息保存在命名卷中，自动重启，并在控制台中显示为 Online。

## Before you start

- 带有 TUN 驱动的 Linux 主机上的 Docker Engine 20.10+（或 Podman）。
- 有权限使用 --cap-add NET_ADMIN 和 --device /dev/net/tun 运行容器。
- 一部手机或一个浏览器，用于登录 Wantastic 控制台。

## 快速安装（5 分钟）

没有官方镜像。你需要基于官方发布版本构建一个小镜像，认领一次，然后在后台运行。无需令牌。

1. 将下方的 Dockerfile 保存到一个空目录中。将 WANTASTIC_ARCH 设置为主机的 CPU 类型（amd64、arm64 或 arm）。
2. 构建镜像，并用认领命令运行一次 genkey。它会打印 Claim URL 和二维码。
3. 扫描二维码或打开 Claim URL，登录后点击 Claim Device。
4. 打印出 "Device claimed" 后，按 Ctrl+C。认领信息已保存在 wantastic 卷中。
5. 使用运行命令在后台启动容器。

Dockerfile:

```dockerfile
FROM alpine:3.20
ARG WANTASTIC_ARCH=amd64
RUN apk add --no-cache ca-certificates curl \
 && TAG=$(curl -fsSL https://get.wantastic.app/latest | cut -d+ -f1) \
 && mkdir -p /tmp/w \
 && curl -fsSL https://get.wantastic.app/$TAG/wantasticd-linux-$WANTASTIC_ARCH.tar.gz | tar -xz -C /tmp/w \
 && install -m 755 "$(find /tmp/w -name wantasticd -type f | head -n 1)" /usr/local/bin/wantasticd \
 && rm -rf /tmp/w \
 && wantasticd version
ENV WANTASTIC_CONFIG_DIR=/etc/wantastic \
    WANTASTIC_DNS_MODE=off
VOLUME /etc/wantastic
ENTRYPOINT ["wantasticd"]
CMD ["connect", "--config", "/etc/wantastic/config.conf", "--no-auto-update"]
```

构建并认领（一次）:

```bash
docker build -t wantasticd-local --build-arg WANTASTIC_ARCH=amd64 .
docker run -it --rm --hostname office-docker --cap-add NET_ADMIN --device /dev/net/tun -v wantastic:/etc/wantastic wantasticd-local genkey
```

在后台运行:

```bash
docker run -d --name wantasticd --hostname office-docker --restart unless-stopped --cap-add NET_ADMIN --device /dev/net/tun -v wantastic:/etc/wantastic wantasticd-local
```

*安装并认领: genkey 打印 Claim URL 和二维码后进入等待。你在控制台中点击 Claim Device 后，它会打印 "Device claimed"。然后按 Ctrl+C 并重启服务。*

```
$ $ docker run -it --rm --hostname office-docker --cap-add NET_ADMIN --device /dev/net/tun -v wantastic:/etc/wantastic wantasticd-local genkey
…
…
Generated device claim key: /etc/wantastic/device-claim-key.json
Public key: Vd3hK1oQ7mR2sT9uW4xY6zA8bC0dE2fG4hJ6kL8mN0P=
Server URL: https://console.wantastic.app
Claim URL: https://console.wantastic.app/#desktop?claim_public_key=Vd3hK1oQ…%3D&wantastic_server=https%3A%2F%2Fconsole.wantastic.app

█▀▀▀▀▀█ ▄▀▄ ▀█▄ █▀▀▀▀▀█
█ ███ █ ▀█▄▀▄█ █ ███ █
█ ▀▀▀ █ ▄▀█ ▀▄ █ ▀▀▀ █
▀▀▀▀▀▀▀ ▀ ▀ █ ▀ ▀▀▀▀▀▀▀
▀█▄▀▄▀▀█▀▄█▀▄▀▀▄█▀▄▀█▄
█▀▀▀▀▀█ █▄▀▄▀█ ▀▄█ ▄▀
▀▀▀▀▀▀▀ ▀▀ ▀▀▀ ▀▀▀ ▀▀▀

2026/10/29 09:14:03 Waiting for claim. Press Ctrl+C to stop.
2026/10/29 09:15:12 Device claimed. Configuration saved to /etc/wantastic/config.conf
^C
$ $ docker run -d --name wantasticd --hostname office-docker --restart unless-stopped --cap-add NET_ADMIN --device /dev/net/tun -v wantastic:/etc/wantastic wantasticd-local
```

*Console: Claim Wantastic Device。Claim URL 和二维码会自动填入密钥，你只需点击 Claim Device。*

1. Claim public key 已自动填写
2. Wantastic server 保持默认即可
3. 点击 Claim Device

> **Operational note:** WANTASTIC_DNS_MODE=off 可防止代理修改由 Docker 管理的容器 DNS。容器中的自动更新处于关闭状态：如需更新，请重新构建镜像。

## 检查是否正常工作

1. 运行状态命令。它会显示 Status: Connected。
2. 在控制台中打开 Devices。设备显示为 Online。

检查:

```bash
docker exec wantasticd wantasticd status
docker logs --tail 20 wantasticd
```

*wantasticd status: Status: Connected 加上 VPN IP 表示隧道已建立。*

```
$ $ docker exec wantasticd wantasticd status
Status:        Connected
Version:       v1.0.54+377f3c0
Mode:          TUN (wantastic0)
VPN IPs:       10.200.0.14/32
Public key:    q7ZkV3m1yH0dTn8pXo2bLwR4sE6uJ9cA1fG5hK8iN0M=
Server:        wg.wantastic.app:51820
Exit node:     false
Local access:  false
Auto update:   true
Traffic:       ↓ 18.4 KiB  ↑ 9.2 KiB
Peers:         2 (direct 1, relay 1)
Uptime:        4m12s
```

*Console: Devices 窗口：新设备以其名称出现，并在几秒钟内变为 Online。*

1. 按名称搜索
2. 状态显示 Online
3. 覆盖网络 IP 与 wantasticd status 一致
4. Last seen 持续更新

## Docker Compose

- 默认网络：只有容器位于覆盖网络中。
- network_mode: host 会将 wantastic0 放到主机上，这样主机本身也可以通过覆盖网络访问。

compose.yaml:

```yaml
services:
  wantasticd:
    build:
      context: .
      args:
        WANTASTIC_ARCH: amd64
    image: wantasticd-local
    hostname: office-docker
    restart: unless-stopped
    cap_add:
      - NET_ADMIN
    devices:
      - /dev/net/tun:/dev/net/tun
    volumes:
      - wantastic:/etc/wantastic
    # network_mode: host   # put wantastic0 on the host itself
volumes:
  wantastic: {}
```

认领一次，然后启动:

```bash
docker compose run --rm wantasticd genkey
# after "Device claimed": press Ctrl+C, then:
docker compose up -d
```

## 更新

使用最新发布版本重新构建:

```bash
docker build --no-cache -t wantasticd-local --build-arg WANTASTIC_ARCH=amd64 .
docker rm -f wantasticd
# then run the "Run in the background" command again (the volume keeps the claim)
```

## 其他安装方式

仅在自动化或特殊情况下才需要这些方式。上面的快速安装是最简单的方法。

### 使用令牌（多个容器）

使用 Account → Secrets 中的令牌向卷中登录一次，然后使用运行命令。

使用令牌登录到卷:

```bash
docker run --rm --hostname office-docker -v wantastic:/etc/wantastic wantasticd-local login --no-connect --token <TOKEN>
```

> 像对待密码一样对待令牌。在其过期之前，任何持有它的人都能向你的账户添加设备。

*Console: Account → Secrets → + New Secret。密钥的值就是令牌。*

1. 打开 Secrets 选项卡
2. 点击 + New Secret
3. 为其命名
4. 点击 Generate
5. 复制新密钥

## Verify the result

- docker ps 显示 wantasticd 为 Up（而不是 Restarting）。
- docker exec wantasticd wantasticd status 显示 Status: Connected。
- 设备在 Devices 中显示为 Online。

## Common issues

### 容器因 TUN 错误立即停止。

```
Failed to start agent: create device: create tun: CreateTUN("wantastic0") failed; /dev/net/tun does not exist
```

- **Where:** docker logs
- **Likely cause:** 启动容器时没有使用 --device /dev/net/tun，或主机没有 TUN 驱动。
- **Fix:** 在主机上加载 TUN，然后使用快速安装中的确切运行命令。

在主机上:

```bash
sudo modprobe tun
```

### 容器因权限错误而停止。

```
operation not permitted
```

- **Where:** docker logs
- **Likely cause:** 缺少 NET_ADMIN 能力，或运行时为 rootless 模式。
- **Fix:** 添加 --cap-add NET_ADMIN。使用 rootless Docker 或 Podman 时，请以 root 身份运行容器。

### 容器启动后立即循环重启。

```
Config not found at /etc/wantastic/config.conf. Run `wantasticd genkey --out <key>` to create a claim key and wait for claim, or pass --wait-claim with an existing key.
```

- **Where:** docker logs
- **Likely cause:** 容器从未被认领，或使用了不同的卷。
- **Fix:** 使用相同的卷名运行一次认领命令，然后再次启动容器。

认领:

```bash
docker run -it --rm --hostname office-docker --cap-add NET_ADMIN --device /dev/net/tun -v wantastic:/etc/wantastic wantasticd-local genkey
docker restart wantasticd
```

### 在容器内运行 install.sh 时在最后失败。

```
System has not been booted with systemd as init system (PID 1). Can't operate.
```

- **Where:** install.sh
- **Likely cause:** 容器通常没有 systemd。
- **Fix:** 请使用本页的 Dockerfile，而不是安装程序。

### 日志每小时都显示更新失败。

```
Error: no supported service manager found; refusing an unverifiable update
```

- **Where:** docker logs
- **Likely cause:** 自动更新已开启，但容器没有服务管理器。
- **Fix:** 在 CMD 中保留 --no-auto-update（与 Dockerfile 中相同），并通过重新构建镜像来更新。

### 认领或登录被拒绝。

```
Login failed: account has reached its device limit — upgrade your plan or remove an existing device
```

- **Where:** wantasticd login / 控制台
- **Likely cause:** 你的套餐设备数量已满。控制台显示 "You have reached the maximum number of devices for your plan."
- **Fix:** 在 Devices 中删除一台不再使用的设备（设备行 → Delete），或升级套餐。然后重新认领。

### status 显示 Running (tunnel down)，或 peers 显示 (server) 为 HANDSHAKE never。

```
Device health check failed: …
```

- **Where:** 服务日志
- **Likely cause:** 网络阻止了到中心节点（wg.wantastic.app）的出站 UDP 51820 端口，或 DNS 不可用。
- **Fix:** 在防火墙中放行出站 UDP 51820，然后重启服务。

测试 DNS 并重启:

```bash
nslookup wg.wantastic.app
docker restart wantasticd
```

- **Verify:** wantasticd peers 显示 (server) 有最近的握手（例如 "8s ago"）。

## Continue with

- [Manual tarball install](https://wantastic.app/zh/docs/install-manual): Install wantasticd by hand from the release archive on any Linux CPU (including ppc64, s390x and loong64), claim it with genkey, and add your own service.
- [Install on Debian and Ubuntu](https://wantastic.app/zh/docs/install-debian-ubuntu): Install wantasticd on Debian, Ubuntu, Mint and Proxmox in 2 minutes: one command, scan a QR code, click Claim Device.
- [wantasticd CLI reference and operations](https://wantastic.app/zh/docs/wantasticd): The most used wantasticd commands first, then every command and flag, environment variables, config and state paths per OS, log lines, local access, exit node, and the claim flow (genkey / --wait-claim).
- [Self-host WantasticCore](https://wantastic.app/zh/docs/self-hosting): Run your own Wantastic console with one command: install, setup wizard, DNS, ports, devices and backups.
- [Connect and runtime errors](https://wantastic.app/zh/docs/errors-connect-runtime): Fix service start failures, config errors, TUN problems, missing handshakes and DNS warnings.
- [Update, roll back and uninstall](https://wantastic.app/zh/docs/update-and-uninstall): Update or uninstall wantasticd with one line per OS. Also: how auto-update works, turning it off, rolling back to the previous binary, and purging all data.
