Docker Machine

So, when you ran the Docker Quickstart Terminal application, it created a bunch of certificates, SSH keys, and configured your user's environment to run Docker. It also launched a virtual machine running Docker.

Developing locally

The Docker Quickstart Terminal application did this using Docker machine, you can check the status of the machine launched by the application by running the following command:

docker-machine active

This will list the names of any active machines, the default machine launched when you first install Docker is called default, if you run:

docker-machine status default

It should tell you that the virtual machine is currently running. Finally, you should be able to SSH into the virtual machine by running the following command:

docker-machine ssh default

You will notice that when you SSH into the virtual machine, it is running the Boot2Docker distribution.

Note

Boot2Docker is an extremely lightweight Linux distribution based on Tiny Core Linux, and its one purpose is to run Docker. Due to this, the entire distribution comes in at less than 30 MB, and it boots in around five seconds, which makes it perfect for running local development machines. For more information on Boot2Docker, refer to http://boot2docker.io/, and for Tiny Core Linux, refer to http://tinycorelinux.net/.

You should something similar to the following terminal session when running these commands:

Developing locally

There isn't much need to SSH into the virtual machine, though, as the Docker client that was installed by toolbox has been configured to connect to the Docker Engine on the virtual machine, this means that when you run the Docker commands locally, it passes all the calls through Docker on the virtual machine, try running the hello-world container:

docker run hello-world

You should see the following output:

Developing locally

At this stage, you may be thinking to yourself, this all is very good, but it's hardly a tool to get excited about. Well, you are wrong. Docker Machine has a few more tricks up its sleeve than being able to launch a Boot2Docker virtual machine locally.

Heading into the cloud

Docker Machine is able to connect to the following services, provision an instance, and configure your local Docker client to be able to communicate to the cloud-based instance.

The public cloud providers that currently are supported are as follows:

The following self-hosted platforms can also be used:

The DigitalOcean driver

Let's start creating some instances in the cloud. First, let's launch a machine in DigitalOcean.

There are two prerequisites for launching an instance with Docker Machine in DigitalOcean, the first is a DigitalOcean account and the second is an API token.

To sign up for a DigitalOcean account, visit https://www.digitalocean.com/ and click on the Sign Up button. Once you have logged in to your account, you can generate an API token by clicking on the API link in the top menu.

To grab your token, click on the Generate New Token button and follow the on-screen instructions:

Tip

You only get one chance to make a record of your token, make sure that you store it somewhere safe, as it will allow anyone who has it to launch instances into your account.

The DigitalOcean driver

Once you have the token, you can launch your instance using Docker Machine. To do this, run the following command; make sure to replace the example API token with your own:

Tip

Using a backslash: As we have a lot options to pass to the docker-machine command, we are using to split the command over multiple lines so that it's easier to follow what is going on.

 docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token sdnjkjdfgkjb345kjdgljknqwetkjwhgoih314rjkwergoiyu34rjkherglkhrg0 
    dotest

This will launch a dotest instance into your DigitalOcean account, you will see something similar to the following terminal output:

The DigitalOcean driver

If you check your DigitalOcean control panel, you will now see that the instance that was created by Docker Machine is listed here:

The DigitalOcean driver

Now we have two instances launched by Docker Machine, one running locally running on our machine called default and one hosted in DigitalOcean called dotest. We can confirm this by running the following command:

docker-machine ls

This will return all of the machines we have running and confirm their state, IP address, Docker version, and name. There is also a column that allows you to know which of the two machines your local environment is configured to communicate with:

The DigitalOcean driver

In the preceding example, our local Docker client is configured to communicate with the default instance, which is the run running locally. Let's change it so that it interacts with the DigitalOcean instance.

To do this, you have change some local environment variables, luckily, Docker Machine provides an easy way to find out what these are and also change them.

To find out what they all you have to do is simple, run the following command:

docker-machine env dotest

