Estoy tratando de ejecutar una aplicación basada en el marco ASP.NET Core 3.1 en un servidor Ubuntu (18.04.3 LTS) usando el contenedor Docker.
Creé el siguiente archivo docker-compose.yml
para poder ejecutar imágenes nginx-proxy
y private_image_name
en mi servidor. Obviamente, nginx-proxy
es un servidor proxy que será el proxy que enrutará el tráfico proveniente de la web a mis otras imágenes en ejecución. Seguí el artículo para la configuración nginx-proxy
.
version: '3.4' services: nginx-proxy: image: jwilder/nginx-proxy container_name: nginx-proxy ports: - 80:80 - 443:443 volumes: - /var/run/docker.sock:/tmp/docker.sock:ro - certificates:/etc/certificates private_image_name: image: private_image_name:latest container_name: private_image_name depends_on: - nginx-proxy environment: - VIRTUAL_HOST=sub.domain-example.com - ASPNETCORE_ENVIRONMENT=Production - ASPNETCORE_URLS=https://+:443;http://+:80 ports: - 51736:80 - 44344:443 volumes: - storage:/storage - /var/run/docker.sock:/tmp/docker.sock:ro - certificates:/etc/certificates - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro volumes: storage: certificates: networks: default: external: name: nginx-proxy secrets: server.cert: file: ./server.cert server.key: file: ./server.key
Los archivos server.cert
y server.key
se almacenan en /etc/certificates
. Ambos archivos fueron creados usando el siguiente comando
sudo openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 -subj "/C=US/ST=CA/L=SF/O=Docker-demo/CN=app.example.org" -keyout server.key -out server.cert
Intenté ejecutar mis dos imágenes ejecutando docker-composer up
. Sin embargo, el nginx-proxy
no presentó ningún problema y mientras private_image_name
no se pudo ejecutar. Lo siguiente es lo que obtengo cuando private_image_name
intenta iniciar
**WARNING**: The APPDATA variable is not set. Defaulting to a blank string. Recreating private_image ... done Attaching to private_image private_image | crit: Microsoft.AspNetCore.Server.Kestrel[0] private_image | Unable to start Kestrel. private_image | System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date. private_image | To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'. private_image | For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054. private_image | at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions) private_image | at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions) private_image | at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context) private_image | at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding) private_image | at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken) private_image | Unhandled exception. System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date. private_image | To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'. private_image | For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054. private_image | at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions) private_image | at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions) private_image | at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context) private_image | at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding) private_image | at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken) private_image | at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) private_image | at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) private_image | at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) private_image | at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) private_image | at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) private_image | at private_image.Program.Main(String[] args) in /src/private_image/Program.cs:line 17 private_image exited with code 139
El comando dotnet dev-certs https --trust
funciona solo en Windows y macOS.
Pregunta
¿Cómo puedo solucionar este problema en el servidor de Ubuntu? ¿Cómo puedo adjuntar correctamente el certificado SSL a la imagen de la ventana acoplable?
Además, cuando voy a http://server-ip-address o http://sub.domain-example.com obtengo
503 Servicio no disponible temporalmente nginx/1.17.5
Y cuando voy a https://server-ip-address o https://sub.domain-example.com obtengo
No puede conectarse.
Tan pronto como haya configurado el certificado en nginx, no veo ningún sentido habilitarlo en el contenedor principal de asp.net, ya que su red docker será visible para el público a través de nginx.
Para deshabilitar la escucha de Kestrel Https, simplemente elimine el puerto 443 del siguiente código:
- ASPNETCORE_URLS=https://+:443;http://+:80
Reemplázalo con:
- ASPNETCORE_URLS=http://+:80
Para las personas que vinieron aquí por un problema similar, esto me ayudó a resolver un problema:
Limpiar los certificados de desarrollo:
dotnet dev-certs https --clean
Crear uno nuevo
dotnet dev-certs https -t
En mi caso, los problemas principales fueron con el archivo docker-compose.override.yml
. Los archivos de Docker se generaron en una máquina con Windows, por lo que las siguientes líneas no eran correctas para Mac.
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
Tuve que reemplazarlos con las siguientes líneas:
- ~/.aspnet/https:/root/.aspnet/https:ro - ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro
Código final para docker-compose.override.yml
que funcionó:
version: '3.4' services: project-api: image: project-api environment: - ASPNETCORE_ENVIRONMENT=Development - ASPNETCORE_URLS=https://+:443;http://+:80 ports: - "5001:443" - "5000:80" volumes: - ~/.aspnet/https:/root/.aspnet/https:ro - ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro