Program.exe类似于:
int main()
{
printf("Hey, there!\n");
return 0;
}
Dockerfile就像:FROM mcr.microsoft.com/windows/servercore:2004
USER ContainerAdministrator
COPY Debug/. ./bin/
COPY log/. ./log/
RUN Powershell.exe -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x86.exe -OutFile 'c:\vc_redist.x86.exe' ; Start-Process c:\vc_redist.x86.exe -ArgumentList '/passive' -RedirectStandardOutput 'c:\Windows\System32' -Wait ; Remove-Item c:\vc_redist.x86.exe -Force
当我运行容器并导航到C:/ bin以执行Program.exe时,没有任何内容输出到控制台,并且Program.exe退出时没有错误。Program.exe依赖的所有DLL和Lib都可以(通过procmon和dependency walker验证),并且vcredist已正确安装。
docker logs
命令什么都不记录。Program.exe在主机中可以正常执行。
在这种情况下如何打印到控制台?
最佳答案
当构建镜像而不是在容器运行时使用ENTRYPOINT或CMD执行RUN。看看最好的做法here
FROM mcr.microsoft.com/windows/servercore:2004
USER ContainerAdministrator
COPY Debug/. ./bin/
COPY log/. ./log/
RUN Powershell.exe -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x86.exe -OutFile 'c:\vc_redist.x86.exe' ; Start-Process c:\vc_redist.x86.exe -ArgumentList '/passive' -RedirectStandardOutput 'c:\Windows\System32' -Wait ; Remove-Item c:\vc_redist.x86.exe -Force
CMD C:/bin/Program.exe
您可以使用ENTRYPOINT来设置主命令,并使用CMD来设置默认选项,以遵循最佳实践ENTRYPOINT ["command"]
CMD ["param", "param"]
关于windows - 从Docker容器在exe内部执行打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63349803/