Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup $env:Path in pwsh.exe in powershell:nanoserver-1803 container? #88

Closed
kkbruce opened this issue Nov 22, 2018 · 5 comments
Closed

Comments

@kkbruce
Copy link

kkbruce commented Nov 22, 2018

I want build some application use mcr.microsoft.com/powershell:nanoserver-1803, but can't set $env:PATH by SetEnvironmentVariable method.

How to setup $env:Path in pwsh.exe in powershell:nanoserver-1803 container?

Steps to reproduce

Sample Dockerfile

# escape=`

FROM mcr.microsoft.com/powershell:nanoserver-1803
SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ENV	REDIS_DIR="C:\Redis;"

# Test1
RUN	$env:PATH = $env:REDIS_DIR + $env:PATH; `
    [Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine)

Expected behavior

SetEnvironmentVariable is work.

[Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine)

Actual behavior

Show "Requested registry access is not allowed." error message.

docker build -t kkbruce/redis-test .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM mcr.microsoft.com/powershell:nanoserver-1803
 ---> cbf80f683980
Step 2/4 : SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
 ---> Running in 4a78633d3ffb
Removing intermediate container 4a78633d3ffb
 ---> b3becf9915a2
Step 3/4 : ENV  REDIS_DIR="C:\Redis"
 ---> Running in 1823528f2e6d
Removing intermediate container 1823528f2e6d
 ---> 6e947483c825
Step 4/4 : RUN  $env:PATH = $env:REDIS_DIR + $env:PATH;     [Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine)
 ---> Running in ea1135de36b8
Exception calling "SetEnvironmentVariable" with "3" argument(s): "Requested registry access is not allowed."
At line:1 char:116
+ ...  $env:PATH; [Environment]::SetEnvironmentVariable('PATH', $env:PATH,  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : SecurityException

The command 'pwsh -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; $env:PATH = $env:REDIS_DIR + $env:PATH;     [Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine)' returned a non-zero code: 1

Environment data

> docker info
Client: Docker Engine - Community
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:47:51 2018
 OS/Arch:           windows/amd64
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.24)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:56:41 2018
  OS/Arch:          windows/amd64
  Experimental:     true
> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.1
PSEdition                      Core
GitCommitId                    6.1.1
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@TravisEz13
Copy link
Member

Set the user variable and it will have the same effect on nano server. Sorry, nano server is weird.

@TravisEz13
Copy link
Member

Feel free to mention me on any of the issues you file in this repo. It can take me some time to get around to looking at the issues otherwise (the fact that I've talked on this thread is equivalent now.)

@kkbruce
Copy link
Author

kkbruce commented Dec 4, 2018

Hi
Nano server base image remove PowerShell is good and bad, good is size small, good just for .net core, other all is bad. cmd.exe CLI is very bad experience. sorry, let me complain.

So, I need to PowerShell to do some thing, especially environment variables.

@TravisEz13
Copy link
Member

Yeah, you just got one line of code wrong.

[Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine)

is not supported on nanoserver, but

[Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::User)

Should work and do what you want, per the nanoserver team. I hit the same issue making the image.

@kkbruce
Copy link
Author

kkbruce commented Dec 26, 2018

Hi @TravisEz13

[EnvironmentVariableTarget]::User is work, but funny.

Dockerfile

# escape=`
FROM mcr.microsoft.com/powershell:nanoserver-1803
SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ENV	REDIS_DIR="C:\Redis;"

# User
RUN	$ENV:PATH = $ENV:REDIS_DIR + $env:PATH; `
    Write-Host $ENV:PATH; `
    [Environment]::SetEnvironmentVariable('PATH', $ENV:PATH, [EnvironmentVariableTarget]::User)

RUN Write-Host $ENV:PATH

ENTRYPOINT ["pwsh"]

Build Step

docker build --no-cache -t kkbruce/pwshenv .
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM mcr.microsoft.com/powershell:nanoserver-1803
 ---> cbf80f683980
Step 2/6 : SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
 ---> Running in 708766ff9480
Removing intermediate container 708766ff9480
 ---> 2dd87ba74fc9
Step 3/6 : ENV  REDIS_DIR="C:\Redis;"
 ---> Running in 558e0da92d92
Removing intermediate container 558e0da92d92
 ---> 1279514f824f
Step 4/6 : RUN  $ENV:PATH = $ENV:REDIS_DIR + $env:PATH;     Write-Host $ENV:PATH;     [Environment]::SetEnvironmentVariable('PATH', $ENV:PATH, [EnvironmentVariableTarget]::User)
 ---> Running in ce234316ba30
C:\Redis;C:\Windows\system32;C:\Windows;C:\Users\ContainerUser\AppData\Local\Microsoft\WindowsApps;C:\Program Files\PowerShellPowerShell
Removing intermediate container ce234316ba30
 ---> ac1fdc4ef9bc
Step 5/6 : RUN Write-Host $ENV:PATH
 ---> Running in 17e527d3721c
C:\Windows\system32;C:\Windows;C:\Redis;C:\Users\ContainerUser\AppData\Local\Microsoft\WindowsApps;C:\Program Files\PowerShellRemoving intermediate container 17e527d3721c
 ---> e57d0ebb8125
Step 6/6 : ENTRYPOINT ["pwsh"]
 ---> Running in d60265fd455b
Removing intermediate container d60265fd455b
 ---> 7832ac72f63b
Successfully built 7832ac72f63b
Successfully tagged kkbruce/pwshenv:latest

Step 4/6: setting is work.
Step 5/6: C:\Windows\system32;C:\Windows; always restore go line first.

Thanks your help.

@kkbruce kkbruce closed this as completed Dec 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants