Goals were:
- build amzn2 AMI and launch it on AWS
- build similar amz2 container on local (OSX) hosted docker
After finding that packer can create a vagrant box in post processing I tried creating one ready for docker, but it won’t launch.
My general steps are:
packer build aws-amzn2.pkr.hcl
vagrant box add --name amzn2 packer_amzn2_docker.box
vagrant init amzn2
vagrant up
…which fails with:
Bringing machine 'default' up with 'docker' provider...
==> default: Creating and configuring docker networks...
==> default: Creating the container...
default: Name: packer_tutorial_default_1627124615
default: Image: sha256:5b4a89e6c2a1508aa6f518d565e4640cd0a620b106847e8fb1ba52a22d56722b
default: Volume: /Users/jsharpe/work/ctm-deploy/packer_tutorial:/vagrant
default:
default: Container created: 306f5278a582cf7d
==> default: Waiting for container to enter "running" state...
The container started either never left the "stopped" state or
very quickly reverted to the "stopped" state. This is usually
because the container didn't execute a command that kept it running,
and usually indicates a misconfiguration.
If you meant for this container to not remain running, please
set the Docker provider configuration "remains_running" to "false":
config.vm.provider "docker" do |d|
d.remains_running = false
end
I’m using this hcl (it currently has the amazon-es stuff commented out)
variable "ami_prefix" {
type = string
default = "learn-packer-amzn2-redis"
}
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}
packer {
required_plugins {
amazon = {
version = ">= 0.0.2"
source = "github.com/hashicorp/amazon"
}
docker = {
version = ">= 0.0.7"
source = "github.com/hashicorp/docker"
}
}
}
source "amazon-ebs" "amzn2" {
ami_name = "${var.ami_prefix}-${local.timestamp}"
instance_type = "t2.micro"
region = "us-west-2"
source_ami_filter {
filters = {
name = "amzn2-ami-ecs-hvm-2.0.*-x86_64-ebs"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
}
ssh_username = "ec2-user"
}
source "docker" "amzn2" {
image = "amazonlinux:2"
commit = true
}
build {
sources = [
# "source.amazon-ebs.amzn2",
"source.docker.amzn2"
]
provisioner "shell" {
environment_vars = [
"FOO=hello world",
]
inline = [
"echo Installing Redis",
"sleep 5",
# "yum-config-manager --enable epel",
# "yum install redis"
# "sudo apt-get update",
# "sudo apt-get install -y redis-server",
# "echo \"FOO is $FOO\" > example.txt",
]
}
provisioner "shell" {
inline = ["echo This provisioner runs last"]
}
post-processor "vagrant" {
keep_input_artifact = true
provider_override = "docker"
}
}