hashicorp / packer-plugin-docker

Packer plugin for Docker Builder
https://www.packer.io/docs/builders/docker
Mozilla Public License 2.0
31 stars 25 forks source link

When provisioning a docker container, it is not clear how to use the instance_id parameter in the provisioner(s) #68

Open flixman opened 2 years ago

flixman commented 2 years ago

Overview of the Issue

I have a packer file containing the following:

[....]
source "docker" "Test" {
    image        = "centos:7"
    export_path  = "test.tar"
}
[....]
build {
  sources = ["source.docker.Test"]
  provisioner "shell" {
    inline = ["echo 'proxy=http://<proxy_url>' >> /etc/yum.conf", "rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7", "yum install -y python3"]
  }

  provisioner "ansible" {
    extra_arguments = [
      "-e", "proxy_url='http://<proxy_url>'",
      "-e", "ansible_connection=docker"
    ]
    playbook_file   = "playbooks/GoldenImage.yml"
    user            = "root"
  }
}

Ansible fails the first time that tries to connect to the docker container (when doing the initial host scan, called "facts gathering" in Ansible). And I suspect is because the contents of the inventory file are like

default ansible_host=127.0.0.1 ansible_user=root ansible_port=42779

By setting the host_alias parameter in ansible provisioner I can work around this situation. However, I do not get to see how can I reference the builder.instance_id variable from the config file.

Plugin and Packer version

packer version: 1.7.4 I do not know how to get the plugin versions

flixman commented 2 years ago

@azr Excuse me for naming you explicitly here. I had posted a similar bug in the packer-plugin-ansible, but my following findings have made me think I should be reporting this question about how to reference the instance_id from the packer file. This is really blocking me to continue with my work, and.... I do not know who else can I turn to. Can you/somebody give me a hand?

andrey-rjeutski-tfs commented 2 years ago

@flixman Does build.ID work for you? It should output container id.

  provisioner "powershell" {
    inline = ["Write-Output '${build.ID}'"]
  }