Docker运行,无法访问应用程序

我build立一个运行在3000端口上的Express(nodejs)应用程序,带有一个简单的hello world,并且被加热到一个公共的github仓库。

其实,它工作得很好,代码看起来像这样:

var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); }); 

我正在学习docker,并且想让这个小代码在一个容器中工作,所以我创build了这个dockerfile:

 FROM phusion/baseimage:0.9.17 # Use baseimage-docker's init system. CMD ["/sbin/my_init"] # Install corresponding packages # Git, pm2, curl RUN apt-get update && apt-get install -y \ git \ curl # Downloading NodeJs v0.12 RUN curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash - RUN apt-get install -y nodejs # Cloning repository RUN git clone https://github.com/User/hello-world.git # Install global dependencies RUN npm install -g pm2 # Move inside of the project RUN cd hello-world && npm install && pm2 start app.js # Clean up APT when done. RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* 

然后我运行这些命令:

 docker build -t hello-world docker run -p 3000:3000 

在这之后(并且全部成功),当我尝试访问http:// localhost:3000 /时 ,页面未find。

我需要一些帮助来理解我的错误。

感谢所有

您应该login到您的运行容器:

 docker exec -it <container-id> /bin/bash 

进入容器后,你可以在那里运行你的应用程序,直接在容器内进行debugging。

就像是:

 cd your-directory/hello-world 

然后:

 node app.js 

控制台会吐出像本地开发环境的错误。

根据您的docker安装,它可能不在本地主机上。 我的安装例如在127.0.0.1上不可用,但在192.168.99.100上不可用。

以下hostsentry:

192.168.99.100 dockerhost

将允许我通过http:// dockerhost:3000 /

希望有帮助:-)