Nodejs API - App starts, but refuses connections

I have a very simple nodejs app using fastify. I followed the NodeJS Guide for a basic setup, but my app is using Typescript.

My app builds fine locally and builds successfully when deploying to fly.io, but once I attempt to make a request, it ends up failing. I think this is some combination of my fly.toml or Dockerfile networking being wonky. Thanks in advance, hopefully this is very straightforward.

Dockerfile

FROM debian:bullseye as builder

ARG NODE_VERSION=16.16.0

RUN apt-get update; apt install -y curl python-is-python3 pkg-config build-essential
RUN curl https://get.volta.sh | bash
ENV VOLTA_HOME /root/.volta
ENV PATH /root/.volta/bin:$PATH
RUN volta install node@${NODE_VERSION}

#######################################################################

RUN mkdir /app
WORKDIR /app

# NPM will not install any package listed in "devDependencies" when NODE_ENV is set to "production",
# to install all modules: "npm install --production=false".
# Ref: https://docs.npmjs.com/cli/v9/commands/npm-install#description


COPY . .

RUN npm install
FROM debian:bullseye

LABEL fly_launch_runtime="nodejs"

COPY --from=builder /root/.volta /root/.volta
COPY --from=builder /app /app

WORKDIR /app
ENV NODE_ENV production
ENV PATH /root/.volta/bin:$PATH

EXPOSE 8080
CMD [ "npm", "run", "start" ]
# fly.toml file generated for dry-frog-7810 on 2023-02-12T19:44:32-05:00

app = "my-app-name"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]

[experimental]
  auto_rollback = true
  private_network = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

Below are some logs of what ends up happening:

 2023-02-13T01:09:02.118 app[6e5c9c06] ord [info] {"level":30,"time":1676250542117,"pid":557,"hostname":"6e5c9c06","msg":"Server listening at http://127.0.0.1:8080"}

2023-02-13T01:09:02.120 app[6e5c9c06] ord [info] {"level":30,"time":1676250542120,"pid":557,"hostname":"6e5c9c06","msg":"Server listening at http://[::1]:8080"}

2023-02-13T01:09:56.872 proxy[ee61af66] ord [error] instance refused connection
2023-02-13T01:09:56.991 proxy[6e5c9c06] ord [error] instance refused connection
2023-02-13T01:10:01.420 proxy[6e5c9c06] lax [warn] Could not proxy HTTP request. Retrying in 1000 ms (attempt 10) 

I think this is actually an issue with my Fastify setup - debugging, but getting closer. Will post my findings for others.

Probably not the problem, but given that you are using Typescript, I would have expected a line like:

RUN npm run build

… immediately after the COPY . . line. And for the typescript output directory to be included in your .dockerignore file.

I have done some debugging, and I can hit very simple route with no issues. I believe the issue is when I am trying to make a request to my postgres supabase DB. Even removing ENV vars from the equation, any request to my supabase DB fails. Totally works locally though, same config.

I can also make a simple axios request to a public free API. Curious if ther eis something up with how fly.io is making requests from my app? Possibly my supabase db isnt handling it?

When these requests fail, I get a 502 and I see in my supabase postgres logs:

could not receive data from client: Connection reset by peer

This seems to be my exact issue - it seems to maybe resolve my issue