新建web项目,此时launchSettings.json文件为:
"iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:54372", "sslPort": 54373 }
此时sslPort不为0(开启https时)
http和https两种协议的端口都固定
Dockerfile文件中可以不暴露端口(EXPOSE可以不设置),如:
#EXPOSE 80 #EXPOSE 443
sslPort修改为0(关闭https时)坑出现了
每次重新编译程序端口号会递增,无法固定。
Dockerfile文件中必须暴露端口,如:
EXPOSE 80
结论:docker调式时,不能把web项目launchSettings.json中的sslPort设置为0来关闭https,这样会使端口变成不可控。
完
-----------------------------------------------------------------------------------------------------------------------------------------------------------
发布时,必须设置EXPOSE
80和443两个默认端口都可以通过Dockerfile进行修改,EXPOSE 跟着改,如:
ENV ASPNETCORE_URLS http://+:81 EXPOSE 81
或
ENV ASPNETCORE_URLS http://+:81,https://+:444 EXPOSE 81 EXPOSE 444
另:vs+docker启动环境下,多种方式修改配置,环境变量始终为Development,代码内修改除外。
Production部分
启用https时docker-compose.override.yml文件为
aspnetcore环境变量修改为非Development时,因为代码中加入了app.UseHsts();且配置文件(用户机密)中无类似"Kestrel:Certificates:Development:Password": "7de8c379-3703-4683-9d33-fafd9540743c"的配置,若无证书,docker-compose启动会报错
此时内容为:
version: '3.4' services: dockerlinuxdemo.web: restart: always environment: - ASPNETCORE_ENVIRONMENT=Production - ASPNETCORE_URLS=https://+:443;http://+:80 - ASPNETCORE_HTTPS_PORT=38112 ports: - "38111:80" - "38112:443" volumes: - ./DockerLinuxDemo.Web/bin/Release/netcoreapp2.1/publish:/app - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
不启用https时修改ASPNETCORE_URLS即可,ASPNETCORE_HTTPS_PORT和ports并不影响:
version: '3.4' services: dockerlinuxdemo.web: restart: always environment: - ASPNETCORE_ENVIRONMENT=Production - ASPNETCORE_URLS=http://+:80 - ASPNETCORE_HTTPS_PORT=38112 ports: - "38111:80" - "38112:443" volumes: - ./DockerLinuxDemo.Web/bin/Release/netcoreapp2.1/publish:/app - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
docker-compose up --build -d 挺方便的