How to Setup Docker Machine with VirtualBox

29/12/2020
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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Docker Image vs Container

Understanding the process Docker uses to store data through images and containers will help you better design your Docker...
28/12/2020

Using Dockerfile to Expose Ports

It is really easy to create a custom Docker image from existing Docker images using Dockerfile. Usually people uses a minimal...
29/12/2020

How to Use Docker Registry

Docker is a technology which allows one to create virtual machines which encapsulate applications and all its dependences...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024