hashicorp / hashicorp/packer-plugin-docker

docker builder: double backslash breaks exec form of changes

Open
#12 2 comments 0 reactions 0 assignees View on GitHub
bug builder/docker stage/waiting-reply track-internal
Dominant language
Go
Stars
40
Forks
29
PR merge metrics
No merged PRs in 30d

Description

_This issue was originally opened by @Yoshiiiiiii as hashicorp/packer#9092. It was migrated here as a result of the [Packer plugin split](https://github.com/hashicorp/packer/issues/8610#issuecomment-770034737). 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.