
Containerize Blazor Web App with ASP.NET Core Web Api
Hey everyone,
I'm currently learning Docker because I want to use this technology to ship ready to run containers for my web apps. So far I have created a very basic project which contains a Blazor Web App with interactive server side rendering and a web api which should be utilized by the web app. The code is available on GitHub right here: https://github.com/MarvinKlein1508/DockerSample
The app builds fine and the api is also working.
However I have a few question regarding the code of this project. The docker-compose.yml contains this code:
services:
blazorwebapp:
image: ${DOCKER_REGISTRY-}blazorwebapp
container_name: mk_test
build:
context: .
dockerfile: src/BlazorWebApp/Dockerfile
ports:
- "5000:5000"
- "5001:5001"
blazorwebapp.api:
image: ${DOCKER_REGISTRY-}blazorwebapp.api
container_name: mk_test_api
build:
context: .
dockerfile: src/BlazorWebApp.Api/Dockerfile
ports:
- "5002:5002"
- "5003:5003"services:
blazorwebapp:
image: ${DOCKER_REGISTRY-}blazorwebapp
container_name: mk_test
build:
context: .
dockerfile: src/BlazorWebApp/Dockerfile
ports:
- "5000:5000"
- "5001:5001"
blazorwebapp.api:
image: ${DOCKER_REGISTRY-}blazorwebapp.api
container_name: mk_test_api
build:
context: .
dockerfile: src/BlazorWebApp.Api/Dockerfile
ports:
- "5002:5002"
- "5003:5003"
It has been generated mainly by Visual Studio itself. I only exchanged the port numbers here. When I try to call the API via https during development then my app crashes because the certificate for the API could not be verified.
I've read online that I should use http only for internal communication between docker containers. So I have updated my appSettings.json from "WeatherApi": "https://blazorwebapp.api:5003" to "WeatherApi": "http://blazorwebapp.api:5002" and I have removed app.UseHttpsRedirection();app.UseHttpsRedirection(); from the web API. This fixes my issue but I'm wondering if this is the right approach. Do I not want to use the HttpsRedirection in the final image outside of my debug environment? How should I handle this? Is it even a good idea to remove https here?
Another question I have is regarding the port numbers. Right now I have specified them in the Dockerfile s of the individual projects. The web app exposes 5000 and 5001 and the api exposes 5002 and 5003. This port numbers are also set in the docker-compose.yml file. But what if these ports aren't available on the target machine? Does the person who wants to use the app then have to change the ports in both docker files and the docker-compose file and compile it for themself? Or how can they specify a custom port?
Last but not least can anyone tell me what's the purpose of ${DOCKER_REGISTRY-} ? Is this required for Visual Studio in order to debug the apps, or can I remove this part safely?
Thanks for any help or tips! :)