将 SSL 证书添加到 Docker Linux 容器

Tar*_*rta 7 ssl https certificate ssl-certificate docker

预期行为 能够从容器内进行 HTTPs 调用

实际行为

System.InvalidOperationException: IDX10803: Unable to obtain configuration from: 'https://identity.test/.well-known/openid-configuration'. ---> System.IO.IOException: IDX10804: Unable to retrieve document from: 'https://identity.test/.we
ll-known/openid-configuration'. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: SSL connect error
Run Code Online (Sandbox Code Playgroud)

信息 我认为问题是我的容器根本不知道此 http 调用需要使用的证书。所以我试图做的是通过 dockerfile 将它们提供给容器本身,如下所示:

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app

#Copy csproj and restore as distinct layers
COPY PublishOutput/. ./

FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app .

COPY Certificates/myCertificate.cer /usr/local/share/ca-certificates/myCertificate
RUN update-ca-certificates

ENTRYPOINT ["dotnet", "CaseApi.Web.dll"]
Run Code Online (Sandbox Code Playgroud)

PublishOutput文件夹中,我只拥有在 Docker 容器中运行所需的 .net core api 的所有 dll。

当我构建 dockerfile 时,它​​说:

Step 8/9 : RUN update-ca-certificates
 ---> Running in b025a42f2edc
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
Run Code Online (Sandbox Code Playgroud)

这让我认为我想要使用的证书并未真正被使用。我究竟做错了什么?提前致谢!

Tar*_*rta 3

应遵循的步骤:

1)确保证书的扩展名是.crt

2)用Notepad++或类似工具打开证书

3) 将证书复制到 /usr/local/share/ca-certificates/ 。update -ca-certificates命令从该文件夹读取证书:http://manpages.ubuntu.com/manpages/trusty/man8/update-ca-certificates.8.html

4)完成这些步骤后,构建 dockerfile 应该不会再显示0 添加,0 删除;添加了 1,删除了 0;或类似的,取决于您添加的证书数量

5)解决方案可能还没有出现。证书依赖于其他证书的层次结构。我在 Windows 中,通过转到证书管理器,我可以看到我的证书依赖于 2 个更高的证书(这显示在证书路径中):

认证路径

因此,您需要确保将层次结构中的所有证书放入 /usr/local/share/ca-certificates/ 中。

6) 尽管如此,您可能想通过正确的证书,但也许您没有。在我的例子中,IdentityServer 托管在 IIS 上,在绑定中,我可以看到 IdentityServer 确实期望通过 https 进行调用,通过双击绑定,我可以看到 IdentityServer 接受调用所需的证书。