This will tell you exactly what you need to run to change from the default machine to dotest. The best thing is that the command itself formats the results in such a way that they can be executed, so we run the command again, but this time in a way where the output will be executed:

eval $(docker-machine env dotest)

Now if you get a listing from Docker Machine, you will notice that the dotest environment is now the active one:

The DigitalOcean driver

Now that we have our DigitalOcean instance active, you can run the docker command on your local machine, and they will have been executed on the DigitalOcean instance. Let's test this by running the hello-world container.

If you run the following command, you should see the image download and then the output of running the hello-world container:

docker run hello-world

If you then run the following command, you will see that the hello-world image exited a few seconds ago:

docker ps –a

This is demonstrated by the following Terminal output:

The DigitalOcean driver

As you can see, I used ssh to get into the DigitalOcean instance and ran the docker ps –a and docker images commands to demonstrate that the commands I ran locally were executed on the DigitalOcean instance; however, the beauty of this setup is that you shouldn't have to SSH instance often.

One thing you may have noticed is that all we told Docker Machine is that we want to use DigitalOcean and our API token; at no point did we tell it which region to launch the instance in, what specification we wanted, or which SSH key to use.

Docker Machine has some following sensible defaults:

  • digitalocean-image = ubuntu-15-10-x64
  • digitalocean-region = nyc3
  • digitalocean-size = 512mb

As I am based in the UK, let's look at changing the region and the specifications of the machine. First of all, we should remove the dotest instance by running the following command:

docker-machine rm dotest

This will terminate the 512mb instance running in NYC3.

Tip

It is important to terminate instances that you are not using, as they will be costing you for each hour they are active. Remember one of the key advantages of using Docker Machine is that you can spin up instances both quickly and with as little interaction as possible.

Now that we have removed the old instance, let's add some additional flags to our docker-machine command to launch the new instance in the desired region and specification, we will be calling our new instance douktest. The updated docker-machine create command now looks similar to the following (remember to replace the example API token with your own):

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token sdnjkjdfgkjb345kjdgljknqwetkjwhgoih314rjkwergoiyu34rjkherglkhrg0 
    --digitalocean-region lon1 
    --digitalocean-size 1gb 
    douktest

You should see similar output from the command as before, once the instance has been deployed, you can make it active by running:

eval $(docker-machine env douktest)
The DigitalOcean driver

When you enter the control panel, you will notice that the instance has launched in the specified region and at the desired specification:

The DigitalOcean driver

For full details on each of the regions and what machine types are available in each one, you can query the DigitalOcean API by running the following command (remember to replace the API token):

curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer sdnjkjdfgkjb345kjdgljknqwetkjwhgoih314rjkwergoiyu34rjkherglkhrg0" "https://api.digitalocean.com/v2/regions" | python -mjson.tool

This will output information about each region.

One last thing, we still haven't found out about the SSH key. Each time you run Docker Machine, a new SSH key for the instance you are launching is created and uploaded to the provider, each key is stored in the .docker folder in your user's home directory. For example, the key for douktest can be found by running the following command:

cd ~/.docker/machine/machines/douktest/

Here, you will also find the certificates used to authenticate the Docker agent with the Docker installation on the instance and also the configuration:

The DigitalOcean driver

This covers DigitalOcean, what about other services? Let's quickly look at Amazon Web Services so that we can get an idea between the drivers for the different cloud providers.

The Amazon Web Services driver

If you don't already have an Amazon Web Services account, you should sign up for one at http://aws.amazon.com/. If you are new to AWS, then you will eligible for their free tier at http://aws.amazon.com/free/.

I would recommend reading through Amazon's getting started guide if you are unfamiliar with AWS before working through this section of the chapter, you can find the guide at http://docs.aws.amazon.com/gettingstarted/latest/awsgsg-intro/gsg-aws-intro.html.

