After learning how to install Docker on Linux Mint 18 and 18.1 using this article and using this other one for learning how to install Docker Machine on the same distribution, the next logical step is to use that Docker Machine to provision Dockerized hosts, or machines, on supported Cloud platforms.
And that is the subject of this article – How to use Docker Machine to provision Dockerized hosts on a supported Cloud hosting provider. For this article, we’ll be provisioning the servers on the DigitalOcean platform, which offers cheap servers with SSD storage.
For this to work, you’ll need to have a DigitalOcean account. To sign up you may use this link. It’s an affiliate link, so using it to sign up benefits me and this site. You’ll also need to generate a DigitalOcean API token, which will be used for authentication when provisioning a machine. Use this DigitalOcean article to learn how to generate one.
Create a Bash Variable for Your DigitalOcean API Token
The DigitalOcean API token is a long string that can be difficult to work with on the command line. To make things easier, it’s better to assign it to a Bash variable. And the best method of doing that is to add it to the .bashrc file under your home directory. Use the following commands to accomplish that.
# Use these commands to assign your DigitalOcean API token to a Bash variable # Open the .bashrc file nano ~/.bashrc # Append the next line to the file # Replacewith the one you generated export AUTH-TOKEN= # Assuming the token is db5b297b2th0f5897b09, the actual command would be export AUTH-TOKEN=db5b297b2th0f5897b09 # Save and close the file # To activate the changes, type source ~/.bashrc # To check that the variable is recognized by the system, type echo $AUTH-TOKEN # The output should be your DigitalOcean API token #
Provision One Dockerized Host from Linux Mint 18/18.1
At this point, you may start provisioning machines using the docker-machine command. And we’ll be doing that on the DigitalOcean platform. If you have VirtualBox installed, you can also provision machines locally. The command you need to provision your first machine is given below:
# Docker Machine command to create a machine on DigitalOcean # This command creates a machine called docker-node. You may replace that with your own name docker-machine create -d digitalocean --digitalocean-access-token $AUTH-TOKEN docker-node #
It will take about three minutes, but after that, you’ll have a server on DigitalOcean with the latest Docker Engine installed. And though the last line of the output will tell you how to connect your Docker client to the remote machine, the best method is to employ the docker-machine use command. By default, that machine will be running the latest Ubuntu LTS version, which is Ubuntu 16.04 at the time of this writing. It will also be the smallest DigitalOcean Droplet, with 512 MB of RAM.
You can instruct Docker Machine to use an operating system other than Ubuntu 16.04, and even specify the size of the Droplet, that is, how much RAM it should be allocated.
Provision Multiple Dockerized Host from Linux Mint 18/18.1
Provisioning one Dockerized host is nice, but the command can be extended with a little Bash magic to provision as many as you can afford to pay for. For example, the commands you may use to provision two or more machines are given next.
# This command will provision just two machines # And they will be named node-1 and node-2 respectively for i in 1 2 ; do docker-machine create -d digitalocean --digitalocean-access-token $AUTH-TOKEN node-$i ; done # To provision, say, 10 machines automatically, use this variation of the command for i in {1..10} ; do docker-machine create -d digitalocean --digitalocean-access-token $AUTH-TOKEN node-$i ; done #
With those commands, you’ve now seen how you can provision hundreds, even thousands of Dockerized hosts with a single command.
Managing Dockerized Hosts from Linux Mint 18/18.1
With Docker Machine, managing Dockerized hosts is not complicated. In this section, you’ll become familiar with basic commands you’ll need to work with it on Linux Mint 18/18.1. The simplest command you’ll be using regularly is docker-machine ls, which is used to list provisioned machines. The output is given in the following code block.
# The following is the output of docker-machine ls command NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS dockerNode-1 - digitalocean Running tcp://104.236.29.236:2376 v1.12.4 dockerNode-2 - digitalocean Running tcp://104.131.76.56:2376 v1.12.4 # And this is the same output when one of the machines has been activated # Notice that the active machine has an asterisk in its ACTIVE column NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS dockerNode-1 * digitalocean Running tcp://104.236.29.236:2376 v1.12.4 dockerNode-2 - digitalocean Running tcp://104.131.76.56:2376 v1.12.4 #
So how do you activate a machine? By using the following command. Note that when a machine is activated, your Docker client is connected to it, so that all Docker commands (not Docker Machine commands) you type are executed on it.
# How to activate a machine # The command takes this form docker-machine use# For example, to activate a machine named dockerNode-1, type docker-machine use dockerNode-1 #
If you installed Docker Machine using this article, activating a machine will include its name in your command prompt. That makes it easy to keep track of which machine you’re working on when you have multiple machines.
A Dockerized host is just like any other server, so you can log into it using SSH and perform all the administrative tasks you need to. You can also log into it using the docker-machine ssh command. And with a variation of the same command, it’s possible to execute any command on the machine and perform the same administrative tasks without actually logging into it. Below are a few example commands you can execute on a remote machine using the docker-machine ssh command.
# The syntax of the 'docker-machine ssh' command takes this form docker-machine ssh# For example, to log into a machine named node-1, type docker-machine ssh node-1 # To execute a command on the same host, append the command to the above, like this docker-machine ssh node-1 apt update # While we are at it, upgrade the system docker-machine ssh node-1 apt upgrade -y # Docker commands are fair game too docker-machine ssh node-1 docker info # Or docker-machine ssh node-1 docker images # Are there any containers running on the remote machine? docker-machine ssh node-1 docker ps #
Running Containers on Remote Machines form Linux Mint 18/18.1
The whole point of provisioning a server with Docker Engine installed on it is to be able to run containers on it. As you saw in the previous section, with Docker Machine, you can do that without actually logging into the machine in the traditional way. The following commands show how to run containers on a remote machine using the docker-machine ssh command.
# To run the hello-world container on the remote machine, type docker-machine ssh node-1 docker run hello-world # Now if you query the system, it will show one inactive container docker-machine ssh node-1 docker ps -a # Since the container has outlived its usefulness, trash it docker-machine ssh node-1 docker rm# And while we're at it, trash the image also? docker-machine ssh node-1 docker rm hello-world #
Cleaning Up
After provisioning and test-driving one or more Dockerized hosts, there will come a time when you’ll need to stop and/or destroy them. This last set of commands show how to go about it.
# Use this command to stop a machine docker-machine stop# Want to destroy it? Use the next command docker-machine rm # This will stop more than one machine for i in {1..10}; do docker-machine stop node-$i; done # And this will forcefully destroy them for i in {1..10}; do docker-machine rm -f node-$i; done #
Resources
The foregoing has shown how to provision Dockerized hosts, or machines, on the DigitalOcean platform from your Linux Mint 18 or 18.1 desktop. DigitalOcean is just one of many Cloud platforms supported by Docker Machine out of the box. The complete list of core drivers are available here. In a future article, we’ll show how to perform the same operations on the Vultr Cloud platform.