Docker and ASP.NET Core 3.0
A sample code for the presentation "Docker and ASP.NET Core 3.0". There's also an older version of the presentation and different sample, for ASP.NET Core 2.1. You can find it here.
The intention of this sample is to demonstrate using ASP.NET Core application inside Docker container and consuming other services which are also dockerized. This is done using
docker-compose. The following containers are created:
- TimeTracker API - ASP.NET Core application, hosted on Linux container via Kestrel
- TimeTracker Client - Blazor WebAssembly application, hosted on Linux container via nginx
- TimeTracker DB - PostgreSQL database
- TimeTracker Proxy - Reverse proxy service with HAProxy
- Elasticsearch and Kibana - ELK stack for logging messages from API container
overlay network is used to connect all those services. Few Docker volumes are also used to persist the data on the host.
The application itself is taken from my ASP.NET Core workshop. Some docker and config files taken from (inspired by) Quick intro to Docker and Docker Compose: Angular, ASP.NET Core and Postgres app post by @joaofbantunes.
The instructions below are assuming you are using Windows operating system as a host.
- Docker Desktop
- Visual Studio 2019+
- Alternatively, Visual Studio Code with
- HTTPS certificate - for serving HTTPS from inside Linux Docker image
Creating and adding HTTPS Certificate
NOTE: Please note that Visual Studio will initialize everything that's needed for you. If you are not using Visual Studio or have issues with running HTTPS from the containers, read on.
To find out the reasoning and read the instructions, check out this document.
The following assumes you are using PowerShell for running commands. If you are using
cmd, provide environment variables in this format
%USERPROFILE% instead of
To create certificate, run the following:
dotnet dev-certs https -ep $env:APPDATA\ASP.NET\Https\TimeTracker.pfx -p TimeTracker#
TimeTracker# is a password for certificate. You can use a different value, but remember to use it in the following calls too. Now you need to add that password to user secrets, so Kestrel server can use the certificate. Run the following from the root repository folder:
dotnet user-secrets -p src\TimeTracker\TimeTracker.csproj set "Kestrel:Certificates:Development:Password" "TimeTracker#"
Running the sample
Run this command from the root folder:
The application is now accessible from the following URLs:
Running individual services
In most cases, you should run using docker compose, but if you want to run individual services, here's how you can do it.
To run the TimeTracker API project with Docker, first create an image from the root folder:
docker build -t timetracker-api-1 . -f src/TimeTracker/Dockerfile
Run with PowerShell:
docker run -p 5008:443 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_URLS="https://+" -v $env:APPDATA/Microsoft/UserSecrets/:/root/.microsoft/usersecrets:ro -v $env:APPDATA/ASP.NET/Https:/root/.aspnet/https/ timetracker-api-1
Or run with
docker run -p 5008:443 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_URLS="https://+" -v %APPDATA%/Microsoft/UserSecrets/:/root/.microsoft/usersecrets:ro -v %APPDATA%/ASP.NET/Https:/root/.aspnet/https/ timetracker-api-1
The command above will correctly map the host machine certificate folder and user secrets folder to locations in Linux based container that Kestrel expects. You should now be able to browse the API (Swagger docs) on this URL: https://localhost:5008/swagger/
Build the image:
docker build -t timetracker-client-1 . -f src/TimeTracker.Client/build/Dockerfile
Run the image:
docker run -p 5009:80 timetracker-client-1