Docker Machine Tutorial : Getting Started Guide

You all might have had that moment like “ Ahh man! I have to execute all these commands again!!”. And if you are that guy who hates to configure a docker host again and again, docker-machine is there for the rescue. So, you can leave all the installation and configuration tasks of docker to docker-machine.

Docker machine lets you spin up docker host VMs locally on your laptop, a cloud-provider (AWS, Azure etc) and your private data center (OpenStack, Vsphere etc). Not only docker host provisioning, using docker machine you can manage deploy and manage containers on individual hosts. In this post, we will explain how to spin up docker hosts locally on your laptop using virtual box.

Docker Machine tutorial

With the new Docker toolbox, setting up docker and its related components like docker-compose and docker-machine is relatively very easy. Toolbox is supported for both windows and Mac systems. All you need to do is download toolbox and install it as if you install any other application. It will install VirtualBox, docker and its related components on the fly.

If you do not want to go with Docker toolbox, you can install and configure docker machine on your local docker host using the following steps.

Note: For manual installation, a working docker installation of latest docker should be present in your local workstation.

1. Download the latest version of docker machine to /usr/local/bin folder using the following command.

2. The docker-machine binary needs execution permissions. Apply permissions using the following command.

3. Check the installation using the following command.

Creating Docker Machines on Virtual Box

Assuming that docker machine is configured in your workstation, you can list all the machine available using the following command.

The above shows that there is a default virtual box machine in the stopped state.

“docker-machine create” command is used to create a new docker machine. To spin up a new machine on virtual box, use the following command.

In the above command, “—driver” flag tells docker machine which platform to use. In our case, it is VirtualBox. If it is was ec2 it would have a driver named amazonec2. “Test” is a user defined name for the machine you want to create. It can be anything like dev, staging etc..

The output for the above command looks like the following.

Execute the following command to get the information about your newly created test machine.

As you can see from the above output, our new test machine is up and running. Under URL, it shows the IP address and the port. You can use that information, however, if you want access the test host without docker machine.

Now, next this you might want to do is spin up some containers in your new test host. To do this, first we need to tell docker-machine which machine it should manage. To do this, execute the following command.

The above command will set all the necessary environment variable for your current terminal to manage the test machine. To view all the environment variables, execute the following command.

Note: every time you open a new terminal, you should execute the eval command to with the required machine name (In case if you have created more machines).

Now, if you run a “docker ps” from your terminal, docker-machine will connect the test docker engine using the docker client configured by docker-machine.

Creating a container using docker-machine

Let’s spin up a busybox container in our new test machine using the following command.

Now, if you run a “docker ps -a” command, you will see the created container.

Stopping, Starting and Restarting a Machine:

Every time you create a machine, a new VM will be launched. So if you want to save the system memory, you can stop the unwanted machines using the following command.

You can start a machine using the following command.

You can restart a machine using the following command.

More commands:

docker-machine is not limited to creating hosts, there are many flags and commands associated with docker-machine. You can list all the supported commands by just typing “docker-machine” in the terminal.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.