简体   繁体   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. I am trying to set up SSL for a ASP.NET web application that is running in a docker for windows container. I am running on Windows 10.我在 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

Below is my dockerfile:下面是我的 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;"

Problem: Docker fails with the error that selfCert.pfx is not found.问题:Docker 失败,错误为找不到 selfCert.pfx。

I tried moving all these commands to a powershell script and tried running that too.我尝试将所有这些命令移动到 powershell 脚本并尝试运行它。 Somehow docker/powershell cannot find my.ps1 file too.不知何故 docker/powershell 也找不到 my.ps1 文件。

When I connect to the container manually, I can see the files in the expected location.当我手动连接到容器时,我可以在预期位置看到文件。 I can also execute.PS1 successfully.我也可以成功执行.PS1。 The problem occurs only when its executed from the dockerfile.该问题仅在从 dockerfile 执行时出现。

I tried USER ContainerAdministrator, also different ways of calling PowerShell with -Bypass -File but unable to run it from dockerfile.我尝试了 USER ContainerAdministrator,以及使用 -Bypass -File 调用 PowerShell 的不同方式,但无法从 dockerfile 运行它。

Need some help in getting this run and identify why this is happening.需要一些帮助才能运行并确定发生这种情况的原因。

Thanks.谢谢。

I was also facing the same issue.我也面临同样的问题。 As per my investigation, there is one .dockerignore file within the same directory which is causing these file path related restriction.根据我的调查,同一目录中有一个.dockerignore文件导致这些文件路径相关的限制。 We would require to mention these relative path ignore syntax in the .dockerignore file.我们需要在.dockerignore文件中提及这些相对路径忽略语法。

I have used Publish solution feature in visual studio then copied the content from Publish folder to the container working directory through docker file.我在 Visual Studio 中使用了发布解决方案功能,然后通过 docker 文件将内容从发布文件夹复制到容器工作目录。

Below steps have been followed:已执行以下步骤:

  1. In Visual Studio, Publish the application solution in Publish folder: /bin/Release/Publish .在 Visual Studio 中,在 Publish 文件夹中发布应用程序解决方案: /bin/Release/Publish
  2. In dockerfile, use COPY command to copy the items from Publish folder( /bin/Release/Publish/ )[ instead of docker folder ] to container working directory.[ Command: COPY /bin/Release/Publish/.在 dockerfile 中,使用 COPY 命令将项目从 Publish 文件夹(/bin/Release/Publish/)[而不是 docker 文件夹]复制到容器工作目录。[命令:COPY /bin/Release/Publish/。 ] ]
  3. Include the above relative path ignore syntax in.dockerignore file like .bin\Release\Publish*.在 .dockerignore 文件中包含上述相对路径忽略语法,例如.bin\Release\Publish*。
  4. Specify the.pfx file path in the SSL import powershell command in the docker file.在docker文件中的SSL import powershell命令中指定.pfx文件路径。 [Command: -FilePath C:\inetpub\wwwroot\selfCert.pfx OR -FilePath./selfCert.pfx ] [命令: -FilePath C:\inetpub\wwwroot\selfCert.pfx-FilePath./selfCert.pfx ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从主机访问网站时的HTTP状态403.14,但可在Docker容器中使用 - HTTP status 403.14 when trying to access website from host machine, but works inside Docker container 尝试从数据库到数据网格显示数据时出现“系统找不到指定的文件”错误 - “The system cannot find the specified file” error when trying to display data from database to datagrid asp.net mvc:从IIS运行应用程序时,InitializeSimpleMembershipAttribute引发异常,但从Visual Studio运行应用程序时,它工作正常 - asp.net mvc: InitializeSimpleMembershipAttribute throwing exception when app is run from IIS, but works fine when app is run from Visual Studio 当在类库中执行File.Exists但从MVC项目中调用File.Exists时,文件应该放在哪里? - Where to place files when File.Exists is executed in a class library but called from a MVC project? 使用代码首次迁移时,无法通过Visual Studio中的服务器资源管理器手动在表中插入数据 - cannot insert data in table manually through server explorer in visual studio when code first migration is used 上载文件时找不到资源错误 - Resource Cannot be found error when uploading a file 在本地运行的IIS7下运行asp.net MVC应用程序时出错 - Error when running asp.net MVC application under IIS7 that works when run locally “错误:无法初始化OLE”当我尝试运行SPA时? - “Error: Cannot Initialize OLE” When I try to run a SPA? 使用构建定义文件从TFS 2013运行自动部署时,无法排除文件 - Unable to exclude files when automated deployment is run from TFS 2013 using build definition file 当应用程序以“开始而不调试”运行时,用户未被识别为已登录,当以“开始调试”运行时,用户身份验证工作正常 - User not recognized as logged in when application is run as Start Without Debugging, user authentication works perfectly when run as Start Debugging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM