Search code examples
vagrantdockervagrantfileboot2docker

How to access Docker containers from within a Vagrant VM


I have a Vagrant setup that provisions two Docker containers. The Docker containers boot without issue. Within the Vagrant VM I can run docker ps and see both containers attached to the correct ports. If I run docker logs XXX I see that both my Redis and Mongo daemons are running. But from within the Vagrant box itself I can not telnet or access either of the running services. I always get a connection refused. How can I make the Vagrant VM see the running services running within Docker?

My Vagrantfile looks like:

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 

Solution

  • In the Vagrant documentation for the Docker provisioner there is no mention of ports. I suggest you use args instead.

    replace

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

    with

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