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

/bin/sh: 0: Can't open node #61

Closed hc-github-team-packer closed 3 years ago

hc-github-team-packer commented 3 years ago

This issue was originally opened by @floydspace in https://github.com/hashicorp/packer/issues/11150 and has been migrated to this repository. The original issue description is below.


Hi dear HashiCorp team.

I'm first day user of packer, willing to figure out how does it work. But it's already midnight and I cannot crack an error /bin/sh: 0: Can't open node when I run a container.

here is my pkg.hcl

source "docker" "example" {
  image = "node:14"
  commit = true
  changes = [
    "WORKDIR /app",
    "CMD [ \"node\", \"server.js\" ]"
  ]
}

build {
  sources = ["source.docker.example"]

  provisioner "file" {
    sources = ["./server.js"]
    destination = "/tmp/"
  }

  provisioner "shell" {
    inline = [
      "mkdir /app",
      "mv -f /tmp/server.js /app/"
    ]
  }

  post-processor "docker-tag" {
    repository = "local/node"
    tags = ["latest"]
  }
}

so I build it using command packer build pkr.hcl and then running docker run -it --rm local/node:latest which fails with the error

thank you

azr commented 3 years ago

Hey @floydspace thanks for opening, I would pass the server.js option not in the CMD part, but as a parameter or ENTRYPOINT, as in:

(well, I don't have a server.js file)

$ docker run -it --rm node:latest server.js
node:internal/modules/cjs/loader:930
  throw err;
  ^

Error: Cannot find module '/server.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
    at Function.Module._load (node:internal/modules/cjs/loader:772:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
floydspace commented 3 years ago

Hey @azr thank you for the feedback. I used your suggestion and played with it and I could make it work with this config:

source "docker" "example" {
  image = "node:14"
  commit = true
  changes = [
    "WORKDIR /app",
    "ENTRYPOINT [\"docker-entrypoint.sh\"]"
  ]
}

build {
  sources = ["source.docker.example"]

  provisioner "file" {
    sources = ["./server.js"]
    destination = "/tmp/"
  }

  provisioner "shell" {
    inline = [
      "mkdir /app",
      "mv -f /tmp/server.js /app/"
    ]
  }

  post-processor "docker-tag" {
    repository = "local/node"
    tags = ["latest"]
  }
}

and command docker run -it --rm local/node:latest server.js works for me.

As you can see I had to overwrite the ENTRYPOINT in changes with the original entrypoint form the base image. Is it correct behavior? because I expected that if you are not using changes the base image options would be used.

And as a side question, is it correct way to upload my source code to the image, so I copy it in tmp and then move to the workdir inside image? thank you

azr commented 3 years ago

@floydspace, I would actually do all changes in the changes part, to make use of Docker's caching. I would copy directly to the location I intend to use things, so from my local drive to /app/. I think that NodeJS needs to be able to read your code and its own code to do things, so yeah, you'll need everything, potentially making that Docker image big. But, that could be totally fine, depending on how fast the internet is 🙂 .

I think your issue is solved, so I'm going to close this issue. Cheers !