Giter Site home page Giter Site logo

https-aspnetcore-in-docker's Introduction

How to run ASP.NET Core app on HTTPS in Docker

This prove of concept demonstrates following topics:

  1. Configuration of HTTPS in ASP.NET Core app: Program.cs and Startup.cs
  2. Using SSL certificate file in ASP.NET Core app: localhost-dev.pfx
  3. Generating localhost development certifcate file: generate-dev-cert.ps1
  4. Running ASP.NET Core on HTTPS inside Docker container: Dockerfile and docker-compose.override.yml
  5. Configuring HTTPS ports for Visula Studio and Docker container: launchSettings.json and docker-compose.override.yml

Step 1: Generate a local development certificate

Execute Cert\generate-dev-cert.ps1 file which will create a certificate: Cert\localhost-dev.pfx with and auto-generated random password (a GUID) and saves that password in UserSecrets (secrets.json) for this project

Step 2: Run docker-compose

In the root catalog (where the docker-compose.yml file is) run command

docker-compose up -d --build

this will build docker image and create a docker container.

Step 3: Open a web browser

Open https://localhost:44309/api/values to confirm the app is running correctly.

Explanation

This PoC uses PowerShell script Cert\generate-dev-cert.ps1 which is using dotnet dev-certs tool to generate a certificate file Cert\localhost-dev.pfx with a random password (a GUID). This password and full path to the generated certificate file is then saved to User Secrets for the project.

Good starting point for HTTPS development is: Enforce HTTPS in ASP.NET Core

Main points

HTTPS is configured in Program.cs

.UseKestrel(options =>
    {
        //...
        options.Listen(IPAddress.Any, httpsPort, listenOptions =>
        {
            listenOptions.UseHttps(certPath, certPassword);
        });
    });

and Startup.cs

if (env.IsDevelopment())
{
    //...
}
else
{
    app.UseHsts();
}

app.UseHttpsRedirection();

HTTPS port and allowed urls are configured with environment variables defined in launchSettings.json (for running in Visual Studio) and docker-compose.override.yml (for running in Docker container)

"environmentVariables": {
    "ASPNETCORE_HTTPS_PORT": "44300",
    "ASPNETCORE_URLS": "https://*:443"
}
------
environment:
    - ASPNETCORE_HTTPS_PORT=44309
    - ASPNETCORE_URLS=https://+:443;http://+:80
    - CertPath=/app/cert/localhost-dev.pfx

Running in Visual Studio

When running in Visul Studio, HTTPS port and allowed urls are overriden by launchSettings.json. CertPath and CertPassword are stored in User Secrets for the project and used when running the application: configuration.GetValue<string>("CertPassword")

Running in Docker

When running in Docker, HTTPS port and allowed urls are overriden by docker-compose.override.yml

CertPassword is still stored in User Secrets for the project and is available inside Docker container by creating a volume linked to User Secrets folder on the host machine in docker-compose.override.yml

    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro

CertPath in User Secrets is overriden by environment variable defined in docker-compose.override.yml. This is the same path as defined in Dockerfile: /app/cert/localhost-dev.pfx

ARG CERT_PATH_DEST=/app/cert/localhost-dev.pfx
ENV CertPath=$CERT_PATH_DEST
COPY Cert/localhost-dev.pfx $CertPath

https-aspnetcore-in-docker's People

Contributors

kkoziarski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.