The AWS driver is similar to the DigitalOcean driver and it has some sensible defaults, rather than going into too much detail about how to customize the EC2 instance launched by Docker Machine, I will stick to the defaults. For AWS driver, the defaults are as follows:

  • amazonec2-region = us-east-1 (North Virginia)
  • amazonec2-ami = ami-26d5af4c (Ubuntu 15.10)
  • amazonec2-instance-type = t2.micro
  • amazonec2-root-size = 16GB
  • amazonec2-security-group = docker-machine

Before we launch our instance, we will also need to know our AWS access and secret keys, and also the VPC ID will be launching our instance. To get these, log in to the AWS console that can be found at https://console.aws.amazon.com/.

You should already have a copy of your access and secret ID as these are created when your user was first created in AWS. If you have lost these, then you can generate a new pair by navigating to Services | IAM | Users, then selecting your user, and finally going to the Security Credentials tab. There you should see a button that says Create Access Key.

Note

Amazon describes Amazon Virtual Private Cloud (VPC) as letting you provision a logically-isolated section of the AWS cloud, where you can launch resources in a virtual network that you define. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways.

Before you find your VPC ID, you should make sure that you are in the correct region by ensuring that it says N. Virginia at the top right-hand corner of your AWS console, if it doesn't select it from the drop-down list.

Once you have ensured you are in the correct region, go to Services | VPC and click on Your VPCs. You don't need to worry about creating and configuring a VPC as Amazon provides you with a default VPC in each region. Select the VPC and you should see the something similar to the following screenshot:

The Amazon Web Services driver

Make a note of the VPC ID, you should now have enough information to launch your instance using Docker Machine. To do this, run the following command:

docker-machine create 
    --driver amazonec2 
    --amazonec2-access-key JHFDIGJKBDS8639FJHDS 
    --amazonec2-secret-key sfvjbkdsvBKHDJBDFjbfsdvlkb+JLN873JKFLSJH 
    --amazonec2-vpc-id vpc-35c91750 
    awstest

If all goes well, you should see something similar to the following output:

The Amazon Web Services driver

You should also be able to see an EC2 instance launched in the AWS Console by navigating to Services | EC2 | Instances:

The Amazon Web Services driver

You may have noticed that Docker Machine created the security group and also assigned an SSH key to the instance without any need for us to get involved, keeping within the principle that you don't need to be an expert in configuring the environments that you are launching your Docker instance into.

Before we terminate the instance, let's switch our local Docker client over to use the AWS instance and launch the Hello World container:

The Amazon Web Services driver

As you can see, once you have launched an instance using Docker Machine and switched your local Docker client to it, there is no difference in usage between running Docker locally and on a cloud provider.

Before we start to rack up the cost, we should terminate our test AWS instance by running the following command:

docker-machine rm awstest

Then confirm that the instance has been terminated correctly in the AWS console:

The Amazon Web Services driver

If you don't do this, the EC2 instance will quite happily sit there costing you $0.013 per hour until it is terminated.

Note

Note that this is not Amazon's Elastic Container Service (ECS). We will be covering Amazon ECS in Chapter 7, Looking at Schedulers.

Other considerations

As you can see from examples we have worked through, Docker Machine is a powerful part of Docker Toolbox as it allows users of all skill levels to be able to launch an instance either locally or in a cloud provider without having to roll their sleeves up and get stuck in configuring server instances or their local Docker client.

The examples we have used in this chapter have been launching either Boot2Docker or Ubuntu. Docker machine also supports the following:

The other thing to mention about Docker Machine is that, by default, it operates and opts in for crash reporting, considering the amount of different configuration/environment combinations Docker Machine can be used with, it is important that Docker get notified of any problems to help them make a better product. If, for any reason, you want to opt-out, then running the following command will disable crash reporting:

mkdir -p ~/.docker/machine && touch ~/.docker/machine/no-error-report

For more information on Docker Machine, you can refer to the official documentation:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset