hashicorp / packer-plugin-docker

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

docker builder: double backslash breaks exec form of changes #12

Open ghost opened 3 years ago

ghost commented 3 years ago

This issue was originally opened by @Yoshiiiiiii as hashicorp/packer#9092. It was migrated here as a result of the Packer plugin split. The original body of the issue is below.


The docker builder for windows images behaves unexpected when trying to change the ENTRYPOINT and CMD of the image. Double backslash breaks exec form of changes and turns it into shell form. Escaping the backslash with another backslash felt like a thing that should have worked.

Overview of the Issue

I was trying to build an image that executes a powershell script on startup. I tried to change the ENTRYPOINT and the CMD the following way:

"changes": [
    "ENTRYPOINT [\"powershell\", \"-NoExit\", \"-Command\", \"Set-Location\", \"C:/Setup\", \";\"]",
    "CMD [\".\\Start.ps1\"]"
]

This resulted in the following image:

"Cmd": [
    "cmd /S /C [\".\\Start.ps1\"]"
],
"ArgsEscaped": true,
"Image": "mcr.microsoft.com/windows/servercore:1809",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
    "powershell",
    "-NoExit",
    "-Command",
    "Set-Location",
    "C:/Setup",
    ";"
],

So it turned the change of the CMD into shell form. "CMD [\"./Start.ps1\"]" does work. It took a while to figure this out. I couldn't find anything in the docs that adresses this. Maybe it was just me approaching this the wrong way.

Before doing it correct I tried this:

"changes": [
  "ENTRYPOINT [\"powershell\", \"-NoExit\", \"-Command\", \".\\Start.ps1\", \";\"]",
  "WORKDIR C:/Setup"
]

which resulted into this via docker inspect:

"WorkingDir": "C:\\Setup",
"Entrypoint": [
  "cmd /S /C [\"powershell\", \"-NoExit\", \"-Command\", \".\\Start.ps1\"]"
]

So the WORKINGDIR is translated into double backslashes from docker. And you can see that the backslashes also break the exec form of the ENTRYPOINT.

Reproduction Steps

Try to escape '\' in a change like this '\\' and the docker builder is not able to detect the exec form of the CMD. Same with the ENTRYPOINT.

Packer version

From packer_1.5.5_windows_amd64.

Simplified Packer Buildfile

{
  "builders": [
    {
      "type": "docker",
      "image": "mcr.microsoft.com/windows/servercore:1809",
      "container_dir": "c:/Temp",
      "windows_container": true,
      "commit": true,
      "changes": [
        "ENTRYPOINT [\"powershell\", \"-NoExit\", \"-Command\", \"Set-Location\", \"C:/Setup\", \";\"]",
        "CMD [\".\\Start.ps1\"]"
      ]
    }
  ],
  "provisioners": [
    {
      "type": "powershell",
      "inline": [
        "New-Item -Path 'c:\\' -Name 'Setup' -ItemType 'directory'"
      ]
    },
    {
      "type": "file",
      "source": "{{ template_dir }}/Start.ps1",
      "destination": "C:/Setup/Start.ps1"
    }
  ]  
}

Operating system and Environment details

Windows Server 2019 Datacenter

azr commented 3 years ago

I think this should be way better with HCL2 config files. Please tell me if that is not the case. We'll soon close this issue otherwise.

mloskot commented 1 year ago

I've noticed the same issue in my.pkr.hcl

changes = [
    "ENTRYPOINT [\"powershell.exe\", \"-NoProfile\", \"-ExecutionPolicy\", \"Bypass\", \"-Command\", \"C:\\entrypoint.ps1\", \";\"]"
  ]

which generates

"Entrypoint": [
      "cmd /S /C [\"powershell.exe\", \"-NoProfile\", \"-ExecutionPolicy\", \"Bypass\", \"-Command\", \"C:\\entrypoint.ps1\", \";\"]"
  ],

Changing my.pkr.hcl to

changes = [
  "ENTRYPOINT [\"powershell.exe\", \"-NoProfile\", \"-ExecutionPolicy\", \"Bypass\", \"-Command\", \"Set-Location\", \"C:/\", \";\"]",
    "CMD [\"./entrypoint.ps1\"]"
]

seems to be the right workaround and it generates correct syntax:

"Cmd": [
    "./entrypoint.ps1"
],
"Entrypoint": [
    "powershell.exe",
    "-NoProfile",
    "-ExecutionPolicy",
    "Bypass",
    "-Command",
    "Set-Location",
    "C:/",
    ";"
],

By the way, there is this old issue which also touches on this topic, but the documented syntax does not seem working, https://github.com/hashicorp/packer/issues/8225

$ packer version
Packer v1.8.7
$ packer plugins installed
C:\Users\MateuszL\AppData\Roaming\packer.d\plugins\github.com\hashicorp\docker\packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe