Connect to Docker Daemon in Vagrant VM from host via Docker Client

Viewed 1267

I setup a Vagrant VM that contains Docker

Vagrant.configure("2") do |config|
  config.vm.define "node1" do |node1|
    node1.vm.box = "ubuntu/xenial64"
    node1.vm.provision :shell, path: "../docker-installation.sh"
    node1.vm.network "private_network", ip: "192.168.33.10"
  end
end

I also installed Docker on my host machine and from there I like to call Docker inside the VM.

My first attempt was to do it like this:

docker -H 192.168.33.10 info 

However, this outputs this message:

Cannot connect to the Docker daemon at tcp://192.168.33.10:2375. Is the docker daemon running?

I guess this is a problem with the connection itselfs as the docker daemon is running. I assume SSH has to be configured.

How I have to configure the Docker client on my host as I can connect to the Docker daemon in my Vagrant VM?

1 Answers

I am glad I figured this out. Maybe the answer is helpful for someone else.

  1. Lookup path of private_key via

    vagrant ssh-config
    
  2. Generate pem file from private_key

    openssl rsa -in path/private_key -outform pem > key.pem
    chmod 600 key.pem
    
  3. Add User to docker group:

    sudo usermod -aG docker $USER
    sudo su vagrant
    
  4. Open a SSL Tunnel (for details: https://sysadmins.co.za/forwarding-the-docker-socket-via-a-ssh-tunnel-to-execute-docker-commands-locally/)

    screen -S docker
    sudo ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -i /path/key.pem -NL 127.0.0.1:2375:/var/run/docker.sock vagrant@192.168.33.10
    
  5. Call Docker

    docker -H 127.0.0.1:2375 info
    
Related