Can't get packer-build docker box to launch with vagrant

Goals were:

  1. build amzn2 AMI and launch it on AWS
  2. 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:

  1. packer build aws-amzn2.pkr.hcl
  2. vagrant box add --name amzn2 packer_amzn2_docker.box
  3. vagrant init amzn2
  4. 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"
  }
}

After vagrant up a new docker container has been created, but it is offline and says “EXITED(0)”

I’ve untarred the box file and it looks right-ish(?):

tar zxf packer_amzn2_docker.box -O
Vagrantfile

# The contents below were provided by the Packer Vagrant post-processor

Vagrant.configure("2") do |config|
        config.vm.provider :docker do |docker, override|
                docker.image = "sha256:bbd693d18d763b2dd89d32638efb8be73c7994ca4a3338dda8401b1bdd7c54b0"
        end
end


# The contents below (if any) are custom contents provided by the
# Packer template during image build.

metadata.json

{"provider":"docker"}

But after adding the box, and running vagrant init amzn2 the resulting Vagrant file has:

  config.vm.box = "amzn2"

and no mention of docker