Dau*_*eDK 6 github mongodb github-codespaces
I'm trying out Github codespaces, specifically the "Node.js & Mongo DB" default settings.
The port is forwarded, and my objective is to connect with MongoDB Compass running on my local machine.
The address forwarded to 27017 is something like https://<long-address>.githubpreview.dev/
I attempted to use the following connection string, but it did not work in MongoDB compass. It failed with No addresses found at host. I'm actually unsure about how I even determine if MongoDB is actually running in the Github codespace?
mongodb+srv://root:example@<long-address>.githubpreview.dev/
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
And a devcontainer.json file
// 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"
}
}
Run Code Online (Sandbox Code Playgroud)
and finally a Docker file:
# [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>"
Run Code Online (Sandbox Code Playgroud)
Update I also posted here in the MongoDB community, but no help...
正如@iravinandan 所说,你需要建立一条隧道。
单独发布端口并无帮助,因为所有传入请求都通过 http 代理进行。
如果您dig CNAME <long-address>.githubpreview.dev会看到它是 github-codespaces.app.online.visualstudio.com。您可以将任何内容放入 githubpreview.dev 子域中,它仍然会在 DNS 级别上解析。
代理依赖 HTTP 主机标头将请求路由到正确的上游,因此它仅适用于 HTTP 协议。
要使用任何其他协议(在您的情况下为 MongoDb 有线协议),您需要设置从代码空间到您的计算机的 TCP 隧道。
在撰写本文时,默认的 Node + Mongo 代码空间使用 Debian buster,因此ssh 端口转发将是显而易见的选择。在 codespace/VSCode 终端中:
ssh -R 27017:localhost:27017 your_public_ip
Run Code Online (Sandbox Code Playgroud)
然后在你的罗盘中连接到
mongodb://localhost:27017
Run Code Online (Sandbox Code Playgroud)
当然,它需要您的本地计算机运行 sshd,拥有一个白色 IP(或者至少您的路由器应该将传入的ssh 流量转发到您的计算机)并允许它进入防火墙。如果 27017 已在本地使用,您可以选择任何端口。
这是最简单的设置,但会将您的笔记本电脑暴露在互联网上,并且被感染只是时间问题。
为了让你的本地系统位于 DMZ 后面,你可以设置一个 Jumpbox - 互联网上某个地方的一个简约的一次性 Linux 盒子,它将用于链接 2 个隧道:
相同
mongodb://localhost:27017
Run Code Online (Sandbox Code Playgroud)
在蒙戈罗盘上。
Jumpbox 必须将 sshd 暴露到互联网,但您可以通过强化其安全性来最大程度地降低风险。毕竟它除了代理流量之外什么也不做。EC2 nano 已经足够了,但请记住,大数据传输可能会很昂贵。
您可以在 5 分钟内尝试一下。ngrok已经存在十多年了,它正是这样做的 - 它出售隧道(有一些免费层足以用于演示)。
在您的代码空间/VScode 终端中:
npm i ngrok --save-dev
Run Code Online (Sandbox Code Playgroud)
避免每次安装,但确保您不附带生产代码。您需要在 ngrok 上注册一个帐户(使用 github 进行 SSO 即可)以获取身份验证代码并将其传递到 codespaces/VSCode 终端:
./node_modules/.bin/ngrok authtoken <the token>
Run Code Online (Sandbox Code Playgroud)
请记住,它将令牌保存到主目录,重建后该目录将被擦除。获得授权后,您可以在 codespace/VSCode 终端中打开隧道:
./node_modules/.bin/ngrok tcp 27017
Run Code Online (Sandbox Code Playgroud)
Codespace 会自动转发端口:
终端将向您显示一些统计信息(注意免费套餐限制)和连接字符串:
每次打开隧道时,子域和端口都会更改。从上图中,mongodb compas 的连接参数为:
mongodb://0.tcp.ngrok.io:18862
Run Code Online (Sandbox Code Playgroud)
根据需要在 mongodb 级别使用授权参数。
再次请记住,您将 mongodb 暴露在互联网上 (0.tcp.ngrok.io:18862),并且 mongo 接受未经身份验证的连接。
我不会让它打开的时间超过必要的时间。
Node + mongo 环境预装了方便的 VScode 插件:
当然,它缺少许多 Compas 分析工具,但它开箱即用,足以进行开发。
只需打开插件并连接到本地主机:
在不影响安全性的情况下获得指南针功能并实现零配置目标的最佳选择是自己托管指南针。它是一个电子应用程序,可以在 Mongodb Atlas 的浏览器中完美运行。
源代码可在https://github.com/mongodb-js/compass获取。通过一些努力,您可以制作一个 docker 镜像来托管 compass,将该镜像包含到 docker-compose 中,并在 devcontainer.json 中转发端口
Github codespaces 将负责身份验证(将转发的端口保持私有,以便只有空间的所有者才能访问它)。从桌面到 compass 的所有通信都将通过 https 进行,并且 compass 到 mongodb 将位于 docker 网络本地。安全方面它将与 VSCode mongodb 插件相当