Docker Machine Tutorial : Getting Started Guide

docker machine tutorial

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.

curl -L https://github.com/docker/machine/releases/download/v0.4.0/docker-machine_linux-amd64 /usr/local/bin/docker-machine

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

chmod +x /usr/local/bin/docker-machine

3. Check the installation using the following command.

docker-machine –v

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.

docker-machine ls
devopscube:~ devops$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM
default virtualbox Stopped
devopscube:~ devops$

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

READ  Skillsets To Work In DevOps Environment - A Comprehensive Guide

“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.

docker-machine create --driver virtualbox test

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.

devopscube:~ devops$ docker-machine create --driver virtualbox test
Creating VirtualBox VM...
Creating SSH key...
Starting VirtualBox VM...
Starting VM...
To see how to connect Docker to this machine, run: docker-machine env test
devopscube:~ devops$

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

docker-machine ls
devopscube:~ devops$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM
default virtualbox Stopped
test virtualbox Running tcp://192.168.99.100:2376
devopscube:~ devops$

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.

eval "$(docker-machine env test)"

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.

READ  Understanding Continuous Integration, Delivery and Deployment

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).

docker-machine env test
devopscube:~ devops$ docker-machine env test
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/bibinwilson/.docker/machine/machines/test"
export DOCKER_MACHINE_NAME="test"
# Run this command to configure your shell:
# eval "$(docker-machine env test)"
devopscube:~ devops$

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.

docker run busybox echo This is my first container! Hurayy
hcldevops:~ devops$ docker run busybox echo This is my first container! Hurayy
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
cf2616975b4a: Pull complete
Digest: sha256:df9e13f36d2d5b30c16bfbf2a6110c45ebed0bfa1ea42d357651bc6c736d5322
Status: Downloaded newer image for busybox:latest
This is my first container! Hurayy
hcldevops:~ devops$

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

hcldevops:~ devops$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4056beb31946 busybox "echo This is my firs" 2 minutes ago Exited (0) 2 minutes ago jovial_turing
hcldevops:~ devops$

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.

docker-machine stop <machine-name>
Eg: docker machine stop test

You can start a machine using the following command.

docker-machine start <machine-name>

You can restart a machine using the following command.

docker-machine restart <machine-name>

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.

2 comments

    • Devopscube Reply

      What you mean by without VirtualBox/clouds? You should have some backend to launch the docker hosts. It could be virtual box, Vmware or any public or private cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *