3

I have created a Dockerfile image with an easy react app boilerplate result of npx create-react-app my-app

FROM node
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 4000
CMD "npm" "start

Everything worked fine and created the image, started the container, no errors but cannot open on localhost, does anybody know what could be the problem ?

I have used docker run -p 4000:4000 -it react-repo to start the container.

mcmwhfy
  • 1,654
  • 5
  • 36
  • 58
  • Could you provide the complete `docker run` command used. I believe it might be a problem of exposed ports. – usuario Sep 27 '21 at 05:49
  • @usuario `docker run -p 4000:4000 -it react-repo` – mcmwhfy Sep 27 '21 at 05:52
  • Have you configured the app to listen on port 4000? Looks like the the default is port 3000. – ogdenkev Sep 27 '21 at 06:13
  • Have you changed [`package.json`](https://stackoverflow.com/a/24750341/11647025) to expose port 4000? – crissal Sep 27 '21 at 06:24
  • @ogdenkev hmmm that could be the issue, but why if I'm exposing another port, is not loading the exposed port in Dockerfile ? – mcmwhfy Sep 27 '21 at 06:24
  • 1
    The `EXPOSE` instruction and `docker run -p 4000:4000` simply tell the docker daemon to expose a port from the container to the host. It does not ensure that anything inside the container is listening on that port. To do that, you have to set up your app to listen on port 4000. – ogdenkev Sep 27 '21 at 06:38

1 Answers1

3

A sample Docker file for below express.js app.

const express = require('express');

const PORT = 8080;
const HOST = '0.0.0.0';

const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

To Run,

# docker run -p <host-port>:<container-port> imageName
docker run -p 8080:8080 imageName
Ismail
  • 1,188
  • 13
  • 31