Search code examples
javascriptpythondockerdocker-composedocker-swarm

Running a docker container on localhost not working


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.


Solution

  • 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