I'm trying to convert this app to docker but I'm getting this error.
Here's my express.js file
const express = require("express");
const app = express();
const portNumber = 3000;
const sourceDir = "dist";
const expressStaticGzip = require("express-static-gzip");
app.use(
"/",
expressStaticGzip(sourceDir, {
enableBrotli: true,
orderPreference: ["br", "gz"],
setHeaders: function(res, path) {
res.setHeader("Cache-Control", "public, max-age=31536000");
}
})
);
app.listen(portNumber, () => {
console.log(`Express web server started: http://localhost:${portNumber}`);
console.log(`Serving content from /${sourceDir}/`);
});
here's my dockerfile
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "node", "express.js" ]
Help is appreciated.
This is a very common error. When you're broadcasting within a docker container you usually have to listen on host 0.0.0.0 not just localhost. If you listen on localhost docker won't be able to route traffic from outside of the container to the loopback interface within the container.
You can fix this by updating your express code to listen on 0.0.0.0. I believe that's this:
app.listen(portNumber, "0.0.0.0", () => {
console.log(`Express web server started: http://0.0.0.0:${portNumber}`);
console.log(`Serving content from /${sourceDir}/`);
});
You'll then need to run the dockerfile with something like:
docker build -t node-app .
docker run -p 3000:3000 node-app
I was running the angular application using the command ng serve and facing the same problem.
Because the live development server listens to http://localhost:4200 by default.
than I used the command ng serve --host 0.0.0.0 to run the angular project and that solved my problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With