【问题标题】:Running chocolatey in docker container fails在 docker 容器中运行巧克力失败
【发布时间】:2019-01-02 04:15:36
【问题描述】:

我在 windows server 2016 上有 docker。Dockerfile 包含一些要通过巧克力安装的构建工具。每次我尝试从提到的 Dockerfile 构建图像时都会失败。巧克力工具未在容器中运行。

# 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

【问题讨论】:

    标签: docker windows-server-2016 chocolatey


    【解决方案1】:

    我前段时间遇到过这个问题。它摧毁了我一段时间,我无法弄清楚为什么我拥有的一个 Docker 镜像构建良好,而下一个却没有。

    我最终将其追溯到受限 TLS 的问题,即较新的 Windows docker 基础映像需要默认未启用的 TLS1.2。您可能会在使用 Windows Server 核心基础容器时遇到这种情况。

    Chocolatey 文档在其关于 installing-with-restricted-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 安装以获取有关失败的更多信息。

    【讨论】:

      【解决方案2】:

      对我来说,这被证明是我的防病毒软件,特别是在我的情况下是赛门铁克,一旦被禁用就可以工作。

      【讨论】:

        【解决方案3】:

        你研究过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 unable to connect to internet at all

        【讨论】:

          【解决方案4】:

          我设法通过代理设置在公司网络中从网络安装 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'))
          

          【讨论】:

            【解决方案5】:

            关键在错误信息中:

            "Unable to connect to the remote server"

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

            【讨论】:

            • 当我拉取其他图像时,例如来自 docker hub 或 windowsservicecore 映像的 hello-world 可以正常工作。我已经设置了代理设置。是否有任何 docker 命令来验证互联网连接状态?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-06-13
            • 1970-01-01
            • 1970-01-01
            • 2019-05-09
            • 2018-07-16
            • 1970-01-01
            • 2022-10-25
            相关资源
            最近更新 更多