Getting started with Docker Machine on EC2 is
straight forward: a brief section
of the official documentation covers all command line options and environment
variables which are relevant to
docker-machine --driver amazonec2 create ....
The required steps are:
- Create an new IAM user and attach a Custom Policy to the user
- Find out the VPC ID
- Create the Docker Machine instance
This guide applies to Docker Machine version 0.5.0 on OS X.
Create an new IAM user and attach a Custom Policy to the user
Login to the AWS console and navigate to Services / IAM / Users.
Create a new user (e.g. docker-machine), note the access key and secret and populate the environment variables:
Then select the user, switch to the Permissions tab and add a new Custom Policy (expand Inline Policy to do so). The following policy was shared by Brandon Mangold on docker-machine Issue 1655 (Minimal IAM policy).
A slightly extended it with permissions for creating security groups:
%AWS_ACCOUNT_ID% with your account ID. Get it from: AWS console / My Account.
I also changed the region to
Find out the VPC ID
This step is also covered in the official drivers documentation. Login to the AWS console and navigate to Services / VPC / Your VPCs. Get the VPC ID from the VPC column and set the environment variable like this:
Create the docker machine
Four environment variables have been populated:
As mentioned in the previous step, I changed to region to
It must match the region in the IAM policy:
Create the aws01 machine with
-D option for debugging:
Everything is fine, if the command above completes with the message: "To see how to connect Docker to this machine, run: docker-machine env aws01" If not so, check out the troubleshooting section below.
To verify the setup, just run:
Use the docker-machine command to interact with the newly created EC2 instance.
If the creation of instances via
docker create fails:
- Check the IAM policy: region matches / AWS ID matches?
- Go to AWS console / Services / EC2 / Security Groups and check if the group already exists.
- You may have to remove the aws01 key pair if the first run of
docker-machine createfails: Go to AWS console / Services / EC2 / Key paris.