問題描述
PowerShell 的 Invoke‑WebRequest 在 Docker 容器中不起作用 (PowerShell's Invoke‑WebRequest not working within a Docker Container)
在我的 dockerfile 中有一行 RUN PowerShell ‑ExecutionPolicy Unrestricted ‑File /approot/bin/DockerScripts/reqs.ps1
在 reqs.ps1 中有一個調用‑像這樣的 webrequest 命令, Invoke‑WebRequest ‑Uri https://path/to/exe/ExeNeeded.exe ‑OutFile C:\ExeNeeded.exe
當我運行 docker build
在我的計算機上我在 RUN PowerShell ‑ExecutionPolicy Unrestricted ‑File /approot/bin/DockerScripts/reqs.ps1
步驟中遇到錯誤
錯誤是:
Invoke‑WebRequest : The remote name could not be resolved:
https://path/to/exe/ExeNeeded.exe
At C:\approot\bin\DockerScripts\reqs.ps1:5 char:1
- Invoke‑WebRequest ‑Uri https://path/to/exe/ExeNeeded.exe
~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt
pWebRequest) [Invoke‑WebRequest], WebException
- FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
ll.Commands.InvokeWebRequestCommand
</code></pre> 我可以通過我的網絡瀏覽器訪問https://path/to/exe/ExeNeeded.exe
,所以我知道這個網址很好。我還暫時關閉了計算機上的防火牆。我不知道為什麼
參考解法
方法 1:
It is known issue#6453 for windows users. Add network parameter when building the image.
docker build ‑t myProject . ‑‑network "Default Switch"
參考文件