如何从Vagrant VM中访问Docker容器

时间:2014-06-06 21:22:44

标签: vagrant docker vagrantfile boot2docker

我有一个Vagrant设置,它提供了两个Docker容器。 Docker容器启动没有问题。在Vagrant VM中,我可以运行docker ps并看到两个容器都连接到正确的端口。如果我运行docker logs XXX,我看到我的Redis和Mongo守护进程都在运行。但是从Vagrant盒子本身我无法telnet或访问任何正在运行的服务。我总是拒绝连接。如何让Vagrant VM看到在Docker中运行的正在运行的服务?

我的Vagrantfile看起来像是:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.host_name = "trusty"

  config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
  config.vm.box = "trusty64"

  config.vm.network :private_network, ip: "192.168.10.12"
  config.vm.network :forwarded_port, guest: 6379, host: 6379
  config.vm.network :forwarded_port, guest: 27017, host: 27017

  config.vm.provision "docker" do |d|
    d.pull_images 'dockerfile/redis'
    d.pull_images 'dockerfile/mongodb'

    d.run 'dockerfile/redis', ports: ['6379:6379'], name: 'redis', expose: [6379]
    d.run 'dockerfile/mongodb', ports: ['27017:27017'], name: 'mongodb'
  end
end 

1 个答案:

答案 0 :(得分:6)

Docker provisioner的Vagrant文​​档中,没有提及ports。 我建议你改用args

替换

d.run 'dockerfile/redis', ports: ['6379:6379'], name: 'redis', expose: [6379]
d.run 'dockerfile/mongodb', ports: ['27017:27017'], name: 'mongodb'

d.run 'dockerfile/redis', args: "-p 6379:6379", name: 'redis'
d.run 'dockerfile/mongodb', args: "-p 27017:27017", name: 'mongodb'