繁体   English   中英

docker 无法使用 https 导航到网站

[英]docker unable to navigate to a web site using https

我有以下简单的 docker 文件:

FROM mcr.microsoft.com/windows/servercore:ltsc2019

WORKDIR /azp

COPY test.ps1 .

CMD powershell .\test.ps1

其中 test.ps1 是:

C:\test> cat .\test.ps1
curl https://cnn.com -UseBasicParsing

该脚本可以在我的机器上正常运行,但不能在 docker 容器中运行:

C:\test> docker build -t test:latest .
Sending build context to Docker daemon  75.26kB
Step 1/4 : FROM mcr.microsoft.com/windows/servercore:ltsc2019
 ---> 782a75e44953
Step 2/4 : WORKDIR /azp
 ---> Using cache
 ---> b43270631602
Step 3/4 : COPY test.ps1 .
 ---> Using cache
 ---> 10cfc66cff37
Step 4/4 : CMD powershell .\test.ps1
 ---> Using cache
 ---> 187be18c5495
Successfully built 187be18c5495
Successfully tagged test:latest
C:\test> docker run test
curl : The underlying connection was closed: Could not establish trust
relationship for the SSL/TLS secure channel.
At C:\azp\test.ps1:1 char:1
+ curl https://cnn.com -UseBasicParsing
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:Htt
   pWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
   ll.Commands.InvokeWebRequestCommand

请注意,用http://google.com ( http ) 替换https://cnn.com ( https ) 是有效的,所以这显然是关于 https 的。

我错过了什么?

聚苯乙烯

我正在使用 Windows 10,最近的 docker 切换到使用 Windows 容器。

您的容器似乎无法验证 TLS 服务器证书。 容器中可能缺少 CA 证书(也许它们在 Windows 中具有不同的技术术语)。

你可以:

  • -SkipCertificateCheck (可从 PowerShell V6.0+ 获得),因此将跳过 TLS 证书验证 - 开发的好选择,但它会部分牺牲安全性
  • “在容器中挂载 Windows 主机证书存储” - Docker 论坛

因此,我按照以下步骤设法使其适用于https://google.com

  1. 导航到https://google.com并检查根证书是什么。 它是带指纹的证书75E0ABB6138512271C04F85FDDDE38E4B7242EFE
  2. 将上述证书以及 ZScaler 根证书( D72F47D87420E3F0F9BDCAC6F03A566743C481B9D72F47D87420E3F0F9BDCAC6F03A566743C481B9到一个特殊目录,该目录将包含在C:\\containers下的图像C:\\containers
  3. 修改 test.ps1 脚本 - 见下文。
  4. 修改 Dockerfile 脚本 - 见下文。

测试.ps1

Get-ChildItem /certificates | ForEach-Object {
    $null = Import-Certificate -FilePath $_.FullName -CertStoreLocation Cert:\LocalMachine\Root
}

$res = Invoke-WebRequest https://google.com -UseBasicParsing
$res.StatusDescription

文件

FROM mcr.microsoft.com/windows/servercore:ltsc2019
COPY certificates certificates
WORKDIR /azp
COPY test.ps1 .
CMD powershell .\test.ps1

因此,在主机上我运行以下命令:

C:\test> $certs = dir Cert:\LocalMachine\Root |? { $_.Thumbprint -eq '75E0ABB6138512271C04F85FDDDE38E4B7242EFE' -or $_.Thumbprint -eq 'D72F47D87420E3F0F9BDCAC6F03A566743C481B9' }
C:\test> $certs


   PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\Root

Thumbprint                                Subject
----------                                -------
D72F47D87420E3F0F9BDCAC6F03A566743C481B9  E=support@zscaler.com, CN=Zscaler Root CA, OU=Zscaler Inc., O=Zscaler Inc., L=San Jose, S=California, C=US
75E0ABB6138512271C04F85FDDDE38E4B7242EFE  CN=GlobalSign, O=GlobalSign, OU=GlobalSign Root CA - R2


C:\test> $certs |% { Export-Certificate -FilePath "c:\test\certificates\$($_.Thumbprint).cer" -Cert $_ }


    Directory: C:\test\certificates


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         1/5/2020   8:40 PM           1239 D72F47D87420E3F0F9BDCAC6F03A566743C481B9.cer
-a----         1/5/2020   8:40 PM            958 75E0ABB6138512271C04F85FDDDE38E4B7242EFE.cer


C:\test> docker run test
OK
C:\test>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM