我正在研究 Docker,我正在努力思考为什么这对我不起作用。我已经阅读了很多关于如何设置它的文章和教程,我似乎已经准备好了一切,但我的实际应用程序只是没有显示在浏览器中(localhost:3001
)。我在我的 Mac 上使用最新版本的 Docker,运行 Mavericks,使用 boot2docker。我肯定有 boot2docker 运行,因为docker
命令运行良好,而且我没有得到任何似乎相关的错误。
超级简单的项目如下所示:
src/
..index.js
..package.json
Dockerfile
该src/index.js
文件如下所示:
var express = require('express'),
app = express();
app.get('/', function(req, res){
res.send('Hello world!');
});
app.listen(3001);
该src/package.json
文件如下所示:
{
"name": "node-docker-example",
"version": "0.0.1",
"description": "A NodeJS webserver to run inside a docker container",
"main": "index.js",
"dependencies": {
"express": "*"
}
}
该Dockerfile
文件如下所示:
FROM ubuntu:14.04
# make sure apt is up to date
RUN apt-get update
# install nodejs and npm
RUN apt-get install -y nodejs npm git git-core
# add source files
ADD /src /srv
# set the working directory to run commands from
WORKDIR /srv
# install the dependencies
RUN npm install
# expose the port so host can have access
EXPOSE 3001
# run this after the container has been instantiated
CMD ["nodejs", "index.js"]
有了所有这些,我就在本地构建它:
$ docker build -t me/foo .
没问题...然后我尝试了一些其他方法让它运行,但是这些都不起作用,在浏览器中查看时我看不到任何响应(localhost:3001
)
$ docker run -i -t me/foo
$ docker run -i -t -p 3001:3001 me/foo
$ docker run -i -t -p 127.0.0.1:3001:3001 me/foo
似乎没有任何效果,没有错误出现......好吧,除了localhost:3001
浏览器中的内容之外,什么都没有。
请帮我!我喜欢 docker 的想法,但我无法让最简单的东西运行。谢谢!