unable to prepare context: path “…” not found
Packer cannot run docker command in a script
If you are attempting to run Docker in a Packer script you may have some issues where the Docker file path causes you grief.
Here’s a sample structure to demonstrate what I’m doing:
When I call a script from within the linux.json file the script path should be relative to the run.sh file. So if I want to call script1.sh it would be:
Now I’m trying to run a docker build with a version of a path used to work:
docker build -t 2sl/myapp /home/ec2-user/myapp
But I’m getting this error with whatever path I try:
unable to prepare context: path “/home/ec2-user/myapp” not found
So where am I when calling this script. Let’s see what else is in the current directory by calling this command from within the script that’s trying to run the docker command.
And I see this:
I have a couple of different tools folder in my folder structure on the AMI where I’m running packer and that code should have been cloned into the AMI that’s currently executing commands.
Let’s run another ls to see what is above this tools directory.
Aha. This tools directory is in the home directory, or ec2-user on the instance trying to execute this script.
Let’s see what’s in the tools directory.
In my case, I see a folders for some repositories I clone down to the AMI where I store some tools. That’s expected.
Drilling dow a bit further I see that my files are missing within the subdirectory.
The docker command is looking for files on the EC2 instance that packer spins up to build the new image.
The AMI I’m building is based on an AMI I built previously where I copy files down to the machine. So I went back to check that prior AMI version to see if it ran correctly and find out why the files are missing.
The AMI build completed but there was an error I didn’t notice downloading some files from S3.
FIX: Would be better if Packer stops the build when errors occur so they are not missed.
The error is related to setting IAM credentials for Packer to use to download files from S3.
Packer: Unable to locate credentials. You can configure credentials by running “aws configure”.
Running AWS CLI commands on packer
It also had to do with the fact that I store the bucket name in an SSM Parameter. The Role running the Packer template on the temporary EC2 instance did not have access to the SSM Parameters.
I also added encryption to the parameters using SecureString.The identity running the commands in the Packer template needs permission to decrypt the parameter with the KMS key.
(AccessDeniedException) when calling the GetParameter operation: The ciphertext refers to a…
Trying to access a parameter from a Packer template I got this error:
Once I fixed those things the files downloaded onto the prior AMI and the commands on this AMI in the error message in this post were resolved.
If you liked this story please clap and follow:
Medium: Teri Radichel or Email List: Teri Radichel
Twitter: @teriradichel or @2ndSightLab
Requests services via LinkedIn: Teri Radichel or IANS Research
© 2nd Sight Lab 2022
About this blog:
Want to learn more about Cybersecurity and Cloud Security? Check out: Cybersecurity for Executives in the Age of Cloud on Amazon
Need Cloud Security Training? 2nd Sight Lab Cloud Security Training
Cybersecurity & Cloud Security Resources by Teri Radichel: Cybersecurity and Cloud security classes, articles, white papers, presentations, and podcasts