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.
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
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:
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:
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:
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.
Docker Machine has some sensible defaults which are as follows:
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.
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:
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:
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:
You will then be able to complete your WordPress installation:
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.
18.220.191.227