> For the complete documentation index, see [llms.txt](https://docs.ipcheck.ing/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipcheck.ing/developer/zh/getting-started/deploy-with-docker.md).

# 使用 Docker 部署

Docker 是自托管 MyIP 的推荐方式。一个镜像包含应用的两部分：已构建的 Vue 前端和 Express 后端。

## 镜像

| 仓库              | 镜像                               |
| --------------- | -------------------------------- |
| Docker Hub      | `jason5ng32/myip:latest`         |
| GitHub Packages | `ghcr.io/jason5ng32/myip:latest` |

两者都基于同一发布版本构建，并发布到 `linux/amd64` 和 `linux/arm64`。每个发布版本还会附带一个版本标签，以及 `:latest` —— 如果你想要可复现的升级，请固定其中一个。

## 运行它

{% tabs %}
{% tab title="docker run" %}
{% code title="docker run" %}

```bash
docker run -d \\
  -p 18966:18966 \\
  -e MAXMIND_ACCOUNT_ID="your-account-id" \\
  -e MAXMIND_LICENSE_KEY="your-license-key" \\
  -e MAXMIND_AUTO_UPDATE="true" \\
  -e ALLOWED_DOMAINS="myip.example.com" \
  -e CAIDA_AUTO_UPDATE="true" \
  -e GOOGLE_MAP_API_KEY="你的密钥" \
  -e IPINFO_API_KEY="你的密钥" \
  -e SECURITY_RATE_LIMIT="500" \
  -e LOG_LEVEL="info" \
  --name myip \\
  --restart always \\
  jason5ng32/myip:latest
```

{% endcode %}
{% endtab %}

{% tab title="Docker Compose" %}
{% code title="compose.yaml" %}

```yaml
services:
  myip:
    image: jason5ng32/myip:latest
    container_name: myip
    restart: always
    ports:
      - "18966:18966"
    environment:
      # 必需 — 参见 MaxMind 设置
      MAXMIND_ACCOUNT_ID: "你的账号 ID"
      MAXMIND_LICENSE_KEY: "你的许可证密钥"
      MAXMIND_AUTO_UPDATE: "true"
      # 当你将 MyIP 通过域名对外提供时必需
      ALLOWED_DOMAINS: "myip.example.com"
      # 可选
      CAIDA_AUTO_UPDATE: "true"
      GOOGLE_MAP_API_KEY: "你的密钥"
      IPINFO_API_KEY: "你的密钥"
      SECURITY_RATE_LIMIT: "500"
      LOG_LEVEL: "info"
```

{% endcode %}

使用以下命令启动： `docker compose up -d`.
{% endtab %}
{% endtabs %}

只需要 `MAXMIND_ACCOUNT_ID`, `MAXMIND_LICENSE_KEY` 和 `MAXMIND_AUTO_UPDATE` 即可获得一个可工作的实例。上面其他内容都可选——完整列表见 [环境变量](/developer/zh/reference/environment-variables.md).

{% hint style="info" %}
比起直接写入内联密钥，更推荐使用 env 文件：把变量放在 `.env` 到你的 compose 文件旁边，Compose 会自动读取；或者传入 `--env-file .env` 到 `docker run`.
{% endhint %}

## 端口

容器运行两个 Node 进程：

| 进程                             | 端口                        | 已公开？ |
| ------------------------------ | ------------------------- | ---- |
| 前端 — 提供已构建的 SPA 并代理 `/api` 到后端 | `18966` (`FRONTEND_PORT`) | 是    |
| 后端 — Express API               | `11966` (`BACKEND_PORT`)  | 没有   |

前端代理 `/api` 到 `http://localhost:<BACKEND_PORT>` **在容器内**，因此你不会公开后端端口。仅 `18966` 由镜像暴露。

要在不同的主机端口上提供 MyIP，请更改映射左侧：

```bash
-p 8080:18966
```

如果你更改 `FRONTEND_PORT`，就把映射右侧也改成相应值。

## `VITE_*` 变量在预构建镜像上不起作用

名称以 `VITE_` 开头的变量会被 Vite 在 **构建时** 读取并写入 JavaScript bundle。发布的镜像是在 CI 中构建的，不带 `.env` 文件，因此在运行时通过 `-e` 传入，对前端没有效果。

这适用于 `VITE_CURL_IPV4_DOMAIN`, `VITE_CURL_IPV6_DOMAIN`, `VITE_CURL_IPV64_DOMAIN`, `VITE_GOOGLE_ANALYTICS_ID`, `VITE_SITE_URL` 和 `VITE_SENTRY_DSN_FRONTEND`。要设置它们，你需要自行构建镜像（或使用 [使用 Node.js 部署](/developer/zh/getting-started/deploy-with-nodejs.md)):

