如何从 Github Codespaces 连接到 Postgres

And*_*iss 6 postgresql node.js node-postgres docker-compose codespaces

我正在尝试 GitHub Codespaces,尝试使用 Node 和 Postgres 启动一个应用程序。

\n

我选择了以下选项:\n代码空间选项

\n

产生以下结果devcontainer.json

\n
// Update the VARIANT arg in docker-compose.yml to pick a Node.js version: 10, 12, 14 \n{\n    "name": "Node.js & PostgreSQL",\n    "dockerComposeFile": "docker-compose.yml",\n    "service": "app",\n    "workspaceFolder": "/workspace",\n\n    // Set *default* container specific settings.json values on container create.\n    "settings": { \n        "terminal.integrated.shell.linux": "/bin/bash",\n        "sqltools.connections": [{\n            "name": "Container database",\n            "driver": "PostgreSQL",\n            "previewLimit": 50,\n            "server": "localhost",\n            "port": 5432,\n            "database": "postgres",\n            "username": "postgres",\n            "password": "postgres"\n        }]\n    },\n\n    // Add the IDs of extensions you want installed when the container is created.\n    "extensions": [\n        "dbaeumer.vscode-eslint",\n        "mtxr.sqltools",\n        "mtxr.sqltools-driver-pg"\n    ]\n\n    // Use \'forwardPorts\' to make a list of ports inside the container available locally.\n    // "forwardPorts": [3000, 5432],\n\n    // Use \'postCreateCommand\' to run commands after the container is created.\n    // "postCreateCommand": "yarn install",\n\n    // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.\n    // "remoteUser": "node"\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

docker-compose.yml

\n
version: \'3\'\n\nservices:\n  app:\n    build: \n      context: .\n      dockerfile: Dockerfile\n      args:\n        # [Choice] Node.js version: 14, 12, 10\n        VARIANT: 14\n        # On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.\n        USER_UID: 1000\n        USER_GID: 1000\n\n    volumes:\n      - ..:/workspace:cached\n      \n    # Overrides default command so things don\'t shut down after the process ends.\n    command: sleep infinity\n\n    # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.\n    network_mode: service:db\n\n    # Uncomment the next line to use a non-root user for all processes.\n    # user: node\n\n    # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. \n    # (Adding the "ports" property to this file will not forward from a Codespace.)\n\n  db:\n    image: postgres:latest\n    restart: unless-stopped\n    volumes:\n      - postgres-data:/var/lib/postgresql/data\n    environment:\n      POSTGRES_PASSWORD: postgres\n      POSTGRES_USER: postgres\n      POSTGRES_DB: postgres\n\n    # Add "forwardPorts": ["5432"] to **devcontainer.json** to forward MongoDB locally.\n    # (Adding the "ports" property to this file will not forward from a Codespace.)\n\nvolumes:\n  postgres-data:\n
Run Code Online (Sandbox Code Playgroud)\n

package.json的如下:

\n
{\n  "dependencies": {\n    "pg": "^8.4.2"\n  },\n  "scripts": {\n    "start": "node index.js"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的index.js就是这样:

\n
const { Pool } = require("pg")\n\nconst db = new Pool()\n\ndb.query(`CREATE TABLE IF NOT EXISTS testing(id SERIAL PRIMARY KEY);`)\n
Run Code Online (Sandbox Code Playgroud)\n

运行yarn start会产生以下错误:

\n
codespace \xe2\x9e\x9c ~/workspace/codespace-demo (main \xe2\x9c\x97) $ yarn start\nyarn run v1.17.3\nwarning package.json: No license field\n$ node index.js\n(node:1037) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5432\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)\n(node:1037) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)\n(node:1037) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.\nDone in 0.11s.\n
Run Code Online (Sandbox Code Playgroud)\n

这让我相信 Postgres 实例没有在 Codespace 内启动并运行。

\n

我尝试取消注释有关转发端口的这一行:

\n
"forwardPorts": [3000, 5432]\n
Run Code Online (Sandbox Code Playgroud)\n

但那里没有运气。

\n

我可能缺少一些关于 Docker 组件如何在这里发挥作用的见解,但如果有人能给我指出正确的方向,我会很高兴!

\n

And*_*iss 6

我能够通过两点来实现这一点。

  1. .devcontainer仅仅在里面添加一个文件是不够的。您需要提交更改、推送更改、删除代码空间,然后创建一个新的代码空间以反映更改。

  2. 在 的内部.devcontainer/docker-compose.yml,我已将线路添加network_mode: hostdb服务中。根据此线程,此要求将很快被删除,但目前是必需的: https: //github.community/t/cant-connect-to-postgres/142655/2 ?u=andyweiss1982