Home > Enterprise >  nodejs docker localhost website is not working
nodejs docker localhost website is not working

Time:06-06

I want to run my nodejs app with docker but it seems not to be working

I have created my dockerfile

built and image

and created a container from the image

I tried accessing it through web browser but it is not working

Nodejs Code

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.json({ name: "John Stone", email: "[email protected]" });
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Dockerfile

FROM node:latest
WORKDIR /app
ADD . .
RUN npm install
CMD node index.js

Building image docker build --no-cache -t user-api:lastest .

creating a container docker run --name userapi -d -p 80:3000 2d97204ae010

Output When I go to the web broswer and type localhost:3000 I get "This page isn’t working" response

CodePudding user response:

-p 3000:80 should be -p 80:3000. The host port number comes first, and the container port number second. See this section of the docker run docs for more details.

  • Related