【问题标题】:How do I connect to MongoDB, running in Github codespaces, using MongoDB Compass?如何使用 MongoDB Compass 连接到在 Github 代码空间中运行的 MongoDB?
【发布时间】:2022-01-14 16:23:30
【问题描述】:

我正在试用 Github 代码空间,特别是“Node.js 和 Mongo DB”默认设置。

端口被转发,我的目标是连接本地机器上运行的 MongoDB Compass。

转发到27017的地址类似于https://<long-address>.githubpreview.dev/

我的尝试

我尝试使用以下连接字符串,但它在 MongoDB 指南针中不起作用。它以No addresses found at host 失败。我实际上不确定如何确定 MongoDB 是否真的在 Github 代码空间中运行?

mongodb+srv://root:example@<long-address>.githubpreview.dev/

.devcontainer 文件


docker-compose.yml

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        # Update 'VARIANT' to pick an LTS version of Node.js: 16, 14, 12.
        # Append -bullseye or -buster to pin to an OS version.
        # Use -bullseye variants on local arm64/Apple Silicon.
        VARIANT: "16"
    volumes:
      - ..:/workspace:cached
    init: true

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity

    # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
    network_mode: service:db
    # Uncomment the next line to use a non-root user for all processes.
    # user: node

    # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. 
    # (Adding the "ports" property to this file will not forward from a Codespace.)

  db:
    image: mongo:latest
    restart: unless-stopped
    volumes:
      - mongodb-data:/data/db
    # Uncomment to change startup options
    environment:
     MONGO_INITDB_ROOT_USERNAME: root
     MONGO_INITDB_ROOT_PASSWORD: example
     MONGO_INITDB_DATABASE: foo

    # Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
    # (Adding the "ports" property to this file will not forward from a Codespace.)

volumes:
  mongodb-data: null

还有一个devcontainer.json 文件

// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.203.0/containers/javascript-node-mongo
// Update the VARIANT arg in docker-compose.yml to pick a Node.js version
{
    "name": "Node.js & Mongo DB",
    "dockerComposeFile": "docker-compose.yml",
    "service": "app",
    "workspaceFolder": "/workspace",

    // Set *default* container specific settings.json values on container create.
    "settings": {},

    // Add the IDs of extensions you want installed when the container is created.
    "extensions": [
        "dbaeumer.vscode-eslint",
        "mongodb.mongodb-vscode" 
    ],

    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    "forwardPorts": [3000, 27017],

    // Use 'postCreateCommand' to run commands after the container is created.
    // "postCreateCommand": "yarn install",

    // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
    "remoteUser": "node",
    "features": {
        "git": "os-provided"
    }
}

最后是一个 Docker 文件:

# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 16, 14, 12, 16-bullseye, 14-bullseye, 12-bullseye, 16-buster, 14-buster, 12-buster
ARG VARIANT=16-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}

