Add existing VM/Host to docker-machine
I use Docker a lot in my development lifecycle and like to manage my instances with Docker Machine, a powerful tool for provisioning and managing your Dockerized instances with docker-machine
commands.
Docker Machine has a driver plugin architecture and the generic driver is useful if you are using a provider that it does not support directly or if you would like to import an existing host. This is how I go about adding my existing host(s).
Add newuser
Use adduser
to add new user on remote host.
$ adduser $USER
Add password-less sudo privileges
Use visudo
to add new configuration file on remote host.
$ sudo visudo -f /etc/sudoers.d/passwdless-users
and add $USER ALL=(ALL) NOPASSWD:ALL
. Save and exit.
Configure Key-based Authentication
On your localhost, generate a SSH key pair.
$ ssh-keygen -b 4096
Copy your public key to remote host using ssh-copy-id
.
$ ssh-copy-id $USER@REMOTE_HOST
Create machine
To create a machine, specify --driver generic
, the IP address or DNS name of the host and the path to the SSH private key authorized to connect to the host.
$ docker-machine create --driver generic \
--generic-ip-address REMOTE_HOST_IP \
--generic-ssh-user $USER \
--generic-ssh-key ~/.ssh/id_rsa \
REMOTE_HOST
When the machine is created, Docker generates a unique SSH key and stores it in ~/.docker/machines
. This is used under the hood to access the host directly with the docker-machine ssh
command.
To connect your Docker client to the Docker Engine running on this instance, run:
eval $(docker-machine env REMOTE_HOST)
E.g, docker-machine inspect REMOTE_HOST
lists the machine details.
To disconnect, run:
eval $(docker-machine env -u)
Hope someone finds this helpful.
Leave a comment