Bugs That Bite
Published in

Bugs That Bite

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:

/home/ec2-user/packer/run.sh
/home/ec2-user/packer/templates/linux.json
/home/ec2-user/packer/scripts/script1.sh
/home/ec2-user/myapp/Dockerfile

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:

scripts/script1.sh

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.

ls

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.

ls ../

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.

ls tools

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.

What happened?

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.

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.

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.

Teri Radichel

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

Is your cloud secure? Hire 2nd Sight Lab for a penetration test or security assessment.

Have a Cybersecurity or Cloud Security Question? Ask Teri Radichel by scheduling a call with IANS Research.

Cybersecurity & Cloud Security Resources by Teri Radichel: Cybersecurity and Cloud security classes, articles, white papers, presentations, and podcasts

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Teri Radichel

Teri Radichel

Cloud Security Training and Penetration Testing | GSE, GSEC, GCIH, GCIA, GCPM, GCCC, GREM, GPEN, GXPN | AWS Hero | Infragard | IANS Faculty | 2ndSightLab.com