how to build images for docker and amazon-ebs from the same script, or "to sudo or not to sudo?"

91 views
Skip to first unread message

Kevin Pauli

unread,
Mar 29, 2017, 12:22:10 PM3/29/17
to Packer
Newbie here, trying to figure out how to build both a docker image and an amazon-ebs ami from the same script.  Here I have stripped it down to the essentials:

{
 
"builders": [
   
{
     
"type": "amazon-ebs",
     
"ami_name": "my-ami",
     
"instance_type": "t2.micro",
     
"region": "us-east-1",
     
"source_ami": "ami-f4cc1de2",
     
"ssh_username": "ubuntu"
   
},
   
{
     
"type": "docker",
     
"image": "ubuntu:16.04"
   
}
 
],
 
"provisioners": [
   
{
     
"type": "shell",
     
"inline": [
       
"sleep 30",
       
"sudo apt-get update"
     
]
   
}
 
]
}

The problem is the "apt-get update" command...

If I do not include sudo, the docker build works but the amazon-ebs ami fails to provision with "Permission denied" errors.  

If I do include the sudo, the amazon-ebs build works but the docker build fails with "sudo: not found ". How to solve this?

Rickard von Essen

unread,
Mar 29, 2017, 1:00:52 PM3/29/17
to packe...@googlegroups.com
Just install sudo in the docker image in a shell provisioner with "only": [ "docker" ]. 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/a17dff65-ef29-4c88-853d-d32fcc9df357%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Pauli

unread,
Mar 29, 2017, 3:06:53 PM3/29/17
to Packer
That worked great, thank you!

Two things I learned: 

1) sudo is a package that can be installed like any other (somehow I thought it was built-in to all Linuxes)
2) provisioners support the "only" attribute (somehow I missed that when reading the docs)

I like this because I'm now also able to run the "sleep 30" only on "amazon-ebs".  It is not necessary on "docker" and was unnecessarily delaying the build.


Thanks again
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.

Alvaro Miranda Aguilera

unread,
Apr 2, 2017, 7:14:39 AM4/2/17
to packe...@googlegroups.com
Hi kevin

could you share an updated json template for ppl coming in the future?

thanks!
Alvaro.

To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/38617c31-2932-4642-a70f-56ba26d74ea4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Kevin Pauli

unread,
Apr 3, 2017, 6:00:43 PM4/3/17
to Packer
{
 
"builders": [
   
{
     
"type": "amazon-ebs",
     
"ami_name": "my-ami",
     
"instance_type": "t2.micro",
     
"region": "us-east-1",
     
"source_ami": "ami-f4cc1de2",
     
"ssh_username": "ubuntu"
   
},
   
{
     
"type": "docker",
     
"image": "ubuntu:16.04"
   
}
 
],
 
"provisioners": [
   
{
     
"type": "shell",

     
"only": "amazon-ebs",
     
"inline": [
       
"sleep 30"
     
]
   
}
   
{
     
"type": "shell",
     
"only": "docker",
     
"inline": [
       
"apt-get install -y sudo"
     
]
   
},
   
{
     
"type": "shell",
     
"inline": [
        "sudo apt-get update"
     
]
   
}

 
]
}



--
Alvaro

Reply all
Reply to author
Forward
0 new messages