簡體   English   中英

Powershell 從 dockerfile 運行時找不到文件,但在容器內手動執行時有效

[英]Powershell cannot find file when run from dockerfile, but works when manually executed inside container

I am trying to set up SSL for a ASP.NET web application that is running in a docker for windows container. 我在 Windows 10 上運行。

Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:23:10 2020
 OS/Arch:           windows/amd64
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.24)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:37:20 2020
  OS/Arch:          windows/amd64
  Experimental:     true

下面是我的 dockerfile:

FROM microsoft/aspnet:4.7.2-windowsservercore-1803
USER ContainerAdministrator

EXPOSE 443


ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .

RUN Add-WindowsFeature Web-Scripting-Tools

RUN Remove-WebSite -Name 'Default Web Site'

RUN New-Website -Name 'myApp' -IPAddress '*' -Port 443 -PhysicalPath C:\inetpub\wwwroot -ApplicationPool '.NET v4.5' -Ssl -SslFlags 0

#CMD ["powershell.exe", "-File", "AddCertificate.ps1"]

#RUN ["powershell", "C:\inetpub\wwwroot\AddCertificate.ps1"]

RUN powershell.exe -Command "\
   Import-Module IISAdministration; \
   Import-Module WebAdministration; \
  $pwd = ConvertTo-SecureString -String 'passw0rd!' -Force -AsPlainText; \

  # Import the certificate and store it in a variable to bind to later; \
 $cert = Import-PfxCertificate -Exportable -FilePath C:\inetpub\wwwroot\selfCert.pfx -CertStoreLocation cert:\localMachine\My -Password $pwd; \
  # Take the imported certificate and bind it to all traffic toward port 443 (you need to specify IP if you want multiple apps on 1 docker which I believe is ill-advised); \
  New-Item -Path IIS:\SslBindings\0.0.0.0!443 -value $cert;"

問題:Docker 失敗,錯誤為找不到 selfCert.pfx。

我嘗試將所有這些命令移動到 powershell 腳本並嘗試運行它。 不知何故 docker/powershell 也找不到 my.ps1 文件。

當我手動連接到容器時,我可以在預期位置看到文件。 我也可以成功執行.PS1。 該問題僅在從 dockerfile 執行時出現。

我嘗試了 USER ContainerAdministrator,以及使用 -Bypass -File 調用 PowerShell 的不同方式,但無法從 dockerfile 運行它。

需要一些幫助才能運行並確定發生這種情況的原因。

謝謝。

我也面臨同樣的問題。 根據我的調查,同一目錄中有一個.dockerignore文件導致這些文件路徑相關的限制。 我們需要在.dockerignore文件中提及這些相對路徑忽略語法。

我在 Visual Studio 中使用了發布解決方案功能,然后通過 docker 文件將內容從發布文件夾復制到容器工作目錄。

已執行以下步驟:

  1. 在 Visual Studio 中,在 Publish 文件夾中發布應用程序解決方案: /bin/Release/Publish
  2. 在 dockerfile 中,使用 COPY 命令將項目從 Publish 文件夾(/bin/Release/Publish/)[而不是 docker 文件夾]復制到容器工作目錄。[命令:COPY /bin/Release/Publish/。 ]
  3. 在 .dockerignore 文件中包含上述相對路徑忽略語法,例如.bin\Release\Publish*。
  4. 在docker文件中的SSL import powershell命令中指定.pfx文件路徑。 [命令: -FilePath C:\inetpub\wwwroot\selfCert.pfx-FilePath./selfCert.pfx ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM