Search code examples
node.jsdockerexpressdocker-composedockerfile

Typescript Express wont start listening in docker


I'm trying to start a express server in docker, but it wont start listening, last message i get on screen is "Almost there"

Code of server:

index.ts

import express from 'express';
import dotenv from "dotenv"
dotenv.config()

const PORT = process.env.PORT

const app = express();

console.log("Almost there"); #also tried printing ${PORT}, works fine
app.listen(parseInt(PORT!), "0.0.0.0", () =>{
    `Started listening on port: ${PORT}`
});

Dockerfile:

FROM node:16 #tried various versions, won't budge
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY . .
RUN yarn
EXPOSE 5000
CMD [ "npx", "ts-node", "index.ts" ]

docker-compose:

...
  backend:
    build:
      context: backend
    environment:
      PORT: 5000;
    ports:
      - "5000:5000"

and finally package.json:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "author": "",
  "dependencies": {
    "@types/express": "^4.17.19",
    "dotenv": "^16.3.1",
    "ethers": "^6.7.1",
    "express": "^4.17.0",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  }
}

I tried changing various versions of node, changed few versions of express, tried changing ts-node to tsx. Everything works fine on my pc, just doesn't start listening on that port inside docker


Solution

  • Case closed... While I had more stuff in the code, I'd always get http connection error to the container, than I added the "Started listening on port" and started changing stuff. I guess I had fixed my own problem, but waited for the message to appear in the logs.. I just opened one get endpoint and it's working... Sorry everyone =(