Change shell to git bash.exe in windows docker container - windows

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.

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

Why does terminal stop after run "bash" in a .bat file (WSL) and how to run it properly?

I would like to automatize a process running a .bat file but using the bash terminal in my WSL2. These are my commands in my .bat:
bash
cd ~/Dev/Folder
nvm use 14.15.0
npm start
But the .bat stops after running "bash". I also tried with bash && cd ~/Dev/Folder && nvm use 14.15.0 && npm start and also replacing "bash" with "wsl", but same result.
Maybe I'm doing something wrong, so I would appreciate some help with this.
bash starts a new Bash shell and waits for it to exit, like for any other terminal command. The subsequent commands are not redirected to Bash - they'll be executed once the Bash process exits.
To run commands within Bash, use the -c flag:
bash -c "cd ~/Dev/Folder && ls && nvm use 14.15.0 && npm start"
Also see What does 'bash -c' do?.

RUN powershell failing in docker build on windows 2019 server

I am running on Windows 2019 server.
I am getting an error whenever I invoke powershell from a dockerfile on docker build
Error is..
---> Running in 6efa29aa8a4a
The command 'powershell -Command DIR' returned a non-zero code: 3221226505
Dockerfile..
# escape=` (backtick)
FROM mcr.microsoft.com/windows/servercore:ltsc2019
RUN DIR
RUN ["powershell", "-Command", "DIR"]
COPY ./ app/
WORKDIR app
CMD [ "someapp", "somearg" ]
I have tried replacing cmd with powershell via
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN DIR
and the results are the same.
Thanks
Try running a dockerfile more like
FROM mcr.microsoft.com/windows/servercore/iis
RUN powershell -NoProfile -Command Remove-Item -Recurse
C:\inetpub\wwwroot*
WORKDIR /inetpub/wwwroot
COPY . /inetpub/wwwroot
maybe you gotta move your COPY command after the WORKDIR ?
Try different things out
Check to make sure you have update KB4532691 installed on your build machine (and host). The newest image of ltsc2019 (20/2020) has issues without it.
See https://hub.docker.com/_/microsoft-windows-servercore and https://support.microsoft.com/en-us/help/4542617/you-might-encounter-issues-when-using-windows-server-containers-with-t for more info

When using Shell in Windows, how to solve "sh: D:/DevTools/Git/root/restart_all.sh: No such file or directory"?

I've wrote a shell script file on mac to deploy some docker application. But when I copy it to Windows10, it seems that it cannot run properly.
The path after "docker exec xx /foo/bar" is always transferred into an absolute path in windows, and leads to an error like: "sh: D:/DevTools/Git/root/start_zk.sh: No such file or directory".
What's more, when executing "docker exec xx /foo/bar" in powershell or git bash, it can run properly. This problem only occurs when I write it into a file, and run it through "sh deploy.sh".
I've tried to add "\" before "/", but it doesn't help. My code is listed below.
echo "deploying zookeepers..."
docker exec ac sh /root/start_zk.sh
Is there any fault in my script? The command "sh /root/start_zk.sh" after "docker exec" should be a parameter for the docker container to execute, but it seems that it is recognized by windows as a command or path. Is there any way to solve this?

Why is my "docker-compose run" not working properly on windows?

So I am running a script that calls:
docker-compose run --rm web sh /data/bin/install_test_db.sh
and it seems to work fine when running it on ubuntu but when I run it on Windows using Git Bash I get this error:
sh: 0: Can't open C:/Program Files/Git/data/bin/install_test_db.sh
It seems to be trying to run the script on my machine and not the actual container from which I am trying to call it from.
Any ideas on why this is happening?
First, try passing a command to your sh shell:
docker-compose run --rm web sh -c "/data/bin/install_test_db.sh"
Or:
docker-compose run --rm web "sh -c /data/bin/install_test_db.sh"
That will avoid your host shell (the git bash) to interpret an absolute path (starting with '/') as one from the Git installation path.
That will keep /data/... as an argument to be passed to the shell executed in the container.
Note: If you are using Docker for Windows (meaning not VirtualBox, but HyperV), you don't need git bash at all.
Try the same command from a regular CMD (with docker.exe in your %PATH%, which Docker for Windows set for you)
vonc#VONCAVN7 C:\Users\vonc
> where docker
C:\Program Files\Docker\Docker\Resources\bin\docker.exe
If you need Linux-like commands from that same CMD session, then yes, add git paths:
set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%

Bash brace expansion not working on Dockerfile RUN command

I'm running the following RUN command in my Dockerfile, expecting a "logs" directory to be created under each of the listed subdirectories:
RUN mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs
But when I check the image, I see a directory literally called "{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}" created under /opt/seagull, instead of brace expansion taking place.
What could I be doing wrong?
You're not using brace expansion, because you're not using Bash. If you look at the documentation for the RUN command:
RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
And also:
Note: To use a different shell, other than ‘/bin/sh’, use the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-c", "echo hello"]
So, just change the command to use the exec form and explicitly use a Bash shell:
RUN [ "/bin/bash", "-c", "mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs" ]
If /bin/bash is available in your image, you can change the shell that the docker build system uses to execute your RUN command, like this:
SHELL ["/bin/bash", "-c"]
Now, your RUN command should work unchanged.

Resources