Private networking - Cannot connect to my Docker Container

george-dragos-hutanu
HOBBY

5 months ago

I have deployed a docker container on port 8000.
I'm trying to connect to it thru a railway lambda function.
No result, I've tried everything.

My function:

import { Hono } from "hono@4";
import { cors } from "hono/cors";
import { resolve6 } from "dns/promises";

const app = new Hono();

app.use("/*", cors());

app.get("/", async (c) => {
  let resolvedResponse: any = null;
  let directResponse: any = null;
  let resolvedAddress: string | null = null;

  // --- Method 1: Using DNS resolution ---
  try {
    console.log("Starting DNS resolution for 'chroma.railway.internal'");
    const addresses = await resolve6("chroma.railway.internal");
    console.log("DNS results (IPv6):", addresses);

    if (!addresses || addresses.length === 0) {
      console.error("No IPv6 addresses found for 'chroma.railway.internal'");
      return c.text("No IPv6 addresses found.", 500);
    }

    resolvedAddress = addresses[0];
    console.log("Selected IPv6 address:", resolvedAddress);

    // Construct URL with resolved IPv6 address (inside square brackets) on port 80
    const resolvedUrl = `http://[${resolvedAddress}]:8000`;
    console.log("Constructed URL using resolved IPv6:", resolvedUrl);

    // Call the internal service using the resolved URL
    const resolvedFetchResponse = await fetch(resolvedUrl);
    console.log(
      "Response from internal service (resolved):",
      resolvedFetchResponse.status,
      resolvedFetchResponse.statusText
    );

    if (!resolvedFetchResponse.ok) {
      const errorText = await resolvedFetchResponse.text();
      console.error("Error during fetch call (resolved):", errorText);
      resolvedResponse = {
        error: errorText,
        status: resolvedFetchResponse.status,
      };
    } else {
      resolvedResponse = await resolvedFetchResponse.json();
      console.log(
        "Data received from internal service (resolved):",
        resolvedResponse
      );
    }
  } catch (error) {
    console.error(
      "Error during DNS resolution or fetch with resolved IP:",
      error
    );
    resolvedResponse = { error: error.toString() };
  }

  // --- Method 2: Direct call using DNS name ---
  try {
    const directUrl = "http://chroma.railway.internal:8000";
    console.log("Constructed URL for direct request:", directUrl);

    // Call the internal service directly using the DNS name
    const directFetchResponse = await fetch(directUrl);
    console.log(
      "Response from internal service (direct):",
      directFetchResponse.status,
      directFetchResponse.statusText
    );

    if (!directFetchResponse.ok) {
      const errorText = await directFetchResponse.text();
      console.error("Error during fetch call (direct):", errorText);
      directResponse = { error: errorText, status: directFetchResponse.status };
    } else {
      directResponse = await directFetchResponse.json();
      console.log(
        "Data received from internal service (direct):",
        directResponse
      );
    }
  } catch (error) {
    console.error("Error during direct fetch:", error);
    directResponse = { error: error.toString() };
  }

  // Return both results in the response
  return c.json({
    resolvedAddress: resolvedAddress,
    resolvedMethod: resolvedResponse,
    directMethod: directResponse,
  });
});

app.get("/api/health", (c) => c.json({ status: "ok" }));

Bun.serve({
  port: Number(import.meta.env.PORT) || 3000,
  fetch: app.fetch,
});

Logs:

DNS results (IPv6): [ "fd12:1a0e:96be::75:7c0b:3287" ]

Selected IPv6 address: fd12:1a0e:96be::75:7c0b:3287


Constructed URL using resolved IPv6: http://[fd12:1a0e:96be::75:7c0b:3287]:8000


Error during DNS resolution or fetch with resolved IP: error: Unable to connect. Is the computer able to access the url?
 path: "http://[fd12:1a0e:96be::75:7c0b:3287]:8000/",
 errno: 0,
 code: "ConnectionRefused"

Constructed URL for direct request: http://chroma.railway.internal:8000

Error during direct fetch: error: Unable to connect. Is the computer able to access the url?
 path: "http://chroma.railway.internal:8000/";,
 errno: 0,
 code: "ConnectionRefused"

HTTP result after calling the function:

{"resolvedAddress":"fd12:1a0e:96be::75:7c0b:3287","resolvedMethod":{"error":"Error: Unable to connect. Is the computer able to access the url?"},"directMethod":{"error":"Error: Unable to connect. Is the computer able to access the url?"}}
Awaiting User Response

1 Replies

5 months ago

Hello,

It is because Chroma is not listening on IPv6, that's an issue since this is an IPv6 only private network.

Please deploy this template -

https://railway.com/template/kbvIRV

And follow the instructions in the overview to enable IPv6.

Best,
Brody


Status changed to Awaiting User Response Railway 5 months ago