在Azure应用程序服务中的容器启动时启动Powershell脚本



我正在尝试使用Azure应用程序服务容器来托管Azure DevOps管道代理。从某种意义上说,我的代理使用Docker Desktop在本地运行得很好,但当我将映像发布到应用程序服务时,启动命令永远不会执行。我被迫在容器中获得一个控制台,并手动运行powershell脚本,然后它按预期工作。

这是我的docker文件:

FROM mcr.microsoft.com/windows/servercore:ltsc2019
RUN powershell Install-PackageProvider -Name NuGet -Force
RUN powershell Install-Module PowershellGet -Force
RUN powershell Install-Module -Name Az -Repository PSGallery -Force
RUN powershell Install-Module -Name Az.Tools.Migration -Repository PSGallery -Force
RUN powershell Enable-AzureRMAlias
WORKDIR /azp
COPY start.ps1 .
CMD powershell "c:azpstart.ps1"

部署中心日志显示没有错误。就好像CMD从未运行过一样。

请更换powershell CMD行,如下所示-

CMD ["powershell.exe", "-File", "c:azpstart.ps1"]

如果exe在path中不存在,那么使用它的完整路径也总是很好的。

也使用如下的工作目录-

FROM mcr.microsoft.com/windows/servercore:ltsc2019
RUN powershell Install-PackageProvider -Name NuGet -Force
RUN powershell Install-Module PowershellGet -Force
RUN powershell Install-Module -Name Az -Repository PSGallery -Force
RUN powershell Install-Module -Name Az.Tools.Migration -Repository PSGallery -Force
RUN powershell Enable-AzureRMAlias
WORKDIR c:azp
COPY start.ps1 .
CMD ["powershell.exe", "-File", "c:azpstart.ps1"]

我没有尝试构建docker映像,但您应该研究ENTRYPOINT是如何定义的,以及它如何与CMD交互。

请参阅官方指南。

根据docker文档,如果希望始终执行命令,则必须在ENTRYPOINT中传递,而不是在CMD中传递。在CMD中传递命令时,在执行容器时,它可以被参数覆盖。

CMD将在使用替代参数运行容器时被重写。

所以我不知道您是如何运行容器的,但我建议您尝试在ENTRYPOINT中传递脚本。

类似的东西:

FROM mcr.microsoft.com/windows/servercore:ltsc2019
RUN powershell Install-PackageProvider -Name NuGet -Force
RUN powershell Install-Module PowershellGet -Force
RUN powershell Install-Module -Name Az -Repository PSGallery -Force
RUN powershell Install-Module -Name Az.Tools.Migration -Repository PSGallery -Force
RUN powershell Enable-AzureRMAlias
WORKDIR c:azp
COPY start.ps1 .
ENTRYPOINT ["powershell.exe", "c:\azp\start.ps1"]

最新更新