Sean
Sean

Reputation: 370

Vagrant, Docker, and Node.js on Mac OS X

I've been banging my head against the wall for an entire day on this, so thanks for your help. I am running Vagrant on Mac OS X, and I want to run a Docker container with a simple node.js 'hello world' server running inside. I want to be able to access this server form my browser.

Here is my Vagrantfile.proxy (for running the docker server):

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

Here is my 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

Here is my 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

And here id app.js (the node.js simple server):

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/');

I start the system using 'vagrant up --provider="docker"'. Then I check that the server started using 'vagrant docker-logs', which shows the console output. Then I try to

'curl http://localhost:8888'

but I get 'curl: (52) Empty reply from server' every time.

What am I missing?

Upvotes: 1

Views: 1471

Answers (1)

Sean
Sean

Reputation: 370

The problem is that I was trying to run the nodejs server on 127.0.0.1, rather than 0.0.0.0. The full, revised source can be found at here.

Upvotes: 1

Related Questions