Docker on Google Compute Engine Using Docker Machine
Next up in Using Docker Machine across private and public clouds, is Google Compute Engine (GCE).
As with the other posts, a diagram of how I view the importance of a Docker Machine abstraction.
First, if you don’t already have an account you can get $300 credit expiring after 60-days towards Google GCE that we will use as Docker hosts in this setup.
Setup your $300 in credit on GCE →
Configure Google Compute Engine to Enable Docker Containers and API Calls
Check this link for some of the pre-requisites to get your GCE account enabled for containers.
Once you have signed up, you will get dropped into a page with a project. Record the Project ID (api-project-xxxxxxxxxxxxx).
As a n00b to GCE it wasn’t clear in a couple of areas what the pre-reqs to get started were but I think there are at least two steps after signing up to start running Docker containers. Log into your Google Developers Console
- Head to the Developers Console and enable the “Google Container Engine API” from [APIs & Auth]-> [APIs] -> [Container Engine API] -> [Enable the API]
- Also inside the Google Developers Console, you need to enable billing for the Container Engine. [Compute]-> [Container Engine] -> [Enable Billing]
Create Docker Machines on GCE
See the Docker Machine docs for detailed descriptions and defaults of all of the Docker Machine flags.
The following is just the bare minimum for provisioning a machine:
1 2 3 4 5 6 |
docker-machine create \ --driver google \ --google-project api-project-xxxxxxxx \ google-machine |
If you haven’t seen GCE before you will get prompted for an OAUTH authentication which will spring a web page where you accept and auth with your gmail account.
I ran into a bit of an issue here only on setting up a new project. Once the project was good to go then the docker machine CRUD was perfect. It almost seemed like there was some delay in the project propagating after I enabled billing. After an error on billing not being enabled (listed below in troubleshooting) I got the following API response:
1 2 3 4 |
Error creating machine: googleapi: Error 404: The resource 'projects/api-project-xxxxxxxxxx' was not found, notFound You will want to check the provider to make sure the machine and associated resources were properly removed. |
I poked around for about 30 minutes, created a new project, got the same errors and proceeded to install the gcloud
client listed in Troubleshooting. Then things just started working,(of course ;_). I don’t think it was from doing anything with the gcloud
client but its possible. I also listed my steps with that in case it was related. I haven’t had time to recreate the steps to validate the issue or if it was just someone giving the gremlins water after midnight.
This is all normal stuff and to be expected when getting setup, which is another reason I’m excited about moving towards some ubiquity in container API interfaces because there is no value to me as a consumer using different but same abstractions and implementations when the end goal is all the same. It seems trivial but it is part of the commodities recipe that containerization will drive compute towards and ultimately a race to zero. Moving on!
To delete the machine or if something wasn’t enabled properly on the Google side of your account you can delete the machine with the following (Note: force is used if there was an issue at provisioning and the instance is essentially orphaned meaning it exists on your docker machine side but not in GCE):
1 2 3 4 5 |
docker-machine rm -f google-machine # Deleting disk. # Successfully removed google-machine |
So here are two instances along with the rest of my free trial account stable of container ready docker host ponies in the cloud.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
docker-machine ls # NAME ACTIVE DRIVER STATE URL SWARM # aws-machine amazonec2 Stopped # dev virtualbox Saved # digitalocean-machine digitalocean Running tcp://45.55.146.243:2376 # google-machine google Running tcp://130.211.137.13:2376 # google-machine2 google Running tcp://23.236.48.13:2376 # microsfot-unique-azure-unique azure Stopped tcp://microsfot-unique-azure-unique.cloudapp.net:2376 # metal-machine none tcp://172.16.86.133:2375 # racker-test-instance * rackspace Running tcp://162.209.126.37:2376 # virtualbox-machine * virtualbox Running tcp://192.168.99.100:2376 |
Seriously, that sexy.
Run Docker on GCE Machines
From here on it it’s smooth docker UI sailing.
1 2 3 |
docker run -d -p 80:80 nginx |
Next verify the service you started. First by getting the IP address of the VM you started with docker-machine create
.
1 2 3 |
docker-machine ip test-machine |
Next curl the public port you started the service on using the IP address you just retreived:
1 2 3 4 5 6 |
curl 45.55.146.243:8000 # You can also nest the 'docker-machine ip' command in the curl requested curl $(docker-machine ip test-machine):80 |
The API supports stopping and starting, it does return /creating/deleting
when /start/stop/
are called from the Google API but other then semantics the operations look good.
1 2 3 4 5 6 7 |
docker-machine start google-machine # Creating instance. # Waiting for Instance... # Uploading SSH Key # Waiting for SSH Key |
And a docker-machine stop
:
1 2 3 4 5 |
$ docker-machine stop google-machine # Deleting instance. # Waiting for instance to delete. |
Troubleshooting GCE Container Process
When you setup your GCE account, you have to enable billing as described above. If you don’t enable billing you will get an error like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
docker-machine create --driver google --google-project api-project-272576371661 google-machine # Opening auth URL in browser. # https://accounts.google.com/o/oauth2/auth?client_id=zzzzzzzzzzzzzzzz.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute&state=yyyyyyyyyyyyyyyy # If the URL doesn't open please open it manually and copy the code here. # Enter code: 4/LONG_CODE_BLAH # Got code: 4/LONG_CODE_BLAH # Saving token in /Users/brent/.docker/machine/machines/google-machine/gce_token # Creating host... # Generating SSH Key # Creating instance. # Creating firewall rule. # Error creating machine: googleapi: Error 403: Project projects/api-project-xxxxxxxxxx cannot accept requests to insert while in an inactive billing state. Billing state may take several minutes to update., inactiveBillingState # You will want to check the provider to make sure the machine and associated resources were properly removed. |
This bit is optional but I was having issues with the project not being found so gave the client API a shot to see if it resolved anything. I dont think it did but it was helpful to validate information and API requests.
(Optional)Install gcloud
client..
1 2 3 |
curl https://sdk.cloud.google.com | bash |
Then Auth.. Which will pop open a web browser to get a token.
1 2 3 |
gcloud auth login |
Set the project name in your terminal to the project ID in the dev console
1 2 3 |
gcloud config set project planar-flux-xxxxx |
Thats it! Hopefully you are seeing the pattern of a common interface between dev/private/public virtual machines and provisioning containers within. Couple that with the ease of provisioning and de-provisioning services has both interesting Ops opportunities along with some serious potentials to reduce your cloud bills per workload by tearing down idle infra.
Thanks for stopping by!