运行 docker HTTPS 服务

Run docker service on HTTPS

目前,我使用以下文件 运行 一个简单的 docker 容器。

Docker文件

FROM microsoft/aspnet:4.7.1
WORKDIR /inetpub/wwwroot
EXPOSE 80
COPY index.html .

docker-compose.yml

version: '3.4'

services:

testapp:
  image: mytestapp:${TAG:-latest}
build:
  context: .
  dockerfile: Dockerfile

docker-compose.override.yml

version: '3.4'

services:
  testapp:
   ports:
    - "9091:80"

我使用 windows 图像通过以下命令创建我的容器,我可以通过 http://localhost:9091/ 访问它。

docker-compose -f docker-compose.yml -f docker-compose.override.yml build

我想使用 HTTPS 而不是 http 来访问我的应用程序。

我需要遵循哪些步骤?

  1. 您需要配置 Web 服务器(在 docker 应用程序内)以启用 HTTPS。
  2. 在 docker

    上打开 SSL 端口 (443)
    • 您可以考虑使用 NGINX 作为您的网络服务器的反向代理,并在 nginx 中配置 SSL
    • 另一方面,如果这是一个 public 站点,您可以查看 letsencrypt 为您的域获取免费的 SSL 证书。

感谢杰罗姆的回答。我做了以下事情来让 https 在我的容器上工作。我希望这可能对某人有所帮助。

这张图片上有 IIS。

  1. 将自签名证书添加到此脚本中的图像:

证书.ps1

  1. 创建自签名证书。
  2. 将其安装在本地证书存储区。
  3. 创建 HTTPs 绑定并将生成的 SelfSign 证书添加到具有我的 Web 应用程序的默认网站
import-module webadministration

cd cert:
$cert = New-SelfSignedCertificate -DnsName myweb -Friendlyname MyCert -CertStoreLocation Cert:\LocalMachine\My

$rootStore = New-Object System.Security.Cryptography.X509Certificates.X509Store -ArgumentList Root, LocalMachine

$rootStore.Open("MaxAllowed")
$rootStore.Add($cert)
$rootStore.Close()

cd iis:
new-item -path IIS:\SslBindings[=10=].0.0.0!443 -value $cert
New-WebBinding -Name "Default Web Site" -IP "*" -Port 443 -Protocol https
iisreset
  1. 我的 docker-compose.override.yml 文件中的更改:添加了端口 443。
   version: '3.4'
     services:
       testapp.svc:
         ports:
           - "9091:80"
           - "9092:443"
  1. 我的 Dockerfile 中的更改
    FROM microsoft/aspnet:4.7.1
    WORKDIR /inetpub/wwwroot
    EXPOSE 80 
    EXPOSE 443
    COPY index.html .
    COPY certificate.ps1 .
    RUN powershell.exe ./certificate.ps1