```bash
git clone https://github.com/jason5ng32/MyIP.git
cd MyIP
cp .env.example .env   # 填入 VITE_* 值
docker build -t myip-custom .
```

{% hint style="info" %}
`VITE_SENTRY_DSN_FRONTEND` 是个例外：后端也会读取它 **在运行时** 以决定是否挂载 `/api/monitoring` 隧道。如果你在构建时把它写入镜像，那么也要在运行时向容器传入相同的值。参见 [错误监控](/developer/zh/configuration/error-monitoring.md).
{% endhint %}

## 数据存放位置

MyIP 会在运行时下载一些数据集。它们位于 **在容器内**，位于 `/app/common/`:

| 路径                        | 内容                                                     |
| ------------------------- | ------------------------------------------------------ |
| `/app/common/maxmind-db/` | `GeoLite2-City.mmdb`, `GeoLite2-ASN.mmdb`，以及更新器的状态和锁文件 |
| `/app/common/as-org-db/`  | CAIDA `as-org2info.txt` — ASN 组织名称                     |
| `/app/common/as-rel-db/`  | CAIDA `as-rel2.txt` — ASN 连接图                          |

这些都不会被打包进镜像（MaxMind 的 GeoLite2 许可证禁止再分发），而且它们都不是珍贵资源——每次全新启动都会重新下载。重新创建容器只是意味着 MyIP 会在首次启动时再次获取它们。

如果你不想在每次升级时都重新下载，请挂载卷：

{% code title="compose.yaml（节选）" %}

```yaml
    volumes:
      - myip-maxmind:/app/common/maxmind-db
      - myip-asorg:/app/common/as-org-db
      - myip-asrel:/app/common/as-rel-db

volumes:
  myip-maxmind:
  myip-asorg:
  myip-asrel:
```

{% endcode %}

## 升级

{% tabs %}
{% tab title="docker run" %}

```bash
docker pull jason5ng32/myip:latest
docker stop myip && docker rm myip
# 然后重新运行你原来的 `docker run` 命令
```

{% endtab %}

{% tab title="Docker Compose" %}

```bash
docker compose pull
docker compose up -d
```

{% endtab %}
{% endtabs %}

在未挂载卷的情况下升级后，首次启动会重新下载 GeoLite2 和 CAIDA 数据集。这需要一点时间——后端会等待 MaxMind 加载完成后才开始监听（最长 5 分钟）。

## 检查状态

```bash
docker logs -f myip
```

正常启动会显示后端和前端监听，以及 MaxMind 加载：

```
🚀 后端服务器已就绪，地址为 http://localhost:11966
🚀 静态文件服务器已就绪，地址为 http://localhost:18966
📦 MaxMind 数据库已加载（启动）
```

如果你看到 `❌ 在数据库成功加载之前，MaxMind API 会返回 503`，请前往 [MaxMind 设置](/developer/zh/getting-started/maxmind-setup.md#troubleshooting).

## 下一步

* [MaxMind 设置](/developer/zh/getting-started/maxmind-setup.md) — 所需凭据以及自动更新器的工作方式
* [反向代理与域名](/developer/zh/getting-started/reverse-proxy-and-domains.md) — TLS、Nginx/Caddy、 `ALLOWED_DOMAINS`
* [安全选项](/developer/zh/configuration/security-options.md) — 限流和滥用防护
* [日志](/developer/zh/configuration/logging.md) — 日志级别、JSON 输出、HTTP 请求日志


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.ipcheck.ing/developer/zh/getting-started/deploy-with-docker.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
