Change shell to git bash.exe in windows docker container

Viewed 762

I want to change the shell to bash.exefrom git-for-windows:

I have the docker file:

FROM mcr.microsoft.com/windows/servercore:1809

# $ProgressPreference: https://github.com/PowerShell/PowerShell/issues/2138#issuecomment-251261324
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install -y git

RUN & 'C:/Program Files/Git/bin/sh.exe' -c "ls -al"  #### << Passes
SHELL ["\"C:\\Program Files\\Git\\bash.exe\"", "-c"] #### << Passes
RUN ls -al                                           #### << Crashes

Results in the third statement: /usr/bin/bash: Files/Git/bin/sh.exe: No such file or directory

When I try to quote like the following

SHELL ["\"C:\\Program Files\\Git\\bash.exe\"", "-c"]
RUN ls -al

the build works, but I dont see any output from ls -al and I dont know if it really works?

How can I use bash.exe successfully? I am using docker for windows through WSL2 engine.

1 Answers

I ran into a similar issue using base image mcr.microsoft.com/windows:2004. I am not very familiar with windows command prompt or powershell.

After you install git with chocolatey, update the system PATH environment variable. I used cmd for this because it was the first way I found on stack overflow and it has worked. I have not tested the run command with a path containing a space so the below command might not work out of the box for you.

SHELL ["cmd", "/S", "/C"]

RUN setx path "C:\\Program Files\\Git\\bin;%path%"

With the system path environment variable updated, bash.exe is available at the command line.

SHELL ["bash.exe", "-c"]

RUN "ls -la"

ENTRYPOINT ["bash.exe"]
Related