docker 容器中的 curl 证书失败

twe*_*sat 8 ssl curl docker ubuntu-18.04

我有Ubuntu 18.04 server一个coporate proxy.

我设置http_proxyhttps_proxy环境变量。

服务器正在运行Docker 19.03,它也被配置为使用 http_proxy 和 https_poxy。

如果运行docker run -it ubuntu:18.04,在容器内,我可以执行apt updateapt install curl -y

然后我可以做类似的事情curl www.google.com

但它不适用于https

root@1b6abfb4ff90:/# curl -v -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     
0*   Trying 10.30.88.14...
* TCP_NODELAY set
* Connected to xxx (10.30.88.14) port 8080 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to raw.githubusercontent.com:443
> CONNECT raw.githubusercontent.com:443 HTTP/1.1
> Host: raw.githubusercontent.com:443
> User-Agent: curl/7.58.0
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 200 Connected
< 
* Proxy replied 200 to CONNECT request
* CONNECT phase completed!
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* CONNECT phase completed!
* CONNECT phase completed!
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [91 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [2741 bytes data]
* TLSv1.2 (OUT), TLS alert, Server hello (2):
} [2 bytes data]
* SSL certificate problem: unable to get local issuer certificate
* stopped the pause stream!
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Closing connection 0
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html
Run Code Online (Sandbox Code Playgroud)

curl 未能验证服务器的合法性,因此无法与其建立安全连接。要了解有关这种情况及其解决方法的更多信息,请访问上述网页。

在容器之外,它工作正常。我还在另一台不在代理后面的服务器上尝试了完全相同的方法,它在容器内工作。

所以我想这是docker deamon的配置问题。或者也许我错了......解决方案是什么?

emi*_*emi 15

您需要将 SSL 证书安装到 Ubuntu 容器中。例如,在正在运行的实例上,您可以执行以下操作:

apt-get update
apt-get install -y ca-certificates
Run Code Online (Sandbox Code Playgroud)

然后,您的所有 HTTPs 连接都可以使用 CA 根证书的本地副本进行验证。

对于生产部署,此命令应位于 Dockerfile 中:

RUN \
  apt-get update && \
  apt-get install -y ca-certificates && \
  apt-get clean
Run Code Online (Sandbox Code Playgroud)

编辑

您的代理可能具有不受信任的证书。您可以将其添加到捆绑包中,或者告诉curl不要使用 来检查代理的证书curl --proxy-insecure

来自https://curl.se/docs/sslcerts.html

从版本 7.52.0 开始,curl 可以与服务器的连接分开对代理进行 HTTPS。此 TLS 连接与服务器连接分开处理,因此您可以使用 --proxy-insecure 和 --proxy-cacert 来代替 --insecure 和 --cacert 来控制证书验证。使用这些选项,您可以确保 TLS 连接和代理的信任可以与服务器的 TLS 连接完全分开。


Sou*_*jee 7

从https://curl.haxx.se/ca/cacert.pem下载最新的 cacert.pem ,更好的方法是在 dockerfile 中添加一个步骤来安装证书作为构建步骤的一部分。

按照步骤安装

  1. 从https://curl.haxx.se/ca/cacert.pem下载文件
  2. 将文件重命名为 cacert.crt
  3. 将文件复制到 转到 /usr/local/share/ca-certificates/
  4. 运行命令sudo update-ca-certificates