最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Running a docker container on localhost not working - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share edited Sep 27, 2021 at 5:51 mcmwhfy asked Sep 27, 2021 at 5:45 mcmwhfymcmwhfy 1,6866 gold badges36 silver badges60 bronze badges 6
  • Could you provide the plete docker run mand used. I believe it might be a problem of exposed ports. – usuario Commented Sep 27, 2021 at 5:49
  • @usuario docker run -p 4000:4000 -it react-repo – mcmwhfy Commented Sep 27, 2021 at 5:52
  • Have you configured the app to listen on port 4000? Looks like the the default is port 3000. – ogdenkev Commented Sep 27, 2021 at 6:13
  • Have you changed package.json to expose port 4000? – crissal Commented Sep 27, 2021 at 6: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 Commented Sep 27, 2021 at 6:38
 |  Show 1 more ment

2 Answers 2

Reset to default 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's answer worked for me

#docker run -p : imageName docker run -p 8080:8080 imageName

deeper explanation: In my case I'm running it on ec2 instance where port 80 in the ec2 system is already alloted to something else. Hence, I can only assign my host port to another port, in my case i assigned it to 8080.

but, I don't want to access my api this way: http://x.x.x.x:8080/hotels

I wantd to access it direct to port 80, the port where websites are accessed, I want to access below way:

http://x.x.x.x/hotels

For above to work you need to below:

docker run -p 8080:80 imageName

Deeper explanation: In my aws operating system I'm still running it internally using port 8080. But when a client opens up my application through the operating system (pardon my pun) inside docker, I'm mapping the port to 80.

Hence, (pun) in the operating inside my container the app is accessed by an outside user through port 80 no problem. But internally when docker municates with the kernel inside the aws instance, it is still calling it from 8080.

发布评论

评论列表(0)

  1. 暂无评论