javascript – Docker – 为什么这个带有公开/发布端口的express.js容器拒绝连接? (使用boot2docker)

前端之家收集整理的这篇文章主要介绍了javascript – Docker – 为什么这个带有公开/发布端口的express.js容器拒绝连接? (使用boot2docker)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在docker容器中有一个简单的hello world express.js应用程序.它设置为在端口8080上运行,并且docker文件在映像中公开此端口.另外,我在运行图像时发布了端口.然而,当我尝试做一个简单的curl请求时,连接被拒绝.以下是我设置此测试的方法

我的Dockerfile非常简单:

  1. FROM node
  2. ADD ./src /src
  3. WORKDIR /src
  4. # install your application's dependencies
  5. RUN npm install
  6. # replace this with your application's default port
  7. EXPOSE 8080
  8. # replace this with your main "server" script file
  9. CMD [ "node","server.js" ]

在我的./src目录中,我有一个如下所示的server.js文件

  1. var express = require('express');
  2. var app = express();
  3. app.get('/',function(req,res){
  4. res.send('Hello World');
  5. });
  6. var server = app.listen(8080,function() {
  7. console.log('Listening on port %d',server.address().port);
  8. });

以及一个基本的package.json,它看起来像这样:

  1. {
  2. "name": "hello-world","description": "hello world test app","version": "0.0.1","private": true,"dependencies": {
  3. "express": "4.7.2"
  4. }
  5. }

图像构建得很好:

  1. docker build -t jimjeffers/hello-world .
  2. Sending build context to Docker daemon 1.126 MB
  3. Sending build context to Docker daemon
  4. Step 0 : FROM node
  5. ---> 6a8a9894567d
  6. Step 1 : ADD ./src /src
  7. ---> 753466503fbf
  8. Removing intermediate container 135dab70dfff
  9. Step 2 : WORKDIR /src
  10. ---> Running in 12257ff3f990
  11. ---> 010ce4140cdc
  12. Removing intermediate container 12257ff3f990
  13. Step 3 : RUN npm install
  14. ---> Running in 1a9a0eb9d188
  15. ---> 5dc97c79281e
  16. Removing intermediate container 1a9a0eb9d188
  17. Step 4 : EXPOSE 8080
  18. ---> Running in abbaadf8709d
  19. ---> 9ed540098ed2
  20. Removing intermediate container abbaadf8709d
  21. Step 5 : CMD [ "node","server.js" ]
  22. ---> Running in 63b14b5581cd
  23. ---> eababd51b50e
  24. Removing intermediate container 63b14b5581cd
  25. Successfully built eababd51b50e

并开始很好:

  1. docker run -P -d jimjeffers/hello-world
  2. ee5024d16a679c10131d23c1c336c163e9a6f4c4ebed94ad4d2a5a66a64bde1d
  3. docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. ee5024d16a67 jimjeffers/hello-world:latest node server.js About an hour ago Up 11 seconds 0.0.0.0:49158->8080/tcp jovial_engelbart
  6. 5d43b2dee28d mongo:2.6 /usr/src/mongo/docke 5 hours ago Up 3 hours 27017/tcp some-mongo

我可以确认服务器在容器内运行:

  1. docker logs ee5024d16a67
  2. Listening on port 8080

但是,如果我尝试提出请求,则拒绝连接.

  1. curl -i 0.0.0.0:49158
  2. curl: (7) Failed connect to 0.0.0.0:49158; Connection refused

这里有什么我想念的吗?如果我在不使用docker的情况下运行应用程序,它会按预期工作:

  1. node src/server.js
  2. Listening on port 8080
  3. curl -i 0.0.0.0:8080
  4. HTTP/1.1 200 OK
  5. X-Powered-By: Express
  6. Content-Type: text/html; charset=utf-8
  7. Content-Length: 11
  8. ETag: W/"b-1243066710"
  9. Date: Mon,04 Aug 2014 05:11:58 GMT
  10. Connection: keep-alive
  11. Hello World
最佳答案
我发现了混乱的根源.我的机器在Mac OSX上运行,因此我用boot2docker安装了docker.

所以再次重复这个过程:

  1. docker run -P -d jimjeffers/hello-world
  2. 28431b32b93dbecaa2a8a5b129cbd36eebe8a90f4c20ab10735455d82fa9de37
  3. docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 28431b32b93d jimjeffers/hello-world:latest node server.js 2 hours ago Up 9 minutes 0.0.0.0:49159->8080/tcp stoic_franklin
  6. 5d43b2dee28d mongo:2.6 /usr/src/mongo/docke 6 hours ago Up 4 hours 27017/tcp some-mongo

最后,诀窍不是连接到我自己的机器,而是从VM的IP地址卷曲:

  1. boot2docker ip
  2. The VM's Host only interface IP address is: 192.168.59.103

所以当我卷起虚拟机时,我终于取得了成功:

  1. curl -i 192.168.59.103:49159
  2. HTTP/1.1 200 OK
  3. X-Powered-By: Express
  4. Content-Type: text/html; charset=utf-8
  5. Content-Length: 11
  6. ETag: W/"b-1243066710"
  7. Date: Mon,04 Aug 2014 04:32:37 GMT
  8. Connection: keep-alive

在Docker的installation guide上详细解释了这一点,但我错过了它,因为它是在文档的最后.

猜你在找的Docker相关文章