NODE JS with Typescript Docker Deployment issues with the PORT

024-10-02T21:00:55Z app[48e2905fd03968] ams [info]2024-10-02T21:00:55: PM2 log: PM2 successfully stopped
2024-10-02T21:00:56Z app[48e2905fd03968] ams [info] INFO Main child exited normally with code: 0
2024-10-02T21:00:56Z app[48e2905fd03968] ams [info] INFO Starting clean up.
2024-10-02T21:00:56Z app[48e2905fd03968] ams [info] WARN could not unmount /rootfs: EINVAL: Invalid argument
2024-10-02T21:00:56Z app[48e2905fd03968] ams [info][ 1.788432] reboot: Restarting system
2024-10-02T21:00:56Z runner[48e2905fd03968] ams [info]machine exited with exit code 0, not restarting
2024-10-02T21:01:02Z proxy[48e2905fd03968] ams [info]waiting for machine to be reachable on 0.0.0.0:8080 (waited 7.293498415s so far)
2024-10-02T21:01:08Z proxy[48e2905fd03968] ams [info]waiting for machine to be reachable on 0.0.0.0:8080 (waited 13.298206782s so far)
2024-10-02T21:01:14Z proxy[48e2905fd03968] ams [info]waiting for machine to be reachable on 0.0.0.0:8080 (waited 19.301531963s so far)
2024-10-02T21:01:19Z proxy[48e2905fd03968] ams [error][PM05] failed to connect to machine: gave up after 15 attempts (in 24.304562923s)
2024-10-02T21:01:21Z proxy[185e64eb46d758] ams [info]Starting machine
2024-10-02T21:01:21Z app[185e64eb46d758] ams [info]2024-10-02T21:01:21.714401009 [01J97FSDC5P75DDG2BTEEAY8G3:main] Running Firecracker v1.7.0
2024-10-02T21:01:22Z app[185e64eb46d758] ams [info][ 0.288069] PCI: Fatal: No config space access function found
2024-10-02T21:01:22Z app[185e64eb46d758] ams [info] INFO Starting init (commit: 04656915)…
2024-10-02T21:01:22Z app[185e64eb46d758] ams [info] INFO Preparing to run: docker-entrypoint.sh pm2-runtime ecosystem.config.js as root
2024-10-02T21:01:22Z app[185e64eb46d758] ams [info] INFO [fly api proxy] listening at /.fly/api
2024-10-02T21:01:22Z app[185e64eb46d758] ams [info]2024/10/02 21:01:22 INFO SSH listening listen_address=[fdaa:a:53c7:a7b:42:36f9:6c02:2]:22 dns_server=[fdaa::3]:53
2024-10-02T21:01:22Z runner[185e64eb46d758] ams [info]Machine started in 1.291s
2024-10-02T21:01:22Z proxy[185e64eb46d758] ams [info]machine started in 1.384540716s
2024-10-02T21:01:23Z app[185e64eb46d758] ams [info]2024-10-02T21:01:23: PM2 log: Launching in no daemon mode
2024-10-02T21:01:23Z app[185e64eb46d758] ams [info]2024-10-02T21:01:23: PM2 error: Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/src/app/ecosystem.config.js from /usr/local/lib/node_modules/pm2/lib/Common.js not supported.
2024-10-02T21:01:23Z app[185e64eb46d758] ams [info]Instead change the require of ecosystem.config.js in /usr/local/lib/node_modules/pm2/lib/Common.js to a dynamic import() which is available in all CommonJS modules.
2024-10-02T21:01:23Z app[185e64eb46d758] ams [info]2024-10-02T21:01:23: PM2 error: require() of ES Module /usr/src/app/ecosystem.config.js from /usr/local/lib/node_modules/pm2/lib/Common.js not supported.
2024-10-02T21:01:23Z app[185e64eb46d758] ams [info]Instead change the require of ecosystem.config.js in /usr/local/lib/node_modules/pm2/lib/Common.js to a dynamic import() which is available in all CommonJS modules.
2024-10-02T21:01:23Z app[185e64eb46d758] ams [info]2024-10-02T21:01:23: PM2 log: PM2 successfully stopped
2024-10-02T21:01:24Z app[185e64eb46d758] ams [info] INFO Main child exited normally with code: 0
2024-10-02T21:01:24Z app[185e64eb46d758] ams [info] INFO Starting clean up.
2024-10-02T21:01:24Z app[185e64eb46d758] ams [info] WARN could not unmount /rootfs: EINVAL: Invalid argument
2024-10-02T21:01:24Z app[185e64eb46d758] ams [info][ 2.875899] reboot: Restarting system
2024-10-02T21:01:25Z runner[185e64eb46d758] ams [info]machine exited with exit code 0, not restarting

The port issue is still there even after doing this
const server = app.listen(8080,“0.0.0.0”, () => {

console.log(Server is listening on port ${PORT})
})

FROM node:20-alpine

Create app directory

WORKDIR /usr/src/app

Copy package files and configs

COPY package*.json ./

COPY tsconfig.json ./

COPY ecosystem.config.js ./

Install dependencies and PM2

RUN npm ci

RUN npm install pm2 -g

Copy source code

COPY . .

Build TypeScript code

RUN npm run build

ENV PORT=8080

ENV HOST=0.0.0.0

EXPOSE 8080

Start the server with PM2

CMD [“pm2-runtime”, “ecosystem.config.js”]

export default {
apps: [{
name: ‘server’,
script: ‘./dist/server.js’,
instances: 1,
exec_mode: ‘cluster’,
env: {
NODE_ENV: process.env.NODE_ENV || ‘development’,
PORT: process.env.PORT || 8080,
HOST: ‘0.0.0.0’,
ALCHEMY_API_KEY: process.env.ALCHEMY_API_KEY,
CAPSULE_API_KEY: process.env.CAPSULE_API_KEY,
ALCHEMY_GAS_POLICY_ID: process.env.ALCHEMY_GAS_POLICY_ID,
DATABASE: process.env.DATABASE,
ENCRYPTION_KEY: process.env.ENCRYPTION_KEY,
USER_REGISTRY_CONTRACT_ADDRESS: process.env.USER_REGISTRY_CONTRACT_ADDRESS,
CHAINID: process.env.CHAINID
}
}]
}

I would personally avoid using pm2 and just raw dog the node server. The overhead isn’t worth it.