在Docker容器中运行Chocolatey失败

15

我在Windows Server 2016上安装了Docker。Dockerfile包含一些通过chocolatey安装的构建工具。每次尝试从该Dockerfile构建镜像时,都会失败。容器内没有运行chocolatey工具。

# Use the latest Windows Server Core image. 
FROM microsoft/windowsservercore

ENV chocolateyUseWindowsCompression false

RUN powershell -Command \
        iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')); \
        choco feature disable --name showDownloadProgress

RUN choco install visualstudio2015professional 
RUN choco install qtcreator 
RUN choco install curl 
RUN choco install jq 
RUN choco install 7zip.install 
RUN choco install jfrog-cli 
RUN choco install jom

Build command here.........
    C:\Program Files\Docker>docker build -t test -f Dockerfile.txt .
    Sending build context to Docker daemon  54.73MB
    Step 1/10 : FROM microsoft/windowsservercore
    latest: Pulling from microsoft/windowsservercore
    3889bb8d808b: Pull complete
    fb1ebf2c42b6: Pull complete
    Digest: sha256:750440935dd3ef8ea148a8e4f83a0397540a8014938ae7b59eb78211da1d5969
    Status: Downloaded newer image for microsoft/windowsservercore:latest
     ---> 7d89a4baf66c
    Step 2/10 : ENV chocolateyUseWindowsCompression false
     ---> Running in 8a7b1fc97da5
     ---> 0f3c89daf01c
    Removing intermediate container 8a7b1fc97da5
    Step 3/10 : RUN powershell -Command     iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'));     choco feature disable --name showDownloadProgress
     ---> Running in f7088454db37
    Exception calling "DownloadString" with "1" argument(s): "Unable to connect to
    the remote server"
    At line:1 char:1
    + iex ((new-object net.webclient).DownloadString('https://chocolatey.or ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : WebException

    choco : The term 'choco' is not recognized as the name of a cmdlet, function,
    script file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At line:1 char:88
    + ... .DownloadString('https://chocolatey.org/install.ps1')); choco feature ...
    +                                                             ~~~~~
        + CategoryInfo          : ObjectNotFound: (choco:String) [], CommandNotFou
       ndException
        + FullyQualifiedErrorId : CommandNotFoundException

    The command 'cmd /S /C powershell -Command     iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'));     choco feature disable --name showDownloadProgress' returned a non-zero code: 1
5个回答

11

我之前遇到过这个问题。有一段时间我很苦恼,因为我无法理解为什么一个Docker镜像可以正常构建而另一个却不行。

最终,我发现这是由于受限的TLS问题造成的,新版本的Windows docker基础镜像需要启用TLS1.2,但默认情况下未启用。你可能在使用Windows Server Core基础容器时也会遇到这个问题。

Chocolatey文档在其安装受限制的TLS部分提到了这种情况。

他们目前的解决方法是在重新设置TLS设置之前进行少量的音乐椅操作-请参见以下内容。

$securityProtocolSettingsOriginal = [System.Net.ServicePointManager]::SecurityProtocol

try {
  # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
  # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
  # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
  # installed (.NET 4.5 is an in-place upgrade).
  [System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
} catch {

  Write-Warning 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to do one or more of the following: (1) upgrade to .NET Framework 4.5 and PowerShell v3, (2) specify internal Chocolatey package location (set $env:chocolateyDownloadUrl prior to install or host the package internally), (3) use the Download + PowerShell method of install. See https://chocolatey.org/install for all install options.'
}

iex ((New-Object 
System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

[System.Net.ServicePointManager]::SecurityProtocol = $securityProtocolSettingsOriginal

如果失败了,可以使用docker run --name mycontainer -d [your container id]命令运行没有choco的容器,然后使用docker exec -it mycontainer powershell命令进入交互式shell,这样您就能够交互式地运行choco安装程序以获取有关失败的更多信息。


4

你是否从https://github.com/chocolatey/choco/issues/1055中了解到以下内容:

SET chocolateyUseWindowsCompression='false' REM No spaces in the equals
@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

以下是最相关的问题:Powershell无法连接到互联网


3

对我来说,这个问题是由我的杀毒软件引起的,特别是Symantec。只要禁用它,问题就解决了。


1

我成功地在公司网络中使用代理设置从网上安装了choco。

第一步是创建一个proxy.ps1文件:

$ProxyAddress = "http://proxy:port"
[system.net.webrequest]::defaultwebproxy = New-Object system.net.webproxy($ProxyAddress)
$CredCache = [System.Net.CredentialCache]::new()
$NetCreds = [System.Net.NetworkCredential]::new("username","password","")
$CredCache.Add($ProxyAddress, "Basic", $NetCreds)
[system.net.webrequest]::defaultwebproxy.credentials = $CredCache
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true

然后在Dockfile中,这样做:

FROM mcr.microsoft.com/windows:1809-amd64 AS base
SHELL ["cmd", "/S", "/C"]

# add proxy to powershell profile for all users
ADD proxy.ps1 C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
# Install Chocolatey
RUN powershell -ExecutionPolicy unrestricted -Command `
    iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

真正的英雄。我花了好几个小时尝试各种方法在公司代理下在我的Windows Docker上安装Chocolatey。复制/粘贴这些内容并更改代理变量,只用了30秒就完成了。 - user1024792

-1

关键在于错误信息:

"无法连接到远程服务器"

您的Docker容器没有互联网连接,无法下载Chocolatey安装脚本。


当我从Docker Hub或Windows Service Core镜像中拉取其他镜像(例如hello-world)时,它可以正常工作。我已经设置了代理设置。是否有任何Docker命令来验证Internet连接的状态? - user2301

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接