skip to Main Content

I locally launched docker image successfully but i can’t send any request on localhost.I have errors socket hang up in postman and localhost didn’t send any data.

docker file

FROM node:20-alpine

MAINTAINER Some Dev

RUN mkdir /app
WORKDIR /app

COPY ./backend /app

RUN npm i

CMD ["npm","start"]

app.ts and small rout that I’m trying to reach configs.PORT = 5000 configs.HOST = 0.0.0.0

app.get('/hello',async (req:Request,res:Response,next:NextFunction)=>{
   res.json("Hello")
})

const startServer = async () => {
  try {
    await mongoose.connect(configs.DB_URL);
    console.log("Connected to MongoDB");

      app.listen(+configs.PORT, configs.HOST,() =>{
          console.log(`Server listening on ${configs.PORT} and host ${configs.HOST}`);
      })

  } catch (err) {
    throw new ApiError(err.message, err.status);
  }
};

startServer();

docker build -t buxonlineappv2 .
docker run -d -p 5555:80 buxonlineappv2

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6fd90778e2df buxonlineappv2 "docker-entrypoint.s…" 51 seconds ago Up 39 seconds 0.0.0.0:5555->80/tcp jolly_saha

Postman
Browser

docker logs

In first i added host 0.0.0.0. Tried different ports. Tried localhost:5555/hello 0.0.0.0:5555/hello http:localhost:5555/hello

3

Answers


  1. Chosen as BEST ANSWER

    Thank you both. docker run -d -p 5555:80 buxonlineappv2 worked


  2. Your configs.PORT is set to 5000, so your app listens on port 5000.

    That means that that’s the port you need to map on the container side. Right now you map port 80 with

    docker run -d -p 5555:80 buxonlineappv2
    

    You should map port 5000 like this

    docker run -d -p 5555:5000 buxonlineappv2
    

    and it should work.

    Login or Signup to reply.
  3. since your server is listening on port 5000, you should run this command to run the container:

    docker run -d -p 5555:5000 buxonlineappv2
    

    so that 5555 port on host maps with 5000 port in container.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search