【问题标题】:Mac OS X, Vagrant, Docker, Nginx, Node - how do ports play together?Mac OS X、Vagrant、Docker、Nginx、Node - 端口如何协同工作?
【发布时间】:2014-12-06 12:15:59
【问题描述】:

我有一个简单的 nginx、node.js 设置,我想迁移到 Vagrant、Docker 世界。我不确定如何处理 IP 和端口。

我的目标是在浏览器中看到我的Hello World,最好是在端口 80 上,只需调用我的主机 http://example.com

更新 我自己找到了解决方案 - 我在这里创建了一个简单的样板: https://github.com/ezmilhouse/docker 随意从这里继续。

app.js

var app = express();

app.route('*').all(function(req, res) {
  res.send('Hello World!');
});

app.listen(2000)

nginx.conf

upstream example.com {

  # using the vagrant private network IP (I guess?)
  # using the node port
  server 192.168.33.10:2000

}

server {

  # ports nginx server is listen to
  listen 80;
  listen 443;

  location / {

    # upstream proxy
    proxy_pass http://example.com;

    # ...

  }

}

Vagrantfile.proxy

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/trusty64"
  config.vm.provision "docker"

  # settimg a hostname that matches nginx upstream (I guess?)
  config.vm.hostname = "example.com"

  # setting a private network IP, node.js and nginx run on this IP (i guess?)
  config.vm.network "private_network", ip: "192.168.33.10"

end

流浪文件

  Vagrant.configure("2") do |config|

    config.vm.define "nginx" do |app|
      # forward the nginx port (I guess?)
      app.vm.network "forwarded_port", guest: 80, host: 80
      app.vm.provider "docker" do |d|
        d.build_dir  = "./docker/nginx"
        d.vagrant_vagrantfile = "./Vagrantfile.proxy"
      end
    end

    config.vm.define "node" do |app|
      # forward the node port (I guess?)
      app.vm.network "forwarded_port", guest: 2000, host: 2000
      app.vm.provider "docker" do |d|
        d.build_dir  = "./docker/node"
        d.vagrant_vagrantfile = "./Vagrantfile.proxy"
      end
    end

  end

/nginx/Dockerfile

# ...
EXPOSE ["80"]

/node/Dockerfile

# ...
EXPOSE ["2000"]

Mac OS X /etc/hosts

192.168.33.10 example.com

我希望通过 nginx 在浏览器中调用 http://192.168.33.10 以查看我的 Hello Worldhttp://192.168.33.10:2000 以查看节点实例。因此主机example.com 也不起作用。

我做错了什么?

【问题讨论】:

  • 你能试试 EXPOSE N 而不是 EXPOSE ["N"]?
  • 你能和你的代码分享一个 github repo 的链接吗?玩起来有点意思
  • @kharandziuk - github repo:github.com/ezmilhouse/docker
  • @seanmcl 证明你根本不需要暴露端口(至少在 Vagrant 设置中)
  • 我创建了一个带有 nginx-nodejs 设置的演示项目。看看你是否能从中得到灵感:github.com/materik/docker-web-backend-frontend-demo

标签: node.js nginx vagrant docker vagrantfile


【解决方案1】:

我创建了 fork 并将 pull request 创建到您的存储库中。

主要思想:不要对 docker 容器使用配置。

您可以在此article 中找到更多信息

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多