Skip to main content

Docker Machine

12.1. I heard my life would be better with Docker Machine​

Absolutely!

With Docker Machine you can start your Docker project with just one command!

Before Docker Machine, if we wanted to set up a Docker Host, it was necessary to install the operating system, install and configure Docker, and other tools that were needed.

We would waste valuable time on these steps, when we could already be working effectively with Docker and its containers.

However, everything changed with Docker Machine! With it, you can create your Docker Host with just one command. Docker Machine can work with the main VM hypervisors, such as VMware, Hyper-V, and Oracle VirtualBox, and also with the main infrastructure providers, such as AWS, Google Compute Engine, DigitalOcean, Rackspace, Azure, etc.

To access all the drivers that Docker Machine supports, visit: https://docs.docker.com/machine/drivers/.

When you use Docker Machine to install a Docker Host on AWS, for example, it will provide a machine with Linux and Docker and its dependencies already installed.

12.1.1. Let's install it?​

Installing Docker Machine is quite simple, so let's stop talking and get to work! It's worth remembering that it's possible to install Docker Machine on Linux, MacOS, or Windows.

To install Docker Machine on Linux, do:

# curl -L https://github.com/docker/machine/releases/download/v0.12.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine
# chmod +x /tmp/docker-machine
# sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

To proceed with the installation on MacOS:

curl -L https://github.com/docker/machine/releases/download/v0.15.0/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine
chmod +x /usr/local/bin/docker-machine

To proceed with the installation on Windows if you're using Git bash:

if [[ ! -d "$HOME/bin" ]]; then mkdir -p "$HOME/bin"; fi
curl -L https://github.com/docker/machine/releases/download/v0.15.0/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe"
chmod +x "$HOME/bin/docker-machine.exe"

To check if it was installed and what version it is, do:

root@linuxtips:~# docker-machine version
docker-machine version 0.15.0, build b48dc28

root@linuxtips:~#

Done. Like everything made by Docker, it's simple to install and easy to operate.

12.1.2. Let's start our first project?​

Now that we have Docker Machine installed on our machine, we can install the Docker Host quite simply -- remembering that even if we had installed Docker Machine on Windows, we could easily command the installation of Docker Hosts on AWS on Linux machines. Keep in mind that the machine on which you installed Docker Machine is the maestro that determines the creation of new Docker Hosts, whether on VMs or on some cloud like AWS.

In our first project, we'll have Docker Machine install the Docker Host using VirtualBox.

Since we'll be using VirtualBox, it's obvious that we need to have VirtualBox installed on our machine for everything to work.

Therefore:

root@linuxtips:~# apt-get install virtualbox

To install a new Docker Host, we use the "docker-machine create" command. To choose where we'll create the Docker Host, we use the "--driver" parameter, as follows:

root@linuxtips:~# docker-machine create --driver virtualbox linuxtips
Running pre-create checks...
(linuxtips) Default Boot2Docker ISO is out-of-date, downloading the latest release...
(linuxtips) Latest release for github.com/boot2docker/boot2docker is v17.05.0-ce
(linuxtips) Downloading /Users/linuxtips/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v17.05.0-ce/boot2docker.iso...
(linuxtips) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(linuxtips) Copying /Users/linuxtips/.docker/machine/cache/boot2docker.iso to /Users/linuxtips/.docker/machine/machines/linuxtips/boot2docker.iso...
(linuxtips) Creating VirtualBox VM...
(linuxtips) Creating SSH key...
(linuxtips) Starting the VM...
(linuxtips) Check network to re-create if needed...
(linuxtips) Waiting for an IP...
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 boot2docker...
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 linuxtips

root@linuxtips:~#

Where:

  • docker-machine create -- Creates a new Docker Host.

  • --driver virtualbox -- Will create it using VirtualBox.

  • linuxtips -- Name of the VM that will be created.

To view the host you just created, simply type the following command:

root@linuxtips:~# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
linuxtips - virtualbox Running tcp://192.168.99.100:2376 v18.06.0 -

As we can see, our host is running perfectly! Notice that we have a column called URL, right? In it we have the URL so we can communicate with our new host.

Another way to view information about the host, more specifically about its environment variables, is to type:

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

root@linuxtips:~#

All the host's environment variables will be displayed, such as URL, certificate, and name.

To access the environment of this host we just created, do:

root@linuxtips:~# eval "$(docker-machine env linuxtips)"

The "eval" command is used to define environment variables from the output of a command, that is, the variables we see in the output of "docker-machine env linuxtips".

