Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker not exposing my application to run locally

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)
);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading