Docker 容器总是显示 ssl 连接错误

新手上路,请多包涵

我开始使用 docker compose 并一直在研究简单的演示 flask 应用程序。问题是,我从一个组织内部运行它,该组织以这样一种方式拦截所有通信,即左右抛出 SSL 错误。他们为我们提供了三个我们需要安装的根证书,我通常已经在我自己的机器上运行了这些证书,但是当涉及到让它们在 docker-compose 部署中运行时我迷路了。

当我运行 docker-compose up 时,我得到以下信息:

 $ sudo docker-compose up
Creating network "project_default" with the default driver
Building web
Step 1/5 : FROM python:3.4-alpine
3.4-alpine: Pulling from library/python
81033e7c1d6a: Pull complete
9b61101706a6: Pull complete
415e2a07c89b: Pull complete
f22df7a3f000: Pull complete
8c16bf19c1f9: Pull complete
Digest: sha256:fe436cb066394d81cf49448a04dec7c765082445a500bc44f1ae5e8a455793bd
Status: Downloaded newer image for python:3.4-alpine
 ---> 5c72717ec319
Step 2/5 : ADD . /code
 ---> a5790c0e3e94
Removing intermediate container 052c614e41d0
Step 3/5 : WORKDIR /code
 ---> a2ea9acb3005
Removing intermediate container 77f2375ca0a6
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in 5f4fe856776d
Collecting flask (from -r requirements.txt (line 1))
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb0061f1d30>: Failed to establish a new connection: [Errno -3] Try again',)': /simple/flask/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb0061f19b0>: Failed to establish a new connection: [Errno -3] Try again',)': /simple/flask/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb0061f1828>: Failed to establish a new connection: [Errno -3] Try again',)': /simple/flask/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb0061f1588>: Failed to establish a new connection: [Errno -3] Try again',)': /simple/flask/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb0061f1390>: Failed to establish a new connection: [Errno -3] Try again',)': /simple/flask/
  Could not find a version that satisfies the requirement flask (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for flask (from -r requirements.txt (line 1))

Pip 无法安装任何东西。

docker-compose.yml 文件如下所示:

 version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
  redis:
    image: "redis:alpine"

主要的 Dockerfile 如下所示:

 FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

在这种特殊情况下,有什么办法可以做到这一点吗?是否有针对此类问题的通用解决方案,允许我将 SSL 证书传递给部署的任何容器并使用它们?

原文由 Sean Lovell 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
2 个回答

这实际上不是一个特定于 docker 的问题:您实际上是在问“我如何在 Linux 下安装证书颁发机构”?无论您是在容器内部还是外部运行 ssl 客户端,答案都是一样的。

您的 Python 镜像基于 alpine,而 alpine 使用“ca-certificates”包来管理 CA 证书。要安装本地 CA 证书,您需要 (a) 将它们复制到 /usr/share/ca-certificates 目录和 (b) 运行 update-ca-certificates

例如,将类似这样的内容添加到您的 Dockerfile(在您的 pip install 之前):

 COPY company-ca.crt /usr/share/ca-certificates
RUN update-ca-certificates

原文由 larsks 发布,翻译遵循 CC BY-SA 3.0 许可协议

在我的例子中,我必须在我的 Dockerfile 中添加这些句子:

 COPY company.crt /usr/local/share/ca-certificates/company.crt
RUN update-ca-certificates
...
RUN pip install --cert /etc/ssl/certs/company.pem -r requirements.txt

您需要 .crt 格式的公司证书。当docker执行update-ca-certificates时,linux会在路径/etc/ssl/certs/下创建一个同名的.pem文件。它将在 SSL 中的 pip 之间转换网络。

原文由 Andrés M. Jiménez 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题