How to connect Docker Machine to any docker host

Docker Machine lets you to connect to external docker hosts and run docker commands inside any of these hosts from a single central console.

It is also used by Docker Toolbox to offer docker functions to a machine which doesn’t meet the requirenments to use docker natively:

  • Docker Toolbox will install a VirtualBox virtual machine named default
  • and will install and connect Docker Machine to this default virtual machine.

This way, you will be able to execute docker commands in your machine (even though these docker commands will be executed inside the default virtual machine).

The main problem with Docker Machine is that it is not easy to connect to any generic docker host unless you have the appropriate drivers for this host. I found this problem when I decided to replace the standard default virtual machine provided by Docker Toolbox because it was not good enough for my needs. So, I wonder myself:

How can I create a new virtual machine and connect and use it with Docker Machine?

To answer this question, I needed to search a lot. Because I didn’t find any good documentation. I found lots of comments and tips … which didn’t work for me most of them. But, at last, I figured out how to do it. Let me share with you.

In order to connect Docker Machine to a non standard machine you must execute a command like this:

Where:

  • X.X.X.X must be the ip of the docker host you want to connect to.
  • private_user is a user inside this docker host with special privileges (we will talk about it later).
  • private_key is a private key which can let this user private_user ssh log into the docker host.
  • name is the name you want Docker Machine will use to identify this new connection.

In order Docker Machine to connect to our docker host, the provided user private_user must meet some specific requirenments:

  • It must be part of the docker group inside the docker host (this way, it can run docker commands).
  • It must have granted sudo access inside the machine.
  • It must be able to run sudo commands without typing its password.
  • It must be able to ssh-log into the machine using its private key (that is, without typing its password).

The docker host must also meet some requirenments:

  • Its docker daemon must be reachable from the exterior using port 2376.

Create a Vagrant virtual machine and connect it to Docker Machine

In order to give an example, I am going to define a Vagrant virtual machine and I will connect it to Docker Machine.

Defining the Vagrant machine

The Vagrantfile will be this:

Let’s review this file:

  • Creates a new virtual machine based in Ubuntu 16.04.
  • The forwarded port is 2215.
  • It has its own private ip 192.168.2.15.
  • I have shared my C:\Users folder (it’s a Windows laptop) as /c/Users.
  • As the provider is Virtual Box, I have configured my virtual machine to have 4096Mb of memory and 2 cores.

The last part is the most interesting one. This is when I configure my virtual machine so it can be connected by Docker Machine:

  • I install the latest version of Docker.
  • As I know my virtual machine already has a sudo user vagrant, I will use it. I only need to add this user vagrant to the docker group.
  • I need to open port 2376 for docker. To do so, I copy the needed configuration file from the file provision/docker.service.
  • I also install docker-compose (as it is the version included with Ubuntu, it will not be the latest one, but it’s enough for my needs).

You must save this Vagrantfile inside a new folder (let’s name it vmubuntu). And inside this vmubuntu folder, you must create a subfolder provision with a single file docker.service inside:

This file is a copy of the original one but whith line 13 modified to open port 2376:

Opening port 2376

I honestly do not like the way I have used to open port 2376 by overwritting a system file (overwriting an existing system file). There are many tips to do it in a less invasive way … but none of them has worked for me.

Starting the Vagrant machine

Once you have your new vagrant virtual machine defined, you can start it:

The first time, it will take some time because it is provisioning the machine. But if everything is ok, you will be able to access you new virtual machine:

Connecting the new Vagrant machine to Docker Machine

Once you have your new virtual machine configured and running, you must execute the docker-machine create command. To do so:

  • Open a Windows console in the folder where you have you vagrant machine files.
  • There should be a subfolder .vagrant/machines/default/virtualbox.
  • Inside this subfolder there should be a file private_key. This file, is the private key of the vagrant user inside our virtual machine.

Now, we can create the connection inside the docker console:

Where

  • 192.168.2.15 is the ip we have previously assigned to our new machine in our Vagrantfile file.
  • .vagrant/machines/default/virtualbox/private_key is the file with the private key of user vagrant (we already checked it exists there).
  • vagrant is the user we are going to use.
  • vmbuntu is the name we are going to give to this connection.

The output should be something similar to:

create new docker machine

Finally, we can confirm docker-machine is connected to our new virtual machine:

And we can log into our virtual machine via docker-machine ssh command:

We have connected our new vmubuntu docker host to docker machine. And now we can use all docker-machine commands to manage it.