Now that we're in the environment of the host we created, let's view the running containers:

root@linuxtips:~# docker container ls

Of course, we don't have any containers running yet; let's start our first one now:

root@linuxtips:~# docker container run busybox echo "LINUXTIPS, VAIIII"
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
385e281300cc: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:4a887a2326ec9e0fa90cce7b4764b0e627b5d6afcb81a3f73c85dc29cea00048
Status: Downloaded newer image for busybox:latest
LINUXTIPS, VAIIII

root@linuxtips:~#

As we can see, the container was executed and printed the message "LINUXTIPS, VAIIII", as we requested.

Remember that the container was executed on our Docker Host, which we created through Docker Machine.

To check the IP of the host we created, do:

root@linuxtips:~# docker-machine ip linuxtips
192.168.99.100

root@linuxtips:~#

To access our host, we use the "ssh" parameter, passing the name of the host we want to access:

root@linuxtips:~# docker-machine ssh linuxtips

To know more details about the host, we can use the "inspect" parameter:

root@linuxtips:~# docker-machine inspect linuxtips
{
"ConfigVersion": 3,
"Driver": {
"IPAddress": "192.168.99.100",
"MachineName": "linuxtips",
"SSHUser": "docker",
"SSHPort": 57249,
"SSHKeyPath": "/Users/jeferson/.docker/machine/machines/linuxtips/id_rsa",
"StorePath": "/Users/jeferson/.docker/machine",
"SwarmMaster": false,
"SwarmHost": "tcp://0.0.0.0:3376",
"SwarmDiscovery": "",
"VBoxManager": {},
"HostInterfaces": {},
"CPU": 1,
"Memory": 1024,
"DiskSize": 20000,
"NatNicType": "82540EM",
"Boot2DockerURL": "",
"Boot2DockerImportVM": "",
"HostDNSResolver": false,
"HostOnlyCIDR": "192.168.99.1/24",
"HostOnlyNicType": "82540EM",
"HostOnlyPromiscMode": "deny",
"UIType": "headless",
"HostOnlyNoDHCP": false,
"NoShare": false,
"DNSProxy": true,
"NoVTXCheck": false,
"ShareFolder": ""
},
"DriverName": "virtualbox",
"HostOptions": {
"Driver": "",
"Memory": 0,
"Disk": 0,
"EngineOptions": {
"ArbitraryFlags": [],
"Dns": null,
"GraphDir": "",
"Env": [],
"Ipv6": false,
"InsecureRegistry": [],
"Labels": [],
"LogLevel": "",
"StorageDriver": "",
"SelinuxEnabled": false,
"TlsVerify": true,
"RegistryMirror": [],
"InstallURL": "https://get.docker.com"
},
"SwarmOptions": {
"IsSwarm": false,
"Address": "",
"Discovery": "",
"Agent": false,
"Master": false,
"Host": "tcp://0.0.0.0:3376",
"Image": "swarm:latest",
"Strategy": "spread",
"Heartbeat": 0,
"Overcommit": 0,
"ArbitraryFlags": [],
"ArbitraryJoinFlags": [],
"Env": null,
"IsExperimental": false
},
"AuthOptions": {
"CertDir": "/Users/jeferson/.docker/machine/certs",
"CaCertPath": "/Users/jeferson/.docker/machine/certs/ca.pem",
"CaPrivateKeyPath": "/Users/jeferson/.docker/machine/certs/ca-key.pem",
"CaCertRemotePath": "",
"ServerCertPath": "/Users/jeferson/.docker/machine/machines/linuxtips/server.pem",
"ServerKeyPath": "/Users/jeferson/.docker/machine/machines/linuxtips/server-key.pem",
"ClientKeyPath": "/Users/jeferson/.docker/machine/certs/key.pem",
"ServerCertRemotePath": "",
"ServerKeyRemotePath": "",
"ClientCertPath": "/Users/jeferson/.docker/machine/certs/cert.pem",
"ServerCertSANs": [],
"StorePath": "/Users/jeferson/.docker/machine/machines/linuxtips"
}
},
"Name": "linuxtips"
}

To stop the host we created:

root@linuxtips:~# docker-machine stop linuxtips

To view the status of your Docker host, type:

root@linuxtips:~# docker-machine ls

To start it again:

root@linuxtips:~# docker-machine start linuxtips

To remove it permanently:

root@linuxtips:~# docker-machine rm linuxtips
Successfully removed linuxtips