Search code examples
proxyvagrantvirtualboxdockerboot2docker

Why Can I not resolve docker.io and other hosts behind proxy from within Vagrant VM


I am running Vagrant with 2 docker containers inside and it works fine when not on a corporate proxy but When I am on the corporate proxy and try to run vagrant up I keep getting :

https://index.docker.io/v1/repositories/dockerfile/ubuntu/images: 
dial tcp: lookup index.docker.io: no such host

I have the proxy set inside vagrant using

VAGRANT_HTTP_PROXY="http://proxy.example.com:8080/" vagrant up

I also cannot get it to resolve hosts using nslookup within vagrant on the corporate proxy

I have tried including the following within the vagrantfile as suggested here:

config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end

I have also tried using a bridged connection, restarting the docker server as suggested here and swapping my DNS on my mac (which I saw suggested somewhere else but cannot find link)

I would really appreciate some help/direction, and in case its relavent I am running vargrant 1.6.5 on a macbook running OSX 10.9 with virtual box and this is my vagrant file:

$start = <<SCRIPT
#service docker stop
#HTTP_PROXY=http://proxy:8080/ docker -d &
service docker restart

#stop and remove any existing containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

echo "Building from Dockerfiles"
# Build containers from Dockerfiles
docker build -t sapvagrant/web_app /var/local/app/webApp
docker build -t sapvagrant/node /var/local/app/nodeService

echo "Running & linking containers"
# Run and link the containers
docker run -d --name node myvagrant/node
docker run -d -P -p 49166:80 --name web --link node:db myvagrant/web_app

docker start node
docker start web
SCRIPT

VAGRANTFILE_API_VERSION = "2"

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

=begin
    if Vagrant.has_plugin?("vagrant-proxyconf")
        config.proxy.http     = "http://proxy:8080/"
        config.proxy.https    = "http://proxy:8080/"
        config.proxy.no_proxy = "localhost,127.0.0.1"
    end
=end
    #config.vm.network :bridged
    config.vm.provider :virtualbox do |vb|
        #vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
        #vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    end

    # Port Forwarding
    config.vm.network "forwarded_port", guest: 49166, host: 3000

    # Ubuntu
    config.vm.box = "precise64"
    config.vm.box_url="http://files.vagrantup.com/precise64.box"

    # Install latest docker
    config.vm.provision "docker"

    config.vm.synced_folder ".", "/var/local/app" #, type: "nfs"
    config.vm.provision :shell, inline: "/etc/init.d/docker restart"

    #config.vm.provision "shell", inline: $setup

    config.vm.provision "shell", run: "always", inline: $start

end

Solution

  • It is not necessary to resolve names on the Internet because your proxy server resolves the names. But vagrant-proxyconf does not configure the proxy if no docker is installed. You have 2 ways to resolve this problem.

    Configure the proxy before docker provision

    Vagrant docker provisioner cannot change configuration file. (eg. /etc/default/docker, /etc/sysconfig/docker) You need to configure the proxy before provisioning with shell provisioner as follows.

      # If you use RHEL write to /etc/sysconfig/docker
      config_proxy = 'echo export http_proxy=$http_proxy >> /etc/default/docker'
      config.vm.provision('shell',
                          inline: config_proxy)
      config.vm.provision 'docker'
    

    I simplified your Vagrantfile.

    VAGRANTFILE_API_VERSION = '2'
    
    Vagrant.configure('2') do |config|
    
      if Vagrant.has_plugin?('vagrant-proxyconf')
        config.proxy.http     = 'http://proxy:8080/'
        config.proxy.https    = 'http://proxy:8080/'
        config.proxy.no_proxy = 'localhost,127.0.0.1'
      end
    
      config.vm.synced_folder '.', '/var/local/app'
    
      # Ubuntu
      config.vm.box = 'precise64'
      config.vm.box_url = 'http://files.vagrantup.com/precise64.box'
    
      # If you use RHEL write to /etc/sysconfig/docker
      config_proxy = 'echo export http_proxy=$http_proxy >> /etc/default/docker'
      config.vm.provision('shell',
                          inline: config_proxy)
      config.vm.provision 'docker' do |d|
        d.build_image '/var/local/app/webApp',      args: '-t sapvagrant/web_app'
        d.build_image '/var/local/app/nodeService', args: '-t sapvagrant/node'
        d.run 'sapvagrant/node',    args: '--name node'
        d.run 'sapvagrant/web_app', args: '-P -p 49166:80 --name web --link node:db'
      end
    end
    

    Use docker installed VM box

    Vagrant-proxyconf does not configure the proxy if no docker is installed. Vagrant-proxyconf configure the proxy if you use docker installed VM box.

    • Note:
      • Vagrant-proxyconf 1.4.0 restarts docker always, so sometimes docker containers can be broken.
      • Vagrant-proxyconf 1.4.2(not released on 22nd Dec. 2014) restarts docker only when the proxy configuration changed.

    You can find docker installed VM boxes on the ATLAS. I have found jess/ubuntu-precise-nginx-docker.

    I simplified your Vagrantfile.

    VAGRANTFILE_API_VERSION = '2'
    
    Vagrant.configure('2') do |config|
    
      if Vagrant.has_plugin?('vagrant-proxyconf')
        config.proxy.http     = 'http://proxy:8080/'
        config.proxy.https    = 'http://proxy:8080/'
        config.proxy.no_proxy = 'localhost,127.0.0.1'
      end
    
      config.vm.synced_folder '.', '/var/local/app'
    
      # Ubuntu
      config.vm.box = 'jess/ubuntu-precise-nginx-docker'
    
      config.vm.provision 'docker' do |d|
        d.build_image '/var/local/app/webApp',      args: '-t sapvagrant/web_app'
        d.build_image '/var/local/app/nodeService', args: '-t sapvagrant/node'
        d.run 'sapvagrant/node',    args: '--name node'
        d.run 'sapvagrant/web_app', args: '-P -p 49166:80 --name web --link node:db'
      end
    end