How to update path in Windows container for Docker? - windows

I am trying to update the path inside my container. I have been everywhere and checked out several threads on this and nothing works. So, that's the trick?
# escape=`
ARG SDK_VERSION=4.8
FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}
ENV NODE_VERSION=8.11.2
HEALTHCHECK NONE
RUN Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object
System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:/Foo/bin"
USER ContainerUser
SHELL ["cmd", "/S", "/C"]
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
This is the error:
PS C:\users\cbongiorno\source> docker build -t mercury:latest -m 4GB -f Dockerfile i18n-tools
Sending build context to Docker daemon 2.057MB
Step 1/10 : ARG SDK_VERSION=4.8
Step 2/10 : FROM mcr.microsoft.com/dotnet/framework/sdk:${SDK_VERSION}
---> 4a9d58026a2d
Step 7/10 : RUN setx /M PATH "%PATH%;C:/Foo/bin"
---> Running in 1caf9e758af2
SUCCESS: Specified value was saved.
C:/Foo/bin : The term 'C:/Foo/bin' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:96
+ ... ogressPreference = 'SilentlyContinue'; setx /M PATH %PATH%;C:/Foo/bin
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:/Foo/bin:String) [], ParentCo
ntainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; setx /M PATH "%PATH%;C:/Foo/bin"' returned a non-zero code: 1

There are at least two ways to update the PATH:
Using setx as the earlier answer suggests and as is displayed in the golang's nanoserver-1809/Dockerfile. Notice the use of the USER instruction. The ContainerAdministrator must be used in order to set the system PATH:
# PATH isn't actually set in the Docker image, so we have to set it from within the container
USER ContainerAdministrator
RUN setx /m PATH "%GOPATH%\bin;C:\go\bin;%PATH%"
USER ContainerUser
# doing this first to share cache across versions more aggressively
Using Powershell as displayed in the golang's windowsservercore-ltsc2016/Dockerfile
RUN $newPath = ('{0}\bin;C:\go\bin;{1}' -f $env:GOPATH, $env:PATH); \
Write-Host ('Updating PATH: {0}' -f $newPath); \
[Environment]::SetEnvironmentVariable('PATH', $newPath, [EnvironmentVariableTarget]::Machine);
# doing this first to share cache across versions more aggressively
Both ways do work with recent Docker Desktop.
The ENV instruction is not going to work on Windows.

This is what I got working, but it doesn't seem ideal:
RUN setx /M PATH $($Env:PATH + ';C:\Foo\bin')

Related

How do I run bash commands under windows server 2019 with powershell remoting?

I have build a powershell script to run commands remotely. Nothing really to it, just open a session, invoke-command, do some logging and exit. That script is called remoteExecute.ps1
It is working fine as intended when I run it the outputs are what to be expected.
I'm having trouble running shell scripts using bash command. I have a server on which I installed WSL2 Linux distribution Ubuntu 20. When I log in on the server I can open a terminal window and run the bash command to execute a shell script without issues.
BUT, when I call this remotely from the powershell script it returns an error:
PS D:\batch> .\remoteExecute.ps1 -computer myserver -command "bash -c /mnt/c/somefile.sh"**
Opening new section on remote computer myserver
**The term 'bash -c myshellscript.sh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.**
+ CategoryInfo : ObjectNotFound: (bash -c /mnt/c/somefile.sh:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : myserver
Executed: 'bash -c /mnt/csomefile.sh'
Closed session
Powershell version used on both machines is v7.1 and I defaulted it to windows by setting this registry key
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -name Shell -Value 'C:\Program Files\PowerShell\7\PowerShell.exe -noExit'
What am I missing?

Change shell to git bash.exe in windows docker container

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"]

Can't install DataDog MSI in Dockerfile

From everything I have read, I should be able to do this:
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
ADD https://ddagent-windows-stable.s3.amazonaws.com/datadog-agent-7-latest.amd64.msi C:\datadog-agent-7-latest.amd64.msi
RUN Start-Process msiexec.exe -Wait -ArgumentList '/qn /i C:\datadog-agent-7-latest.amd64.msi APIKEY=abc123 /L*V C:\install.log'
It looks like it built correctly:
> docker build -t test .
Sending build context to Docker daemon 4.608kB
Step 1/4 : FROM mcr.microsoft.com/windows/servercore:ltsc2019
---> 71d3f266f3af
Step 2/4 : SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
---> Running in 1d931a586756
Removing intermediate container 1d931a586756
---> 277770d39d39
Step 3/4 : ADD https://ddagent-windows-stable.s3.amazonaws.com/datadog-agent-7-latest.amd64.msi C:/datadog-agent-7-latest.amd64.msi
Downloading [==================================================>] 146.9MB/146.9MB
---> 93223b61dad3
Step 4/4 : RUN Start-Process msiexec.exe -Wait -ArgumentList '/qn /i C:\datadog-agent-7-latest.amd64.msi APIKEY="abc123" /L*V C:\install.log'
---> Running in 619d7bb20f3b
Removing intermediate container 619d7bb20f3b
---> ddc461e4525a
Successfully built ddc461e4525a
Successfully tagged test:latest
But when I check in the container to see if it installed in C:\Program Files\Datadog, I am not seeing any of the files I am expecting from the installation.
I added the flag to give the install some extra logs (/L*V C:\install.log) but didn't see much in there. I have confirmed the msiexec command works on a Windows host, just not in the Docker build from what I can tell. Is there something simple that I'm missing?

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

Docker Volume Mounting issues with Ruby

I have a testcase where I am running an instance of Ruby inside a windows docker container. I should stress the ruby script works fine outside of docker. When run inside the docker container the script also works fine, except when the target of the Ruby file's access is on a mounted volume. In which case I get an error. I can for the testcase of course work around this issue by copying the file, but that would cause complications for the real script that found this problem.
Here's an example transcript, cbh_test is the mounted volume
PS C:\> echo ""> bob
PS C:\> cp cbh_test\failer.rb .
PS C:\> C:\Ruby25-x64\bin\ruby.exe .\failer.rb
PS C:\> cd cbh_test
PS C:\cbh_test> echo ""> bob
PS C:\cbh_test> C:\Ruby25-x64\bin\ruby.exe .\failer.rb
Traceback (most recent call last):
./failer.rb: Invalid argument # rb_readlink - C:/cbh_test (Errno::EINVAL)
Docker instance invoked by:
docker run -it -v c:\Users\me\work\fred:c:\cbh_test bob/failer powershell
Dockerfile:
# escape=`
FROM microsoft/windowsservercore:ltsc2016
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
COPY install_ruby.ps1 c:
RUN powershell c:\install_ruby.ps1
failer.rb:
def main(args)
fragment = "bob"
$tmp = File.open(File.join(File.dirname(__FILE__), fragment), 'r+').read
end
main($ARGV)
install_ruby.ps:
$RUBY_VERSION = "2.5.3-1"
$RUBY_RELEASE = "2.5.3-1"
$url = ('https://github.com/oneclick/rubyinstaller2/releases/download/rubyinstaller-{0}/rubyinstaller-{1}-x64.exe'-f $RUBY_VERSION, $RUBY_RELEASE);
$exe = "ruby-install.exe"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
Invoke-WebRequest -Uri $url -OutFile $exe;
$args = "/silent /tasks='assocfiles,modpath'"
Start-Process -FilePath $exe -ArgumentList $args -PassThru -Wait
I tried for several days and the only solution I was able to find to this problem was to use ltsc2019.

Resources