microsoft / vscode-dev-containers

NOTE: Most of the contents of this repository have been migrated to the new devcontainers GitHub org (https://github.com/devcontainers). See https://github.com/devcontainers/template-starter and https://github.com/devcontainers/feature-starter for information on creating your own!
https://aka.ms/vscode-remote
MIT License
4.7k stars 1.41k forks source link

Unable to connect to postgresql database in docker container #1723

Open Akarimichi opened 1 year ago

Akarimichi commented 1 year ago

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

david-engelmann commented 1 year ago

This repo isn't active anymore. You'll get better results posting in this repo.