英文:
Cant connect to Database in a docker container with Table plus using vscode devcontainer
问题
你好,我需要一些帮助,我正在使用TablePlus连接我的数据库,但在添加forwardPorts时不起作用。
{
    "name": "Node.js & PostgreSQL",
    "dockerComposeFile": "docker-compose.yml",
    "service": "app",
    "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
    "forwardPorts": [5432]
}
docker-compose.yml
version: '3.8'
services:
    app:
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - ../..:/workspaces:cached
        # 覆盖默认命令,以防止进程结束后关闭容器。
        command: sleep infinity
        # 在与数据库容器相同的网络上运行应用程序,允许在devcontainer.json中使用"forwardPorts"。
        network_mode: service:db
        # 使用"forwardPorts"在**devcontainer.json**中本地转发应用程序端口。
        # (在此文件中添加"ports"属性将不会从Codespace中进行转发。)
    db:
        image: postgres:latest
        restart: unless-stopped
        volumes:
            - postgres-data:/var/lib/postgresql/data
        environment:
            POSTGRES_PASSWORD: postgres
            POSTGRES_USER: postgres
            POSTGRES_DB: postgres
        # 在**devcontainer.json**中添加"forwardPorts": ["5432"]以在本地转发PostgreSQL。
        # (在此文件中添加"ports"属性将不会从Codespace中进行转发。)
volumes:
    postgres-data:
Dockerfile
FROM mcr.microsoft.com/devcontainers/javascript-node:0-18
我已经在devcontainer.json中添加了forwardPorts和在docker-compose.yml中添加了端口,但这并没有改变任何事情。
英文:
Hello i need some help im using TablePlus to connect to my database but this isn't working with adding the forwardPorts
devcontainer.json
    {
        "name": "Node.js & PostgreSQL",
        "dockerComposeFile": "docker-compose.yml",
        "service": "app",
        "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
	      "forwardPorts": [5432]
    }
docker-compose.yml
version: '3.8'
services:
    app:
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - ../..:/workspaces:cached
        # 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
        # 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: postgres:latest
        restart: unless-stopped
        volumes:
            - postgres-data:/var/lib/postgresql/data
        environment:
            POSTGRES_PASSWORD: postgres
            POSTGRES_USER: postgres
            POSTGRES_DB: postgres
        # Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
        # (Adding the "ports" property to this file will not forward from a Codespace.)
volumes:
    postgres-data:
Dockerfile
FROM mcr.microsoft.com/devcontainers/javascript-node:0-18
I already add forwardPorts in devcontainer.json and port in docker-compose.yml
this doesn't change something
答案1
得分: 1
根据devcontainer.json的文档,当你在使用Docker Compose并且想要从一个不是主要容器的容器中转发端口时,你必须同时指定容器名称和端口。
你的devcontainer.json中应该有如下行:
"forwardPorts": ["db:5432"]
英文:
According to the documentation for devcontainer.json, you have to specify the container name together with the port, when using docker compose and you want to forward a port from a container which is not the primary container.
The line in your devcontainer.json should be:
"forwardPorts": ["db:5432"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论