Learn how to create & manage Docker Hosts on VirtualBox with Docker Machine. Docker Machine is a tool that lets us install Docker Engine on virtual hosts on various platforms, on the local environment as well on the cloud. With Docker Machine, we can easily install docker hosts a Windows machine, a MAC machine, or on cloud platforms like AWS, AZURE, etc.

As we know that Docker Engine runs natively on Linux machines, so it's pretty easy to get Docker working on Linux machines but for other OS like MAC or Windows, this was a problem. So Docker Machine was introduced, especially for older versions of MAC & Windows (there are new native Docker apps for the latest OS). Not meant only for Windows or MAC, Docker machine also provides an efficient way to provision multiple Docker hosts on local as well as a cloud network.

To manage the docker hosts, we use rather than using the 'docker' command, which is used with the docker engine. In this tutorial, we will discuss how we can create & manage docker hosts with Docker Machine on VirtualBox.

(Recommended Read: Step by Step guide for creating Docker Image)


Pre-requisites

1- We need to have VirtualBox installed on our machines. To install VirtualBox, kindly refer to our detailed article on “Installing VirtualBox on CentOS & Ubuntu”.

2- We should also have docker installed on our system. To install Docker on your system, refer to the article “Create your first Docker Container


Installing Docker Machine

Before creating a Docker host on VirtualBox, we will install the Docker Machine on our system. To install the Docker machine, we will download its binary file using the following command,

$ curl -L https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine

Next provide execute permissions to the folder,

$ chmod +x /tmp/docker-machine

and lastly copy the downloaded folder to /usr/local/bin/.

$ cp /tmp/docker-machine /usr/local/bin/docker-machine

Docker machine has now been installed, we can test if the command is working or not by checking its version,

$ docker-machine -version

docker machine

Creating a Docker Host on VirtualBox

Now that we have docker-machine installed on our system, we will use it to create a docker host on VirtualBox. Execute the following command on your terminal to create a docker host for VirtualBox,

$ docker-machine create --driver virtualbox docker-local

The process will take some time.

docker machine

Once installed, open VirtualBox & we can also see the Docker host installed there,

docker machine with virtualbox

Now to connect to Docker Host & to get all information regarding the host, run

$ docker-machine env docker-local

& you will get the following output on your terminal,

docker on virtualbox

Next, execute the following command to configure the shell,

$ eval $(docker-machine env docker-local)

Now all the docker commands that we run on our machine will actually be executed on VM machine & not on local machine.
To connect to the ssh session of docker host (docker-local), execute the following command,

$ docker-machine ssh docker.local

docker on virtualbox

Our VirtualBox Docker Host is now ready & we can manage docker hosts with the docker-machine command.


Docker-machine commands

Now let's discuss some useful docker commands,

List all docker-machine hosts-                    docker-machine ls
Show the host status –                                 docker-machine status docker-local
Get URL for the host –                                    docker-machine URL docker-local
Start a host –                                                 docker-machine start docker-local
Stop a host-                                                   docker-machine stop docker-local
Restart a host-                                               docker-machine restart docker-local
Remove a host-                                             docker-machine rm docker-local
Detailed information about the host-          docker-machine inspect docker-local

These were only some of the important commands used with docker-machine. For the complete list of commands, run

$ docker-machine --help

With this we complete our tutorial on how to create & manage Docker hosts on Virtualbox with Docker Machine, please feel free to send in your queries & questions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.