hashicorp / packer-plugin-docker

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

error with provisioning scripts in docker #176

Closed K4ybedenler closed 5 months ago

K4ybedenler commented 6 months ago

packer version 1.0.9.

similar problem in such tickets: 7578, 2697

I'm going with the tutorial, theme "Add provisioner to template"

my docker-ubuntu.pkr.hcl is:

packer {
  required_plugins {
    docker = {
      version = ">= 1.0.8"
      source = "github.com/hashicorp/docker"
    }
  }
}

source "docker" "ubuntu" {
  image  = "ubuntu:jammy"
  commit = true
}

build {
  name    = "learn-packer"
  sources = [
    "source.docker.ubuntu"
  ]
  provisioner "shell" {
    environment_vars = [
      "FOO=hello world",
    ]
    inline = [
      "echo Adding file to Docker Container",
      "echo \"FOO is $FOO\" > example.txt",
    ]
  }
}

everything ok on the build stage:

❯ packer build docker-ubuntu.pkr.hcl
learn-packer.docker.ubuntu: output will be in this color.

==> learn-packer.docker.ubuntu: Creating a temporary directory for sharing data...
==> learn-packer.docker.ubuntu: Pulling Docker image: ubuntu:jammy
    learn-packer.docker.ubuntu: jammy: Pulling from library/ubuntu
    learn-packer.docker.ubuntu: Digest: sha256:a6d2b38300ce017add71440577d5b0a90460d0e57fd7aec21dd0d1b0761bbfb2
    learn-packer.docker.ubuntu: Status: Image is up to date for ubuntu:jammy
    learn-packer.docker.ubuntu: docker.io/library/ubuntu:jammy
==> learn-packer.docker.ubuntu: Starting docker container...
    learn-packer.docker.ubuntu: Run command: docker run -v /home/kay/.config/packer/tmp4060350213:/packer-files -d -i -t --entrypoint=/bin/sh -- ubuntu:jammy
    learn-packer.docker.ubuntu: Container ID: a4166ea59cfc8501a4f2b0dde53a90d02dba8e7e650c0aa62a06bd01385d7c40
==> learn-packer.docker.ubuntu: Using docker communicator to connect: 172.17.0.2
==> learn-packer.docker.ubuntu: Provisioning with shell script: /tmp/packer-shell3856444975
    learn-packer.docker.ubuntu: Adding file to Docker Container
==> learn-packer.docker.ubuntu: Committing the container
    learn-packer.docker.ubuntu: Image ID: sha256:d2ec7a28d25a7474f7304bb1abe4474d8cb1d4906119a32e416cecc67cf1aa81
==> learn-packer.docker.ubuntu: Killing the container: a4166ea59cfc8501a4f2b0dde53a90d02dba8e7e650c0aa62a06bd01385d7c40
Build 'learn-packer.docker.ubuntu' finished after 2 seconds 963 milliseconds.

==> Wait completed after 2 seconds 963 milliseconds

==> Builds finished. The artifacts of successful builds are:
--> learn-packer.docker.ubuntu: Imported Docker image: sha256:d2ec7a28d25a7474f7304bb1abe4474d8cb1d4906119a32e416cecc67cf1aa81

but after it failes to run:

❯ docker run -it d2ec7a28d25a
/bin/bash: 1: Syntax error: "(" unexpected

in 1.0.8 everything is ok

lbajolet-hashicorp commented 6 months ago

Hi @K4ybedenler,

This issue being tied to the Docker plugin, I transferred it over.

I'm not sure what's the error you're getting from, could you docker inspect the image by any chance? I suspect the entrypoint was left as-is from the origin in v1.0.8, which is something that changed with v1.0.9 (#167 is where we introduced that change), looking back at it, someone else reported this change being problematic for them, so this is something we need to address.

In the meantime, I believe you can workaround the bug either by fixing the plugin version to 1.0.8, or by defining your own entrypoint/cmd in the template you're using.

Sorry about that blunder, we'll try to push a fix ASAP.

K4ybedenler commented 6 months ago

could you docker inspect the image by any chance?

sure, here is:

[
    {
        "Id": "sha256:57fcfba27f8637f891b4c895e5a4c42a46af93223210e565cf42fab45628d1c2",
        "RepoTags": [],
        "RepoDigests": [],
        "Parent": "sha256:52882761a72a60649edff9a2478835325d084fb640ea32a975e29e12a012025f",
        "Comment": "",
        "Created": "2024-05-16T19:53:12.679823765Z",
        "DockerVersion": "26.1.2",
        "Author": "",
        "Config": {
            "Hostname": "e2367c1e61fd",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "ubuntu:jammy",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/bin/sh"
            ],
            "OnBuild": null,
            "Labels": {
                "org.opencontainers.image.ref.name": "ubuntu",
                "org.opencontainers.image.version": "22.04"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 77859496,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/37c05d2353952625c162c829b1ef5b76ddbc89c81ff1263e5772168f4f3829f5/diff",
                "MergedDir": "/var/lib/docker/overlay2/08438d4ceb7e88397825f143107a3ce08beaa689f7f20d4e42ca45ff0a7c291c/merged",
                "UpperDir": "/var/lib/docker/overlay2/08438d4ceb7e88397825f143107a3ce08beaa689f7f20d4e42ca45ff0a7c291c/diff",
                "WorkDir": "/var/lib/docker/overlay2/08438d4ceb7e88397825f143107a3ce08beaa689f7f20d4e42ca45ff0a7c291c/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:629ca62fb7c791374ce57626d6b8b62c76378be091a0daf1a60d32700b49add7",
                "sha256:24359aea1b3db3432f4af2488e76609e07b72f814f6c0c2b98c6a1e1454d6dcc"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
lbajolet-hashicorp commented 5 months ago

Hi @K4ybedenler,

I managed to take a look at this, and I think I have a fix that should work for your situation on the PR I mention in this thread. If you can, might I ask you to build the plugin on this branch, install it, and try again with your template? I tried it locally and it does seem to work, but one can never be too sure, and I would appreciate if you were able to test it before we can release.

Thanks!

K4ybedenler commented 5 months ago

@lbajolet-hashicorp hi, sorry that didn't answer earlier! but it seems fixed. thanks for your job!