I have been trying to find some solid documentation around building a docker image using packer and I have come up short in web searches and Hashi docs.
Even trying to piece the simple stuff together using the “snippets” of code in the Hashi docs fails on every run. Obviously I am not that smart to put it all together myself, so perhaps a simple gist wouldn’t go too far astray for people of the likes of me.
Can someone please at least point me to a HCL doc that can do some basic stuff like
- use base ubuntu
- install some apps from shell script
- add a docker tag (my god, i have NFI about this one, using the snippets in hashi docs fails every single time
No labels are expected for post-processors blocks
)
The following basic script i have is:
source "docker" "ubuntu" {
commit = true
image = "ubuntu:20.04"
}
build {
sources = ["source.docker.ubuntu"]
provisioner "shell" {
script = "./apps.sh" # this just does apt-get update
}
}
it runs, it builds, but the container fails every single time.
==> docker.ubuntu: Committing the container
docker.ubuntu: Image ID: sha256:989ac9ca71b0932293382c4a1727a2e3e3dac1d36570943374413c709697e3dd
==> docker.ubuntu: Killing the container: f80d9c30548787b6dd3ab6a7aec4a904d05acd11996265e54fa8738b76fd0426
Build 'docker.ubuntu' finished after 27 seconds 839 milliseconds.
==> Wait completed after 27 seconds 839 milliseconds
==> Builds finished. The artifacts of successful builds are:
--> docker.ubuntu: Imported Docker image: sha256:989ac9ca71b0932293382c4a1727a2e3e3dac1d36570943374413c709697e3dd
asdf@asdf:vscode$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 989ac9ca71b0 12 seconds ago 123MB
ubuntu 20.04 f63181f19b2f 5 weeks ago 72.9MB
asdf@asdf:vscode$ docker run -it 989ac9ca71b0 /bin/sh
/bin/sh: 0: Can't open exec
Hi @fluffy-cakes ! It looks like your frustration may be more with Docker than with Packer. I was able to reproduce your issue using this template. The issue is that the ENTRYPOINT
of the container is /bin/sh
and you are passing as argument /bin/sh
Perhaps you should set an entrypoint on your image, by specifying it in the template. This is covered in the documentation at
1 Like
Many thanks for your reply. You were right, I changed my entrypoint and it worked.
Updated my apps.sh
script, which you can see here: devops_tools/apps.sh at main · fluffy-cakes/devops_tools · GitHub
I can build this and run it using docker run -it a7b878c11091
and it will take me straight into pwsh
which is what I want.
However, I’m still stuck on something so simple as adding a tag. I’ve copied the HCL snippet from the doc site, but it still gives me an error. I’ve no idea how the heck this tag is to be used. Here’s my code now;
source "docker" "ubuntu" {
commit = true
image = "ubuntu:20.04"
changes = [
"ENTRYPOINT pwsh"
]
}
build {
sources = ["source.docker.ubuntu"]
provisioner "shell" {
script = "./apps.sh"
}
post-processors "docker-tag" {
repository = "hashicorp/packer"
tag = "0.7,anothertag"
}
}
Running a validate/build gives this error:
Error: Extraneous label for post-processors
on tools.pkr.hcl line 16, in build:
16: post-processors "docker-tag" {
No labels are expected for post-processors blocks.
Any ideas?
Glad it helped
I think the issue with the post-processors
is that it needs to be a block with a list of post-processor
items:
build {
sources = ["source.docker.ubuntu"]
provisioner "shell" {
script = "./apps.sh"
}
# This is the post-processors block - https://www.packer.io/docs/templates/hcl_templates/blocks/build/post-processors#the-post-processors-block
post-processors {
# This is a list of objects in a block - https://www.packer.io/docs/templates/hcl_templates/blocks/build/post-processor#the-post-processor-block
post-processor "docker-tag" {
repository = "hashicorp/packer"
tag = "0.7,anothertag"
}
}
}
By changing post-processors
(ie the plural form) to post-processor
(ie, the singular) in your template, according to the docs you should get the behaviour you want. The docs explain how the post-processors
block and post-processor
block work:
1 Like
ahhh… I didn’t understand that it was a collection of post-processors. Thanks for linking me to the right area, it makes sense now
1 Like