【问题标题】:Powershell cannot find file when run from dockerfile, but works when manually executed inside container从 dockerfile 运行时 Powershell 找不到文件,但在容器内手动执行时可以工作
【发布时间】:2020-06-09 13:33:45
【问题描述】:

我正在尝试为在 docker for windows 容器中运行的 ASP.NET Web 应用程序设置 SSL。我在 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 is not found。

我尝试将所有这些命令移动到一个 powershell 脚本并尝试运行它。不知何故 docker/powershell 也找不到我的 .ps1 文件。

当我手动连接到容器时,我可以在预期位置看到文件。我也可以成功执行.PS1。该问题仅在从 dockerfile 执行时出现。

我尝试了 USER ContainerAdministrator,以及使用 -Bypass -File 调用 PowerShell 的不同方式,但无法从 dockerfile 运行它。

需要一些帮助才能运行并确定发生这种情况的原因。

谢谢。

【问题讨论】:

    标签: asp.net-mvc powershell docker dockerfile docker-for-windows


    【解决方案1】:

    我也面临同样的问题。根据我的调查,同一目录中有一个 .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]

    【讨论】:

      猜你喜欢
      • 2016-12-25
      • 1970-01-01
      • 2022-11-23
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      相关资源
      最近更新 更多