> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 DigitalOcean 上部署 Bun 应用

[DigitalOcean](https://www.digitalocean.com/) 是一个云平台，提供一系列构建和部署应用程序的服务。

本指南使用 `Dockerfile` 将一个 Bun HTTP 服务器部署到 DigitalOcean。

<Note>
  在继续之前，请确保你已准备：

  * 一个可供部署的 Bun 应用
  * 一个 [DigitalOcean 账户](https://www.digitalocean.com/)
  * 已安装并配置好 [DigitalOcean CLI](https://docs.digitalocean.com/reference/doctl/how-to/install/#step-1-install-doctl)
  * 已安装 [Docker](https://docs.docker.com/get-started/get-docker/) 并将其添加到你的 `PATH` 中
</Note>

***

<Steps>
  <Step title="创建新的 DigitalOcean 容器仓库">
    创建一个新的容器仓库，用于存储 Docker 镜像。

    <Tabs>
      <Tab title="通过 DigitalOcean 仪表盘">
        在 DigitalOcean 仪表盘中，前往 [**容器仓库**](https://cloud.digitalocean.com/registry)，并输入新仓库的详细信息。

        <Frame>
          <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/guides/digitalocean-7.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=01d1594b88c4bb43e0d175ac79b1287d" alt="DigitalOcean 仓库仪表盘" width="5552" height="2856" data-path="images/guides/digitalocean-7.png" />
        </Frame>

        确保详细信息正确，然后点击 **创建仓库**。
      </Tab>

      <Tab title="通过 DigitalOcean CLI">
        ```bash terminal icon="terminal" theme={null}
        doctl registry create bun-digitalocean-demo
        ```

        ```txt theme={null}
        Name                     Endpoint                                           Region slug
        bun-digitalocean-demo    registry.digitalocean.com/bun-digitalocean-demo    sfo2
        ```
      </Tab>
    </Tabs>

    你应该可以在 [**DigitalOcean 仓库仪表盘**](https://cloud.digitalocean.com/registry) 中看到新仓库：

    <Frame>
      <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/guides/digitalocean-1.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=f2b4dd92806629785bc207833823bdd0" alt="DigitalOcean 仓库仪表盘" width="2636" height="1414" data-path="images/guides/digitalocean-1.png" />
    </Frame>
  </Step>

  <Step title="创建新的 Dockerfile">
    在项目的根目录下创建一个新的 `Dockerfile`。该文件包含初始化容器、将本地项目文件复制到其中、安装依赖和启动应用程序的指令。

    ```docker Dockerfile icon="docker" theme={null}
    # 使用官方 Bun 镜像来运行应用程序
    FROM oven/bun:debian

    # 设置工作目录为 `/app`
    WORKDIR /app

    # 将 package.json 和 bun.lock 复制到容器中
    COPY package.json bun.lock ./

    # 安装依赖
    RUN bun install --production --frozen-lockfile

    # 将应用程序的其余部分复制到容器中
    COPY . .

    # 暴露端口（DigitalOcean 会设置 PORT 环境变量）
    EXPOSE 8080

    # 运行应用程序
    CMD ["bun", "index.ts"]
    ```

    <Note>
      确保启动命令与你的应用程序入口点对应。如果你的 `package.json` 中有 start 脚本，也可以是 `CMD ["bun", "run", "start"]`。

      如果你的应用没有依赖项，可以省略 `RUN bun install --production --frozen-lockfile` 这一行。
    </Note>

    在项目的根目录下创建一个新的 `.dockerignore` 文件。它列出了要从容器镜像中\_排除\_的文件和目录，例如 `node_modules`，这有助于使构建更快、更小：

    ```docker .dockerignore icon="Docker" theme={null}
    node_modules
    Dockerfile*
    .dockerignore
    .git
    .gitignore
    README.md
    LICENSE
    .vscode
    .env
    # 任何其他你想要排除的文件或目录
    ```
  </Step>

  <Step title="将 Docker 与 DigitalOcean 仓库进行身份验证">
    在构建和推送 Docker 镜像之前，先将 Docker 与 DigitalOcean 容器仓库进行身份验证：

    ```bash terminal icon="terminal" theme={null}
    doctl registry login
    ```

    ```txt theme={null}
    已成功通过 registry.digitalocean.com 进行身份验证
    ```

    <Note>
      此命令使用你的 DigitalOcean 凭证将 Docker 与 DigitalOcean 的仓库进行身份验证。如果没有此步骤，构建和推送命令将失败并返回 401 认证错误。
    </Note>
  </Step>

  <Step title="构建 Docker 镜像并推送到 DigitalOcean 仓库">
    确保你在包含 `Dockerfile` 的目录中，然后通过一个命令构建 Docker 镜像并将其推送到 DigitalOcean 仓库：

    ```bash terminal icon="terminal" theme={null}
    docker buildx build --platform=linux/amd64 -t registry.digitalocean.com/bun-digitalocean-demo/bun-digitalocean-demo:latest --push .
    ```

    <Note>
      如果你在 ARM Mac (M1/M2) 上构建，必须使用 `docker buildx` 和 `--platform=linux/amd64` 以确保与 DigitalOcean 的基础设施兼容。如果使用 `docker build` 而不带平台标志，将创建一个 ARM64 镜像，该镜像无法在 DigitalOcean 上运行。
    </Note>

    镜像推送完成后，你应该可以在 [**DigitalOcean 仓库仪表盘**](https://cloud.digitalocean.com/registry) 中看到它：

    <Frame>
      <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/guides/digitalocean-2.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=66272c01c6d219662ad82cf670ee28de" alt="DigitalOcean 仓库仪表盘" width="2636" height="1414" data-path="images/guides/digitalocean-2.png" />
    </Frame>
  </Step>

  <Step title="创建新的 DigitalOcean App Platform 项目">
    在 DigitalOcean 仪表盘中，前往 [**App Platform**](https://cloud.digitalocean.com/apps) > **创建应用**。你可以直接从容器镜像创建项目。

    <Frame>
      <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/guides/digitalocean-3.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=ef5385cd1fb32efd12b6e029d0660228" alt="DigitalOcean App Platform 项目仪表盘" width="5272" height="2828" data-path="images/guides/digitalocean-3.png" />
    </Frame>

    确保详细信息正确，然后点击 **下一步**。

    <Frame>
      <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/guides/digitalocean-4.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=6f36a5cc4e599df4fee634359d6f3f0e" alt="DigitalOcean App Platform 服务仪表盘" width="5272" height="2828" data-path="images/guides/digitalocean-4.png" />
    </Frame>

    查看并配置资源设置，然后点击 **创建应用**。

    <Frame>
      <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/guides/digitalocean-6.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=23d083de7a35a63cfbc62644835fc5ee" alt="DigitalOcean App Platform 服务仪表盘" width="5036" height="2688" data-path="images/guides/digitalocean-6.png" />
    </Frame>
  </Step>

  <Step title="访问你的在线应用">
    你的应用现已上线。应用创建完成后，你应该可以在 App Platform 仪表盘中看到它及其公共 URL。

    <Frame>
      <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/guides/digitalocean-5.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=30ef4201edd1686c1040db5891874013" alt="DigitalOcean App Platform 应用仪表盘" width="5036" height="2688" data-path="images/guides/digitalocean-5.png" />
    </Frame>
  </Step>
</Steps>