# Install MongoDB command line tools if on buster and x86_64 (arm64 not supported)
ARG MONGO_TOOLS_VERSION=5.0
RUN . /etc/os-release \
    && if [ "${VERSION_CODENAME}" = "buster" ] && [ "$(dpkg --print-architecture)" = "amd64" ]; then \
        curl -sSL "https://www.mongodb.org/static/pgp/server-${MONGO_TOOLS_VERSION}.asc" | gpg --dearmor > /usr/share/keyrings/mongodb-archive-keyring.gpg \
        && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] http://repo.mongodb.org/apt/debian $(lsb_release -cs)/mongodb-org/${MONGO_TOOLS_VERSION} main" | tee /etc/apt/sources.list.d/mongodb-org-${MONGO_TOOLS_VERSION}.list \
        && apt-get update && export DEBIAN_FRONTEND=noninteractive \
        && apt-get install -y mongodb-database-tools mongodb-mongosh \
        && apt-get clean -y && rm -rf /var/lib/apt/lists/*; \
    fi

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"

# [Optional] Uncomment if you want to install more global node modules
# RUN su node -c "npm install -g <your-package-list-here>"

更新 我还在 MongoDB 社区发布了here,但没有任何帮助...

【问题讨论】:

  • 您能否通过 SSH 连接到 Codespaces 并检查 MongoDB 状态?还想知道您是否尝试过使用 SSH 隧道(端口转发)建立连接,然后通过 MongoDB Compass 连接到 MongoDB。 stackoverflow.com/a/69848342/7102312

标签: mongodb github github-codespaces


【解决方案1】:

正如@iravinandan 所说,您需要建立一个隧道。

单独发布端口无济于事,因为所有传入请求都通过 http 代理。

如果您dig CNAME &lt;long-address&gt;.githubpreview.dev,您将看到它是 github-codespaces.app.online.visualstudio.com。你可以在 githubpreview.dev 子域中放任何东西,它仍然会在 DNS 级别解析。

代理依赖 HTTP Host 标头将请求路由到正确的上游,因此它仅适用于 HTTP 协议。

要使用任何其他协议(在您的情况下为 MongoDb wire protocol),您需要设置一个 TCP 隧道代码空间到您的机器。

最简单的设置 - 直接连接

在编写默认 Node + Mongo 代码空间时使用 Debian buster,因此ssh port forwarding 将是显而易见的选择。在代码空间/VSCode 终端中:

ssh -R 27017:localhost:27017 your_public_ip

然后在您的指南针中连接到

mongodb://localhost:27017

这当然需要您的本地计算机运行 sshd,有一个白色 IP(或者至少您的路由器应该将 传入 ssh 流量转发到您的计算机)并允许它进入防火墙。如果 27017 已在本地使用,您可以选择任何端口。

这是最简单的设置,但它会将您的笔记本电脑暴露在互联网上,并且被感染只是时间问题。

更安全一点 - 中间跳箱

为了让您的本地系统保持在 DMZ 之后,您可以改为设置一个 jumpbox - 互联网某处的简约一次性 linux 盒子,它将用于链接 2 个隧道:

  • 从代码空间到跳转框的远程端口转发
  • 从您的笔记本电脑到 Jumpbox 的本地端口转发

一样

mongodb://localhost:27017

在 mongo compas 上。

jumpbox 必须将 sshd 暴露在互联网上,但您可以通过加强其安全性将风险降至最低。毕竟它除了代理流量什么都不做。 EC2 nano 绰绰有余,但请记住,大数据传输可能会很昂贵。

无忧的隧道即服务

你可以在 5 分钟内尝试的东西。 ngrok 已经存在十多年了,它正是这样做的——它出售隧道(有一些免费层足以用于演示)。

在您的代码空间/VScode 终端中:

npm i ngrok --save-dev

为了避免每次都安装,但请确保您不附带生产代码。 您需要在 ngrok 上注册一个帐户(使用 github 进行 SSO 即可)以获取验证码并将其传递给 codespaces/VSCode 终端:

./node_modules/.bin/ngrok authtoken <the token>

请记住,它将令牌保存到主目录,重建后将被擦除。授权后,您可以在代码空间/VSCode 终端中打开隧道:

./node_modules/.bin/ngrok tcp 27017

Codespace 会自动转发端口:

终端会显示一些统计信息(注意免费层限制)和连接字符串:

每次打开隧道时,子域和端口都会改变。 从上图中,mongodb compas 的连接参数将是:

mongodb://0.tcp.ngrok.io:18862

根据需要在 mongodb 级别使用授权参数。

同样,请记住,您将 mongodb 暴露在互联网上 (0.tcp.ngrok.io:18862),并且 mongo 接受未经身份验证的连接。

我不会让它打开超过必要的时间。

使用内置的mongodb客户端

node + mongo 环境预装了方便的 VScode 插件:

当然,它缺少许多 compas 分析工具,但它开箱即用,足以开发

只需打开插件并连接到本地主机:

指南针DIY

在不影响安全性的情况下获得指南针功能并实现零配置目标的最佳选择是自己托管指南针。它是一个电子应用程序,可以在 Mongodb Atlas 的浏览器中完美运行。

源代码位于https://github.com/mongodb-js/compass。 通过一些努力,您可以制作一个 docker 镜像来托管 compass,将此镜像包含到 docker-compose 中,然后在 devcontainer.json 中转发端口

Github 代码空间将负责身份验证(将转发的端口保密,以便只有空间的所有者才能访问它)。从桌面到 compass 的所有通信都将通过 https,而 compass 到 mongodb 将在 docker 网络本地。安全方面,它将与 VSCode mongodb 插件相提并论

【讨论】:

  • 有史以来最好的答案 - 非常感谢。 Ngrok 工作,我也在探索 MongoDB 扩展。您有什么想法可以改进 Ngrok 方法,以便每个开发人员都有自己的帐户 - 无需设置令牌的麻烦?我对 Github 代码空间的目标是零(!)配置让开发人员环境正常工作?
  • 嗯,这有点不同,我已经更新了答案来解决这个目标。使用 ngrok,身份验证代码是恒定的,因此您可以将其存储在某个地方,甚至可以使用 npm liefcycle 脚本将其自动化。它不会给你零配置。 ngrok 的子域和端口是动态的(无论如何都在免费层) - 每次打开隧道时都需要在 compass 上重新配置连接。让我再次强调一下——更大的问题是将数据库暴露在互联网上。请确保您了解风险。
  • 你添加的最后一个想法太棒了——我会努力实现这一点。这正是我梦寐以求的 ? 你是否偶然知道是否有人已经构建了这样的 Docker 映像 - 一些灵感真的很棒☺️
  • 我在 docker hub 上找不到任何东西,但是现在有很多其他的存储库,很容易错过它。自从两年前 Compass 是付费应用程序以来,很有可能什么都没有。实际上,请检查许可证。他们只有在切换到新的 SSPL 许可证后才打开代码。
猜你喜欢
  • 1970-01-01
  • 2018-09-04
  • 2020-10-22
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2019-04-30
相关资源
最近更新 更多