Trying to deploy a Nodejs application but facing the below error:
[error] instance refused connection. is your app listening on 0.0.0.0:8080? 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)
# syntax = docker/dockerfile:1
# Adjust NODE_VERSION as desired
ARG NODE_VERSION=18.14.2
FROM node:${NODE_VERSION}-slim as base
LABEL fly_launch_runtime="Node.js"
# Node.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 -y build-essential pkg-config python-is-python3
# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci
# Copy application code
COPY --link . .
# Final stage for app image
FROM base
# Copy built application
COPY --from=build /app /app
# Start the server by default, this can be overwritten at runtime
EXPOSE 8080
CMD [ "node", "server.js" ]
In server.js file
const port = process.env.port || 8080;
This is a very simple application and works perfectly locally. What am I missing here?
Most common error is that you are listing on localhost instead of 0.0.0.0 which means that your application will only accept connections made from the same machine, not the internet.
It is also possible that your application threw an exception before it got to the point where it called listen.
Iād need to see more of your logs and/or more of your server.js to help further.
Alternately I can offer you an example node application that you can see working:
mkdir demo
cd demo
npx --yes @flydotio/node-demo@latest
That application can be configured with a lot of options (example --express will convert it to use express as the web server), so perhaps you can get a configuration that closely matches your actual usage and then copy from that. See the following blog post for more details:
I did follow the above mentioned blog. Did deploy both the apps. The only difference is flydotio project installs npm packages during flyctl launch while my app fails to install npm packages.
I guess this is the issue for the failure. What should I do sothat flyctl launch runs npm install in my app also?
Not getting any error but npm install step is skipped
ā nodejs-app git:(feature/config-changes) flyctl launch
Creating app in /Users/len/projects/iynthogai/nodejs-app
Scanning source code
Detected a NodeJS app
? Choose an app name (leave blank to generate one): iynthogai-node-app
? Select Organization: Monbgul (monbgul)
Some regions require a paid plan (bom, fra, maa).
See https://fly.io/plans to set up a plan.
? Choose a region for deployment: Singapore, Singapore (sin)
App will use 'sin' region as primary
Created app 'iynthogai-node-app' in organization 'monbgul'
Admin URL: https://fly.io/apps/iynthogai-node-app
Hostname: iynthogai-node-app.fly.dev
create Dockerfile
Wrote config file fly.toml
Validating /Users/len/projects/iynthogai/nodejs-app/fly.toml
Platform: machines
ā Configuration is valid
If you need custom packages installed, or have problems with your deployment
build, you may need to edit the Dockerfile for app-specific changes. If you
need help, please post on https://community.fly.io.
Now: run 'fly deploy' to deploy your Node.js app.
npm install will be skipped if @flydotio/dockerfile is in your package.json already. Since I see create Dockerfile in your output @flydotio/dockerfile appears to be installed and working correctly.
What does the call to listen look like in your server.js?