How to run dockered ASP.net core app generated by Visual Studio 2019 on Linux? Problem with SSL configuration

后端 未结 2 1119
南笙
南笙 2020-12-20 21:06

I\'ve created new ASP.net Core 2.1 project in Visual Studio 2019. I\'ve choosed to generate Docker linux container and docker-compose file. Everything\'s working fine when I

相关标签:
2条回答
  • 2020-12-20 21:56

    Finally fixed it. Two bugs in my configuration:

    1: Unnecessary quotation marks in certificate password

    There was:

    - ASPNETCORE_Kestrel__Certificates__Default__Password="crypticpassword"
    

    Should be:

    - ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword
    

    2: App was looking for certificate in a wrong place.

    I was using docker with sudo, as root, but generated certificate using common account. I thought ${HOME} will point to my home folder, but it was pointing to root folder instead.

    0 讨论(0)
  • 2020-12-20 22:03

    Ok I figured it out! Please follow the exact steps below. Guaranteed this will work for you..so here goes..

    on CLI, in your app root directory run the following commands in the same order as below:

    $ mkdir conf.d
    $ dotnet dev-certs https --clean
    $ dotnet dev-certs https -ep ./conf.d/https/dev_cert.pfx -p madison
    $ dotnet dev-certs https --trust
    

    Ensure that the dev_cert.pfx file is available in ./conf.d/https folder. Now in your docker-compose file modify your api service to look like below:

    ...
    
    api:
        container_name: mad.api
        build:
          context: .
          dockerfile: Dockerfile
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - ASPNETCORE_URLS=https://+:443;http://+80
          - ASPNETCORE_HTTPS_PORT=44311
          - ASPNETCORE_Kestrel__Certificates__Default__Password=madison
          - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/dev_cert.pfx
        ports:
          - "50420:80"
          - "44361:443"
        volumes:
          - ./conf.d/https/:/https/
        networks:
          - mad_network
    
    ...
    
    
    $ docker-compose down # Down any previous setup
    $ docker-compose up --build -d # Build and run containers
    $ docker-compose ps # Check status of api..make sure its "up"
    $ docker-compose logs -f api # Check logs
    

    Now, try any of these urls and see that they work!! (Note: You may not have weatherforecast endpoint in your code...use the appropriate one or you'll receive a 404

    http://localhost:50420/weatherforecast https://localhost:44361/weatherforecast

    (The reason why it wasn't working for me was because when I ran dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here } it was creating the pfx with a weird name in my root directory and not the location specified in the -ep argument. I am now using asp.net core 3.0. This command work fine previously on asp.net core 2.2. So something musta changed between 2.2 and 3.0! Also interestingly it seems like asp.net core 3.0 got rid of the docker-compose project which is a huge blessing! :pray: #blessed)

    0 讨论(0)
提交回复
热议问题