Mac OS X上的Vagrant,Docker和Node.js

问题描述:

我一直在这里对着墙壁敲打我的头,因此感谢您的帮助。我在Mac OS X上运行Vagrant,我想用一个简单的node.js'hello world'服务器运行Docker容器。我希望能够从我的浏览器访问此服务器。Mac OS X上的Vagrant,Docker和Node.js

这是我Vagrantfile.proxy(用于运行服务器泊坞窗):

VAGRANTFILE_API_VERSION = "2" 

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 
    config.vm.box = "hashicorp/precise64" 
    config.vm.provision "docker" 
    config.vm.provision "shell", inline: 
    "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill" 

    config.vm.network :forwarded_port, guest: 8888, host: 8888 
end 

这里是我的Vagrantfile:

# -*- mode: ruby -*- 
# vi: set ft=ruby : 

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! 
VAGRANTFILE_API_VERSION = "2" 

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 

    # Tells vagrant to build the image based on the Dockerfile found in 
    # the same directory as this Vagrantfile. 
    config.vm.define "nodejs" do |v| 
    v.vm.provider "docker" do |d| 
     d.build_dir = "." 
     d.ports = ['8888:8888'] 
     d.vagrant_vagrantfile = "./Vagrantfile.proxy" 
    end 
    end 

    config.vm.network :forwarded_port, host: 8888, guest: 8888 
end 

这里是我的Dockerfile:

FROM ubuntu:14.04 
MAINTAINER Sean 

RUN apt-get update 
RUN apt-get install -y python-software-properties python 
RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ precise universe" >> /etc/apt/sources.list 
RUN apt-get update 
RUN apt-get install -y nodejs 
RUN mkdir /var/www 

ADD app.js /var/www/app.js 

CMD ["/usr/bin/nodejs", "/var/www/app.js"] 

EXPOSE 8888 

而且这里id app.js(node.js简单服务器):

var http = require('http'); 
http.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('Hello World\n'); 
}).listen(8888, '127.0.0.1'); 
console.log('Server running at http://127.0.0.1:8888/'); 

我使用'vagrant up --provider =“docker”'启动系统。然后我检查服务器是否开始使用'vagrant docker-logs',它显示控制台输出。然后我尝试

'curl http://localhost:8888' 

但我每次都得到'curl:(52)来自服务器的空回复'。

我错过了什么?

问题是我试图在127.0.0.1而不是0.0.0.0上运行nodejs服务器。完整的修订来源可在here找到。