簡體   English   中英

curl 證書在 docker 容器中失敗

[英]curl certificate fail in docker container

我在公司coporate proxy后面有一Ubuntu 18.04 server

我設置了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

curl 未能驗證服務器的合法性,因此無法與其建立安全連接。 要了解有關此情況的更多信息以及如何修復它,請訪問上述 web 頁面。

在容器外,它工作正常。 我還在另一台不在代理后面並且在容器內工作的服務器上嘗試了完全相同的操作。

所以我想這是 docker 守護進程的配置問題。 或者也許我錯了......解決方案是什么?

https://curl.haxx.se/ca/cacert.pem下載最新的 cacert.pem,更好的方法是在 dockerfile 中添加一個步驟來安裝證書作為構建步驟的一部分。

按照步驟安裝

  1. https://curl.haxx.se/ca/cacert.pem下載文件
  2. 將文件重命名為 cacert.crt
  3. 將文件復制到 Go 到 /usr/local/share/ca-certificates/
  4. 運行命令sudo update-ca-certificates

您需要將 SSL 證書安裝到 Ubuntu 容器中。 例如,在正在運行的實例上,您可以執行以下操作:

apt-get update
apt-get install ca-certificates

然后,您的所有 HTTPs 連接都可以使用 CA 根證書的本地副本進行驗證。

對於生產部署,此命令應位於 Dockerfile 中:

RUN \
  apt-get update && \
  apt-get install ca-certificates && \
  apt-get clean

編輯

您的代理可能具有不受信任的證書。 您可以將其添加到捆綁包中,或者告訴curl不要使用curl --proxy-insecure檢查代理證書。

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

從版本 7.52.0 開始,curl 可以對代理進行 HTTPS 與與服務器的連接分開。 此 TLS 連接與服務器連接分開處理,因此您使用 --proxy-insecure 和 --proxy-cacert 代替 --insecure 和 --cacert 來控制證書驗證。 使用這些選項,您可以確保 TLS 連接和代理的信任可以與與服務器的 TLS 連接完全分開。

不要將 /etc/:/etc/ 掛載到 docker 容器。 主機中的 /etc/ 不適用於 docker 容器。 讓 docker 容器使用自己的 /etc/。

如果您嘗試通過具有 SSL 攔截證書的計算機進行連接,或者有時使用試圖提供幫助的病毒掃描程序,則可能會遇到此問題。

您可能需要添加證書,請參閱此問題How to add trusted root CA to Docker alpine

如果它可以幫助任何人,我的修復步驟是:

  • 通過 chrome 中的鎖定圖標並導出證書來下載ROOT公司證書。 確保你 select 要導出的鏈的根
  • docker cp corp-cert.crt /usr/local/share/ca-certificates/
  • 容器內的update-ca-certificates

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM