简体   繁体   English

Docker node.js应用程序没有侦听端口

[英]Docker node.js app not listening on port

I'm working on a node.js web application and use localhost:8080 to test it by sending requests from Postman. 我正在使用node.js Web应用程序并使用localhost:8080通过发送Postman的请求来测试它。 Whenever I run the application (npm start) without using Docker, the app works fine and listens on port 8080 . 每当我在不使用Docker的情况下运行应用程序(npm start)时,应用程序工作正常并侦听端口8080

When I run the app using Docker, The app seems to be running correctly (it displays that it is running and listening on port 8080 ), however it is unreachable using Postman (I get the error: Could not get any response ). 当我使用Docker运行应用程序时,应用程序似乎正在正常运行(它显示它正在运行并在端口8080上侦听),但是使用Postman无法访问(我收到错误: Could not get any response )。 Do you know what the reason for this could be? 你知道这个的原因是什么吗?

Dockerfile: Dockerfile:

FROM node:8  
WORKDIR /opt/service
COPY package.json .
COPY package-lock.json .
RUN npm i
COPY . .
EXPOSE 8080
CMD ["npm", "start"]

I build and run the application in Docker using: 我使用以下命令在Docker中构建并运行应用程序:

docker build -t my-app . 
docker run my-app

I have tried binding the port as described below, but I also wasn't able to reach the server on port 8181 . 我已尝试绑定端口,如下所述,但我也无法通过端口8181到达服务器。

docker run -p 8181:8080 my-app

In my application, the server listens in the following way (using Express ): 在我的应用程序中,服务器以下列方式进行侦听(使用Express ):

app.listen(8080, () => {
    console.log('listening on port 8080');
})

I have also tried using: 我也试过用:

app.listen(8080, '0.0.0.0', () => {
    console.log('listening on port 8080');
})

The docker port command returns: docker port命令返回:

8080/tcp -> 0.0.0.0:8181

Do you guys have nay idea what the reason for this could be? 你们有没有想到这可能是什么原因?

UPDATE : Using the IP I obtained from the docker-machine ( 192.168.99.100:8181 ) I was able to reach the app. 更新 :使用从docker-machine( 192.168.99.100:8181 )获得的IP,我能够访问该应用程序。 However, I want to be able to reach it from localhost:8181 . 但是,我希望能够从localhost:8181到达它。

The way you have your port assignment setup requires you to use the docker machine's ip address, not your localhost. 设置端口分配的方式要求您使用docker机器的ip地址,而不是localhost。 You can find your docker machines ip using: 您可以使用以下命令找到您的docker机器ip:

docker-machine ip dev

If you want to map the container ip to your localhost ports you should specify the localhost ip before the port like this : 如果要将容器ip映射到localhost端口,则应在端口之前指定localhost ip,如下所示

docker run -p 127.0.0.1:8181:8080 my-app

Similar question : 类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM