the-bed-stack icon indicating copy to clipboard operation
the-bed-stack copied to clipboard

Dev container does not support custom DB ports

Open yamcodes opened this issue 1 year ago • 1 comments

Version

latest

Describe the bug

The devcontainer does not support changing the DB port to something other than 5432.

Reproduction

  1. Occupy port 5432 with something.
  2. Change the DB port to 5431 in .env.
  3. Rebuild the devcontainer.
  4. You will get an ECONREFUSED error in the console.

System Info

Macbook Air M2

Validations

yamcodes avatar Oct 16 '23 17:10 yamcodes

Wait, isn't this because of how we do the port mapping in docker compose? It looks like this:

    ports:
      - ${POSTGRES_PORT}:5432

which means if you update the .env file, then the 5432 port inside the container will be mapped to a different port in your host machine. But the problem is that the app will try to connect to POSTGRES_PORT inside the container, because it's using that env var.

I think something like this would solve it:

    ports:
      - ${POSTGRES_PORT}:${POSTGRES_PORT}

& we need to add the POSGRES_PORT env var to the environment key too:

db:
    image: postgres:16
    restart: unless-stopped
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_PORT=${POSTGRES_PORT}
    healthcheck:
      test:
        [
          "CMD",
          "pg_isready",
          "-U",
          "${POSTGRES_USER}",
        ]
      interval: 5s
      timeout: 30s
      retries: 10
    # This allows accessing externally from "localhost" in addition to "127.0.0.1"
    ports:
      - ${POSTGRES_PORT}:${POSTGRES_PORT}

Hajbo avatar Oct 18 '23 20:10 Hajbo