I am running a Packer file to build a Docker image based on ubuntu:20.04
. The Packer configuration uses multiple shell scripts (*.sh
) to provision the image by installing tools, dependencies, and configurations. These scripts are executed sequentially during the build process.
The primary goal is to create a fully configured Docker image with all necessary tools (e.g., Git, GCC, Python, etc.) installed.
I am using the existing repository for setting up the docker image for ubuntu:
But while executing the Packer script, it gets stuck in case of some provisioning steps
Let me know what I am doing wrong here, attaching the screenshot for the same:
Below is my Packer file:
packer {
required_plugins {
docker = {
source = “github.com/hashicorp/docker”
version = “>= 1.1.1”
}
}
}
locals {
image_os = “ubuntu22”
toolset_file_name = “toolset-2204.json”
image_folder = “/imagegeneration”
helper_script_folder = “/imagegeneration/helpers”
installer_script_folder = “/imagegeneration/installers”
imagedata_file = “/imagegeneration/imagedata.json”
}
Define variables for Docker tags and credentials
variable “docker_tags” {
type = list(string)
default = [“latest”] # Default value if no input is provided
}
variable “docker_username” {
type = string
default = “” # Default value (empty)
}
variable “docker_password” {
type = string
default = “” # Default value (empty)
}
variable “image_version” {
type = string
default = “1.0.0”
}
source “docker” “ubuntu” {
image = “ubuntu:20.04”
commit = true
}
build {
name = “test-build”
sources = [“source.docker.ubuntu”]
provisioner “shell” {
execute_command = “sh -c ‘{{ .Vars }} {{ .Path }}’”
inline = [“apt-get update && apt-get install -y sudo lsb-release wget”] #added
}
// Create folder to store temporary data
provisioner “shell” {
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
inline = [“mkdir {local.image_folder}", "chmod 777 {local.image_folder}”]
}
provisioner “file” {
destination = “{local.helper_script_folder}"
source = "{path.root}/…/scripts/helpers”
}
// Add apt wrapper to implement retries
provisioner “shell” {
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
script = “${path.root}/…/scripts/build/configure-apt-mock.sh”
}
// Install MS package repos, Configure apt
provisioner “shell” {
environment_vars = [“HELPER_SCRIPTS={local.helper_script_folder}", "DEBIAN_FRONTEND=noninteractive"]
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'"
scripts = [
"{path.root}/…/scripts/build/install-ms-repos.sh”,
“${path.root}/…/scripts/build/configure-apt.sh”
]
}
// Configure limits
provisioner “shell” {
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
script = “${path.root}/…/scripts/build/configure-limits.sh”
}
provisioner “file” {
destination = “{local.installer_script_folder}"
source = "{path.root}/…/scripts/build”
}
provisioner “file” {
destination = “{local.image_folder}"
sources = [
"{path.root}/…/assets/post-gen”,
“${path.root}/…/scripts/tests”
]
}
provisioner “file” {
destination = “{local.installer_script_folder}/toolset.json"
source = "{path.root}/…/toolsets/${local.toolset_file_name}”
}
provisioner “shell” {
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
inline = [“mv {local.image_folder}/post-gen {local.image_folder}/post-generation”]
}
// Generate image data file
provisioner “shell” {
environment_vars = [“IMAGE_VERSION={var.image_version}", "IMAGEDATA_FILE={local.imagedata_file}”]
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
scripts = [“${path.root}/…/scripts/build/configure-image-data.sh”]
}
// Create /etc/environment, configure waagent etc.
provisioner “shell” {
environment_vars = [“IMAGE_VERSION={var.image_version}", "IMAGE_OS={local.image_os}”, “HELPER_SCRIPTS={local.helper_script_folder}"]
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'"
scripts = ["{path.root}/…/scripts/build/configure-environment.sh”]
}
provisioner “shell” {
environment_vars = [“DEBIAN_FRONTEND=noninteractive”, “HELPER_SCRIPTS={local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER={local.installer_script_folder}”]
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
scripts = [“${path.root}/…/scripts/build/install-apt-vital.sh”]
}
provisioner “shell” {
environment_vars = [“DEBIAN_FRONTEND=noninteractive”, “HELPER_SCRIPTS={local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER={local.installer_script_folder}”]
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
scripts = [“${path.root}/…/scripts/build/install-powershell.sh”]
}
provisioner “shell” {
environment_vars = [“HELPER_SCRIPTS={local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER={local.installer_script_folder}”]
execute_command = “sudo sh -c ‘{{ .Vars }} pwsh -f {{ .Path }}’”
scripts = [“${path.root}/…/scripts/build/Install-PowerShellModules.ps1”]
}
provisioner “shell” {
environment_vars = [“DEBIAN_FRONTEND=noninteractive”, “HELPER_SCRIPTS={local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER={local.installer_script_folder}”]
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
scripts = [
“{path.root}/../scripts/build/install-git.sh",
"{path.root}/…/scripts/build/install-git-lfs.sh”,
“{path.root}/../scripts/build/install-github-cli.sh",
"{path.root}/…/scripts/build/install-zstd.sh”
]
}
provisioner “shell” {
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
pause_before = “1m0s”
scripts = [“${path.root}/…/scripts/build/cleanup.sh”]
start_retry_timeout = “10m”
}
provisioner “shell” {
environment_vars = [“HELPER_SCRIPT_FOLDER={local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER={local.installer_script_folder}”, “IMAGE_FOLDER={local.image_folder}"]
execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'"
scripts = ["{path.root}/…/scripts/build/configure-system.sh”]
}
provisioner “shell” {
execute_command = “sudo sh -c ‘{{ .Vars }} {{ .Path }}’”
inline = [“sleep 30”, “/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync”]
}
post-processors {
post-processor “docker-tag” {
repository = “shivams0702/test-packer-image”
tags = var.docker_tags # Use the variable here
}
post-processor “docker-push” {
login = true
login_username = var.docker_username # Use the variable here
login_password = var.docker_password # Use the variable here
}
}
}