Next.js App Not Listening on 0.0.0.0:3000 in Fly.io Deployment

I’m encountering an issue when deploying my Next.js application to Fly.io. Although my application starts correctly locally, Fly.io reports that the instance refused the connection. The error message in the logs is:

[PC01] instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)

Local Setup:

When I run npm run start locally, I see the following output:

npm run start

 web-desechables@0.1.0 start
> next start -H 0.0.0.0

   ▲ Next.js 15.2.3
   - Local:        http://localhost:3000
   - Network:      http://0.0.0.0:3000

This confirms that locally my app is listening on both localhost:3000 and 0.0.0.0:3000.

Configuration Files:

• package.json:

{
  "name": "web-desechables",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev -H 0.0.0.0",
    "build": "next build",
    "start": "next start -H 0.0.0.0",
    "lint": "next lint"
  },
  "dependencies": {
    "motion": "^12.6.2",
    "motion-plus": "^0.1.6",
    "next": "15.2.3",
    "next-themes": "^0.4.6",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  },
  "devDependencies": {
    "@eslint/eslintrc": "^3",
    "@tailwindcss/postcss": "^4",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19",
    "eslint": "^9",
    "eslint-config-next": "15.2.3",
    "tailwindcss": "^4",
    "typescript": "^5"
  }
}

fly.toml:

app = "xxxx"
primary_region = "mia"

[build]

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = 'stop'
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[env]
  PORT = 3000

[[vm]]
  memory = '1gb'
  cpu_kind = 'shared'
  cpus = 1
  memory_mb = 256

Dockerfile:

# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=20.18.0
FROM node:${NODE_VERSION}-slim AS base

LABEL fly_launch_runtime="Next.js"

# Next.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build node modules
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY package-lock.json package.json ./
RUN npm ci --include=dev

# Copy application code
COPY . .

# Build application
RUN npx next build --experimental-build-mode compile

# Remove development dependencies
RUN npm prune --omit=dev


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Entrypoint sets up the container.
ENTRYPOINT [ "/app/docker-entrypoint.js" ]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "npm", "run", "start" ]

Issue Summary:

• Locally, the app starts correctly with Next.js listening on 0.0.0.0:3000.

• On Fly.io, I get the error that the instance refused the connection.

• My configuration (both in package.json and fly.toml) is set to use port 3000 and bind to 0.0.0.0.

Use env variable HOST instead of passing -H

Hello, I modified it and this is what I changed:

fly.toml

[env]
  HOST = '0.0.0.0'
  PORT = "3000"

package.json

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },

Is this what you wanted to say?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.