Docker VirtualBox

How to Setup Docker Machine with VirtualBox

Docker Machine is a tool to connect, use and monitor multiple Docker hosts hosted on VirtualBox, VMware, Hyper-V, DigitalOcean, OpenStack, Google Cloud, Amazon EC2, Microsoft Azure etc. from a single computer with Docker installed.

You can also use Docker Machine to quickly create as many Docker hosts/machines as you need for learning Docker.

In this article, I am going to show you how to install Docker Machine and use Docker Machine with VirtualBox – the open source virtualization platform. I will be using Ubuntu 18.04 LTS for the demonstration in this article. But any modern Linux distribution with Docker and VirtualBox installed should work.

Prerequisites:

In order to successfully follow this article, you must have,

  • A computer with any modern Linux distribution installed.
  • Docker installed on your computer.
  • VirtualBox installed on your computer.
  • curl installed on your computer.

linuxhint.com has many articles written on these topics. I recommend you take a look at them if you need any help.

Installing Docker Machine:

Before you install Docker Machine, make sure you have VirtualBox and Docker installed on your computer.

As you can see, I have VirtualBox 6 and Docker 18.09 installed on my Ubuntu 18.04 LTS machine.

NOTE: Docker Machine will work without Docker, but you won’t be able to use every feature of Docker Machine.

Now, install Docker Machine on your Linux computer with the following command:

$ base=https://github.com/docker/machine/releases/download/v0.16.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
sudo install /tmp/docker-machine /usr/local/bin/docker-machine

As you can see, the Docker Machine binary is being downloaded. It may take a while to complete.

Docker Machine should be downloaded and installed.

Now, check whether Docker Machine is working with the following command:

$ docker-machine --version

As you can see, Docker Machine is working correctly.

You should install the Docker Machine bash auto completion scripts. To do that, run the following command:

base=https://raw.githubusercontent.com/docker/machine/v0.16.0
for i in docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash
do
sudo wget "$base/contrib/completion/bash/${i}" -P /etc/bash_completion.d
done

The bash auto completion scripts for Docker Machine should be installed.

Now, close the terminal and open it again. Then, try to see if Docker Machine auto completion works.

As you can see, it is working really well.

Creating Docker Hosts/Machines:

Now, you can create Docker hosts/machines using Docker Machine.

To create a new Docker machine, run the following command:

$ docker-machine create --driver=virtualbox default

Here, default is the name of the Docker machine. You can call it whatever you like.

Docker machine will download the Boot2Docker ISO file from the internet. So, it will take a while to complete.

The Docker machine default should be created.

The Boot2Docker ISO image will be cached on your operating system. So, the next time you create a new Docker machine, it won’t have to download it again.

Listing All the Available Docker Machines:

You can list all the Docker machines you’ve created so far with the following command:

$ docker-machine ls

All the Docker machines you’ve created including the default Docker machine should be listed. The default Docker machine is using the virtualbox driver as you can see in the screenshot below.

Using Docker Machines:

In order to use a Docker machine, you have to activate it. Once the Docker machine is activated, you can run Docker commands as usual. Once you’ve finished your work, you can either activate another Docker machine, or deactivate it.

You can also connect to your Docker machine via SSH and run your desired Docker commands there. Once you’re done with your work, just exit out of the shell as you usually do.

In the next sections, I am going to show you how to activate and deactivate Docker machines and connect to Docker machines via SSH.

Activating Docker Machines:

You can activate the Docker machine default by souring the environment variables of default with the following command:

$ docker-machine use default

The default Docker machine should be activated.

You can also activate your Docker machine default as follows:

$ eval $(docker-machine env default)

Now, you can run any Docker command as you want.

Checking Which Docker Machine is Active:

You can check which Docker machine is activated with the following command:

$ docker-machine active

As you can see, the name of the active Docker machine is default.

Deactivating an Active Docker Machine:

Once you’re done working with a particular Docker machine, you can deactivate it with the following command:

$ docker-machine use -u

You can also use the following command to deactivate your active Docker machine:

$ eval $(docker-machine env -u)

Connecting to Docker Machines via SSH:

Let’s say, you want to connect to the Docker machine default via SSH. To do that, run the following command:

$ docker-machine ssh default

You should be connected to the Docker machine default.

Now, you can run any Docker command as you want there.

Once your work is complete, close the SSH connection with the following command:

$ exit

Starting and Stopping Docker Machines:

Docker machines runs as a VirtualBox virtual machine as you’re using the VirtualBox Docker Machine driver. So, it uses up your system memory (RAM). You may not want to run all the Docker machines at the same time. Instead, run only the machines you need. Luckily, you can start and stop Docker machines.

To stop a running Docker machine default, run the following command:

$ docker-machine stop default

As you can see, the Docker machine default is stopped.

Now, if you want to start the Docker machine default again, run the following command:

$ docker-machine start default

As you can see, the default Docker machine is running again.

Printing the IP Address of Docker Machines:

You need the IP address of the Docker machine when you want to connect to some services running on the containers hosted on that particular Docker machine. Luckily, you can print only the IP address of the Docker machine you want.

Let’s say, you want to find out the IP address of the Docker machine default. To do that, run the following command:

$ docker-machine ip default

The IP address should be printed on the screen as shown below.

Removing Docker Machines:

You can remove the Docker machines that you don’t need anymore.

To remove the Docker machine default, run the following command:

$ docker-machine rm default

Now, press y and then <Enter> to confirm.

The Docker machine default should be removed.

So, that’s basically how you setup Docker Machine with VirtualBox and use it. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.