【问题标题】:Run docker service on HTTPS在 HTTPS 上运行 docker 服务
【发布时间】:2018-06-12 06:02:32
【问题描述】:

目前,我使用以下文件运行一个简单的 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 来访问我的应用程序。

我需要遵循哪些步骤?

【问题讨论】:

    标签: docker docker-compose docker-swarm


    【解决方案1】:

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

    此图像上有 IIS。

    1. 从此脚本将自签名证书添加到映像:

    certificate.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\0.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
    

    【讨论】:

    • 什么是certificate.ps1?您是否将 Nginx 作为容器运行?请描述您的整个设置
    • 我使用的图像中包含 IIS(Web 服务器)。 Certificate.ps1 执行以下操作。 1. 创建自签名证书。 2. 将其安装在本地证书存储中。 3. 创建 HTTPs 绑定并将生成的 SelfSign 证书添加到具有我的 Web 应用程序的默认网站。希望这会有所帮助。你可以对 Nginx 做同样的事情,但语法会有所不同。
    • 非常感谢:)
    • 我也有同样的场景,比如我的应用程序在 linux 映像容器中运行,我想用 https 访问我的站点我应该怎么做,就像我理解在 docker 和 docker compose 中公开端口 443文件,但接下来,如何运行脚本?从哪里?
    【解决方案2】:
    1. 您需要配置您的 Web 服务器(在 docker 应用程序内)以启用 HTTPS。
    2. 在 docker 上打开 SSL 端口 (443)

      • 您可以考虑使用 NGINX 作为 Web 服务器的反向代理,并在 nginx 中配置 SSL
      • 另一方面,如果这是一个公共站点,您可以查看letsencrypt 来为您的域获取免费的SSL 证书。

    【讨论】:

    • 嗨杰罗姆,我将访问我在 docker 容器内的 https 服务器,从 docker 主机本地调用 https://localhost:9010/。在这种情况下是否也应该使用 NGINX 或letsencrypt?谢谢
    • 如果不是公共站点,它是本地机器的呢
    • 我也有同样的场景,比如我的应用程序在 linux 映像容器中运行,我想用 https 访问我的站点我应该怎么做,就像我理解在 docker 和 docker compose 中公开端口 443文件,但接下来,如何运行脚本?从哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多