Home > Enterprise >  Docker not exposing my application to run locally
Docker not exposing my application to run locally

Time:07-30

I've an express application which looks as follows:

const express = require("express");
const PORT = process.env.PORT || 3001;
app = express();

app.all("*", (req, res) =>
  res.status(200).json({
    status: 200,
    message: "Hello world from Docker.",
  })
);

app.listen(PORT, "127.0.0.1", () =>
  console.log("The server is running on port: %s", PORT)
);

I want to build an image based on this simple express application so my Dockerfile looks as follows:

FROM node:18-alpine

WORKDIR /app

COPY package*.json .

RUN npm install

COPY . .

EXPOSE 3001

CMD ["npm", "start"]

.dockerignore looks as follows:

node_modules

This is how i built my image:

docker build -t my-app:1.0 .

Now if i start an image locally as follows:

docker run -p 3001:3001 my-app:1.0

And if i visit on my web-browser at http://localhost:3001, there's nothing showing in the browser.

OS: Windows 10

What am i missing here?

CodePudding user response:

You can expose the app for external connections this way:

app.listen(PORT, "0.0.0.0", () =>
  console.log("The server is running on port: %s", PORT)
);

  • Related