I just started with Packer, and want to build AMI and Docker images.
Problem is - that Docker's Ubuntu image have no sudo, thus - build failed with an error:
docker: /tmp/script_2085.sh: 3: /tmp/script_2085.sh: sudo: not found
Is there any way to add some "conditions" for a shell provisioner to run cmds with or without sudo, depending on builder?
Currently - my template have:
...
"provisioners": [{
"type": "shell",
"inline": [
"sleep 30",
"sudo apt-get update",
"sudo apt-get install -y nginx"
]
}]
...
Add something like:
if which sudo; then sudo apt-get update && sudo apt-get install -y nginx; else apt-get update && apt-get install -y nginx; fi
Looks too weird solution... I'm sure - Packer has facility to do it in a better way.
Why don't you just install sudo in the docker box.
You could do this one of two ways:
sudo, only on your docker imageYou can use the only parameter in the provisioner to install sudo on an image:
...
"provisioners": [{
"type": "shell",
"inline": [
"apt-get install sudo"
],
"only": ["docker"]
},
... other provisioners here ...
]
...
More info in the packer docs, here, under "Run On Specific Builds".
At the beginning of your first provision script, add the command to install sudo if it is not already installed. Use the following (taken from here):
dpkg -l | grep -qw sudo || apt-get install sudo
to check the list of installed packages and install if sudo is not present.
Personally, I'd be inclined to go for the first option, since it explicitly states that something should be done only on certain boxes. No room for confusion.
Parting notes
sudo in the first one.apt-get update before you do the install.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With