search by tags

for the user

adventures into the land of the command line

docker machine, what are you?

… ahhh I see, you’re like virtualenv, or rbenv, or nvm or whatever. But for the docker daemon running on remote (or even local) hosts.

So how does it work? When you create a machine, it sets some env vars:

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://127.0.0.1:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/mylocalmachine"
export DOCKER_MACHINE_NAME="mylocalmachine"

These env vars are used by the docker daemon to determine where to run its commands. One other thing to note is that if you set some remote computer to be a ‘docker machine’ and it does not yet have a docker daemon installed, docker machine will install the docker daemon as part of setting it up as a docker machine.

Install docker-machine

$ curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine && chmod +x /tmp/docker-machine && sudo cp /tmp/docker-machine /usr/local/bin/docker-machine
$ docker-machine version
docker-machine version 0.12.2, build 9371605

Create some ssh keys and access stuffs

$ ssh-keygen -t rsa
$ ssh 127.0.0.1

Ensure the key is present in ~/.ssh/authorized_keys and the host is in the ~/.ssh/known_hosts file

Create a machine

$ docker-machine create --driver generic --generic-ip-address=127.0.0.1 --generic-ssh-key /root/.ssh/id_rsa --generic-ssh-user root mylocalmachine
Running pre-create checks...
Creating machine...
(mylocalmachine) Importing SSH key...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(upstart)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env mylocalmachine

Show machines

$ docker-machine ls
NAME             ACTIVE   DRIVER    STATE     URL                    SWARM   DOCKER        ERRORS
mylocalmachine   -        generic   Running   tcp://127.0.0.1:2376           v17.06.0-ce

Show machine environment variables

$ docker-machine env mylocalmachine
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://127.0.0.1:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/mylocalmachine"
export DOCKER_MACHINE_NAME="mylocalmachine"
# Run this command to configure your shell:
# eval $(docker-machine env mylocalmachine)

Switch to the machine named “mylocalmachine”

$ eval $(docker-machine env mylocalmachine)
$ docker-machine ls
NAME             ACTIVE   DRIVER    STATE     URL                    SWARM   DOCKER        ERRORS
mylocalmachine   *        generic   Running   tcp://127.0.0.1:2376           v17.06.0-ce

Create another machine

$ docker-machine create --driver generic --generic-ip-address=127.0.0.1 --generic-ssh-key /root/.ssh/id_rsa --generic-ssh-user root mylocalmachine2
$ docker-machine ls
NAME              ACTIVE   DRIVER    STATE     URL                    SWARM   DOCKER        ERRORS
mylocalmachine    *        generic   Running   tcp://127.0.0.1:2376           v17.06.0-ce
mylocalmachine2   *        generic   Running   tcp://127.0.0.1:2376           v17.06.0-ce

Switch to the second machine

$ eval $(docker-machine env mylocalmachine2)

Remove a machine

$ docker-machine rm mylocalmachine
About to remove mylocalmachine
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed mylocalmachine

$ docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS