neondatabase / wsproxy

Apache License 2.0
120 stars 6 forks source link

Not being able to connect to named host from another docker-compose service #3

Closed kevinwolfcr closed 1 year ago

kevinwolfcr commented 1 year ago

Steps to reproduce

Following this tutorial, I created this file:

docker-compose.yml

version: "3"

services:
  db:
    image: postgres:15.2-alpine
    environment:
      POSTGRES_USER: my_user
      POSTGRES_PASSWORD: my_password
      POSTGRES_DB: my_db
    ports:
      - 5432:5432
  db-proxy:
    platform: linux/x86_64
    image: ghcr.io/neondatabase/wsproxy:latest
    environment:
      APPEND_PORT: db:5432
      ALLOW_ADDR_REGEX: .*
      LOG_TRAFFIC: true
    ports:
      - 5433:80
    depends_on:
      - db

Expected result

My request is proxied successfully.

Actual result

The request is not proxied, since the APPEND_PORT is just concatenating to the addr in the script. So it is trying to proxy to localhost:5432db:5432 rather than db:5432

Environment

Local development using docker-compose in an Apple M1 Pro computer (not sure if this is the issue, since I have experienced issues with docker on this architecture before).

Logs, links

docker logs

docker-compose up --pull --build
[+] Running 2/2
 ✔ db-proxy Pulled                                                                                  1.4s 
 ✔ db Pulled                                                                                        4.4s 
[+] Building 0.0s (0/0)                                                                                  
[+] Running 2/1
 ✔ Container web-db-1        Recreated                                                              0.1s 
 ✔ Container web-db-proxy-1  Recreated                                                              0.0s 
Attaching to web-db-1, web-db-proxy-1
web-db-1        | 
web-db-1        | PostgreSQL Database directory appears to contain a database; Skipping initialization
web-db-1        | 
web-db-1        | 2023-06-24 18:39:13.993 UTC [1] LOG:  starting PostgreSQL 15.2 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924, 64-bit
web-db-1        | 2023-06-24 18:39:13.993 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
web-db-1        | 2023-06-24 18:39:13.993 UTC [1] LOG:  listening on IPv6 address "::", port 5432
web-db-1        | 2023-06-24 18:39:13.995 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
web-db-1        | 2023-06-24 18:39:14.001 UTC [24] LOG:  database system was shut down at 2023-06-24 18:39:08 UTC
web-db-1        | 2023-06-24 18:39:14.007 UTC [1] LOG:  database system is ready to accept connections
web-db-proxy-1  | 2023/06/24 18:39:14 Using regex for allowed addresses: .*
web-db-proxy-1  | 2023/06/24 18:39:14 Starting server on port :80
web-db-proxy-1  | 2023/06/24 18:39:21 Got request from 172.19.0.1:45766
web-db-proxy-1  | 2023/06/24 18:39:21 Got request from 172.19.0.1:45766, proxying to localhost:5432db:5432, allowed=true, addressArg=localhost:5432, hostHeader=localhost:5433
web-db-proxy-1  | 2023/06/24 18:39:21 failed to handle websocket: dial tcp: address localhost:5432db:5432: too many colons in address
petuhovskiy commented 1 year ago

Hello @kevinwolfcr!

I tried to reproduce your issue with the code from the tutorial and didn't have the issues with wsproxy. Here's my wsproxy output:

tmpdc-db-proxy-1  | 2023/06/26 19:00:24 Got request from 172.20.0.1:34270
tmpdc-db-proxy-1  | 2023/06/26 19:00:24 Got request from 172.20.0.1:34270, proxying to db:5432, allowed=true, addressArg=, hostHeader=localhost:5433
tmpdc-db-proxy-1  | 2023/06/26 19:00:24 Got 58 bytes client->pg: AAAAOgADAAB1c2VyAG15X3VzZXIAZGF0YWJhc2UAbXlfZGIAY2xpZW50X2VuY29kaW5nAFVURjgAAA==
tmpdc-db-proxy-1  | 2023/06/26 19:00:24 Got 24 bytes pg->client: UgAAABcAAAAKU0NSQU0tU0hBLTI1NgAA

Client code was taken from the tutorial:

// if we're running locally
if (!process.env.VERCEL_ENV) {
  // Set the WebSocket proxy to work with the local instance
  neonConfig.wsProxy = (host) => `${host}:5433/v1`;
  // Disable all authentication and encryption
  neonConfig.useSecureWebSocket = false;
  neonConfig.pipelineTLS = false;
  neonConfig.pipelineConnect = false;

  console.log(neonConfig);
}

if (!process.env.POSTGRES_URL) {
  throw new Error("POSTGRES_URL is not set");
}

export const db = new Kysely<Database>({
  dialect: new NeonDialect({
    connectionString: process.env.POSTGRES_URL,
  }),
});

POSTGRES_URL here is postgres://my_user:my_password@localhost:5432/my_db.

By looking at your output, I can guess that you have a different neonConfig.wsProxy function, you can try to copy it from my snippet and it should work for you.

Or, if you just remove APPEND_PORT from docker-compose.yml file, it will not spoil the address and should work as well.

kevinwolfcr commented 1 year ago

Hello @petuhovskiy, thanks for your answer. You are right, indeed the issue was that I wasn't defining a wsProxy function and instead, passing a string directly (neonConfig.wsProxy =localhost:5433/v`), switching to function mode worked.

Thanks!