将SSL证书添加到Docker Linux容器

时间:2018-07-13 11:01:44

标签: docker ssl https certificate ssl-certificate

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

实际行为

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

信息 我认为问题在于我的容器根本不知道此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"]

PublishOutput 文件夹中,我只有我需要在Docker容器中运行的.net核心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.

这使我认为我要使用的证书并未真正使用。我究竟做错了什么?预先感谢!

2 个答案:

答案 0 :(得分:1)

问题可能出在update-ca-certificates中。该命令仅处理扩展名为.crt的文件。在其手册页中:

  

证书必须具有.crt扩展名才能被   更新证书。

因此,只需在将证书复制到Dockerfile中时添加此扩展名即可:

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

答案 1 :(得分:0)

要遵循的步骤:

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中,转到证书管理器,可以看到我的证书依赖于两个更高的证书(在“证书路径”中显示):

Certification Path

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

6)尽管如此,您可能正在考虑通过正确的证书,但也许您没有。在我的情况下,IdentityServer托管在IIS上,在绑定中,我可以看到IdentityServer确实希望通过https进行调用,然后双击绑定,可以看到IdentityServer接受该调用所需的证书。