Skip to content

Instantly share code, notes, and snippets.

@JoelBCarter
Created June 14, 2016 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelBCarter/4c17990ed6b3d69b809c39e4b75bbb29 to your computer and use it in GitHub Desktop.
Save JoelBCarter/4c17990ed6b3d69b809c39e4b75bbb29 to your computer and use it in GitHub Desktop.
A Packer template for building a Docker container host on an AWS EC2 instance.
{
"variables": {
"aws_access_key": "",
"aws_secret_key": ""
},
"builders": [
{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "ami-663a6e0c",
"instance_type": "m3.xlarge",
"ssh_username": "ubuntu",
"ami_name": "packer-docker",
"ami_description": "Used as a base server for running containers",
"tags": {
"Role": "docker",
"Built": "{{timestamp}}"
},
"ami_block_device_mappings": [
{
"device_name": "/dev/sdf",
"volume_size": "100",
"virtual_name": "EBS-1"
}
]
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"echo 'wait-for-core-services'",
"sleep 10"
]
},
{
"type": "shell",
"inline": [
"echo 'docker-prerequisites'",
"sudo apt-get update -qq",
"sudo apt-get install -qq apt-transport-https ca-certificates",
"sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D",
"echo 'deb https://apt.dockerproject.org/repo ubuntu-trusty main' | sudo tee /etc/apt/sources.list.d/docker.list",
"sudo apt-get update -qq",
"sudo apt-get purge lxc-docker",
"sudo apt-cache policy docker-engine",
"sudo apt-get -qq install linux-image-extra-$(uname -r) apparmor"
]
},
{
"type": "shell",
"inline": [
"echo 'docker-install'",
"sudo apt-get update -qq",
"sudo apt-get install -qq docker-engine"
]
},
{
"type": "shell",
"inline": [
"echo 'docker-add-user-to-group'",
"sudo usermod -aG docker ubuntu"
]
},
{
"type": "shell",
"inline": [
"echo 'docker-configure-dns'",
"echo 'DOCKER_OPTS=\"--dns 8.8.8.8 --dns 8.8.4.4\"' | sudo tee /etc/default/docker",
"sudo service docker restart"
]
},
{
"type": "shell",
"inline": [
"echo 'reboot'",
"sudo reboot"
]
},
{
"type": "shell-local",
"command": "sleep 60"
}
]
}
@JoelBCarter
Copy link
Author

This Packer template is used for building a base box on AWS that can then host containers. I follow the Docker installation instructions for Installation on Ubuntu. It can be used as follows:

packer build --var 'aws_access_key=<YOUR_ACCESS_KEY>' --var 'aws_secret_key=<YOUR_SECRET_KEY>' .\packer-docker-aws.json

Add additional provisioners after the last one for whatever containers/additional packages you might want to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment