我有一个在 docker 容器内运行的 django 应用程序。然而,虽然应用程序在容器内运行时在我的本地 PC 内运行得非常好,但它无法发出任何请求,例如调用某些 API 甚至
pip install
。所有这些操作都会引发类似 [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)
的错误。
我获得
pip install
工作的唯一方法就是做RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org --no-cache-dir -r requirements.txt
尝试过的解决方案:
rootCA.pem
文件复制到容器中的 /usr/local/share/ca-certificates/
RUN update-ca-certification
alpine
,buster
,slim-buster
)还有很多记不清了
我不想只是关闭验证,因为这是不好的做法,所以我试图保持验证。我已将我的
Dockerfile
包含在下面。
Dockerfile:
FROM python:3.11.2-slim-buster
ENV PYTHONUNBUFFERED=1
WORKDIR /app
RUN apt-get update && apt-get install -y ca-certificates
COPY certs/rootCA.pem /usr/local/share/ca-certificates/
RUN update-ca-certificates
COPY requirements.txt requirements.txt
RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
你可以尝试一些事情。首先,验证 rootCA.pem 的权限。如果容器内运行应用程序进程的用户无法访问它,则 ssl 验证将失败。 (你可以试试
RUN chmod 644 /usr/local/share/ca-certificates/rootCA.pem
)
您可以做的另一件事是使用 openssl 验证证书链:
openssl verify -CAfile <full_path_of_rootCA.pem> <full_path_of_certificate.pem>
这是一个例子:
openssl verify -CAfile certs/rootCA.pem certs/certificate.pem
证书必须有
.crt
扩展名。
COPY certs/rootCA.pem /usr/local/share/ca-certificates/rootCA.crt