The Digital Ocean driver

Let us start creating some instances in the cloud. First, let us launch a machine in Digital Ocean.

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

To sign up for a Digital Ocean account, please visit https://www.digitalocean.com/ and click Sign Up. Once you have or are 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 Generate New Token and follow the onscreen instructions.

Tip

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

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

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token 14760f5bdee403cebb36117c22c83e5ee51188515f493a6c0d281c094c552536 
    dotest

Note

Please note, the tokens used in these examples have been revoked.

This will launch an instance called dotest in your Digital Ocean account.

If you check your Digital Ocean control panel, you should now see the instance which was created by Docker Machine listed:

The Digital Ocean driver

We can also confirm our Digital Ocean Docker host is running by using the following command:

docker-machine ls

This will return all the machines we have running, confirming their state, IP address, Docker version, and name. There is also a column which lets you know which of the Docker Machine managed Docker hosts your local client is configured to communicate with:

The Digital Ocean driver

By default, your local Docker client is configured to communicate with our local Docker installation; as we launched our local Docker installation using Docker for Mac or Windows, or you have Docker installed on Linux Docker Machine will not list it.

Let's change it so it interacts with the Digital Ocean instance.

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

To find out what they all you must do is simply 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 which they can execute, so if we run the command again, but this time in a way where the output will be executed:

eval $(docker-machine env dotest)

Or if you have launched your instance using PowerShell on Windows then use:

docker-machine env dotest | Invoke-Expression

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

The Digital Ocean driver

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

Run the following command:

docker container run hello-world

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

docker container ls –a

You should see that the hello-world container exited a few seconds ago.

You can SSH into the Digital Ocean instance using the following command:

docker-machine ssh dotest

Once logged in, run the docker container ls –a command to demonstrate that the docker container run you ran locally was executed on the Digital Ocean instance.

The beauty of this setup is that you shouldn't have to SSH to your instances often.

Note

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

Docker Machine has some sensible defaults which are as follows:

  • digitalocean-image = ubuntu-16-04-x64
  • digitalocean-region = nyc3
  • digitalocean-size = 512mb

As I am based in the UK, let's look at changing the region and specification of the host launched by Docker Machine.

First, we should remove the dotest instance by running the following command:

docker-machine rm dotest

This will terminate the 512 MB instance running in NYC3.

Tip

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

Now 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 like this (again, remember to replace the example API token with your own):

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token 14760f5bdee403cebb36117c22c83e5ee51188515f493a6c0d281c094c552536
    --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 the following command:

eval $(docker-machine env douktest)

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

The Digital Ocean driver

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

curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer 14760f5bdee403cebb36117c22c83e5ee51188515f493a6c0d281c094c552536" "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 users home directory. For example, the key for douktest can be found by running:

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 the configuration:

The Digital Ocean driver

So that covers launching a host in Digital Ocean; how about launching something more exciting than the Hello World container?

No problem, let's use Docker Compose to launch a variation of the WordPress stack we used in Chapter 2, Launching Applications Using Docker. Start by going to the /bootcamp/chapter03/wordpress folder and then run the following command:

docker-machine ls

To check you have your Docker client configured to use your Digital Ocean Docker host. Once you are sure your client is using the remote host, simply run:

docker-compose up -d

This will download the images we need, then launch two containers. This time you will be able to access the WordPress installation on port 80 on your Digital Ocean host. To find the IP of your host, you can run the following command:

docker-machine ip douktest

Or on a Mac or Linux machine to open your browser and go to your installation page run the following command:

open http://$(docker-machine ip douktest)/

The terminal session below shows the output you can expect to see from the previous commands:

The Digital Ocean driver

You will then be able to complete your WordPress installation:

The Digital Ocean driver

Tip

I wouldn't recommend leaving your WordPress installation at the installation screen for long as it is possible that someone could complete the installation on your behalf and get up to no good.

Once you have finished your Digital Ocean, host run the following command to terminate it:

docker-machine rm douktest

Now that we have learned how to launch a Docker host in Digital Ocean let's move on to Amazon Web Services.

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

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