Dev certificate on aspnetcore docker container - docker

I'm new to Docker and I have run into a trouble running my app in docker due missing ssl certificates. How do I run aspnetcore application in docker with non-development environment using dev certificates?
I have the following Dockerfile generated by Visual Studio:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
COPY [...other libraries...]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]
When I run it from the Visual Studio with the default setup (development), it connects Docker and everything runs fine. However, I want to use a different appsettings file (appsettings.docker.json) because I need some different values when running in Docker and I'm already using my appsettings.development.json for my standard run from the VS. Thus I set ASPNETCORE_ENVIRONMENT=Docker. This is causing a pain as I'm suddenly getting an InvalidOperationException:
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.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
Based on this answer, I have figured out that dotnet generates the dev certs automagically when the environment variable is Development. How can that be fixed for a differently called environment?
I did try to use the dotnet dev-certs https command in the Dockerfile, however, build failed saying that there is no sdk in the image and thus doesn't know the command.

Try adding these 2 lines
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
# Add the line below ---------------------------------------------------
RUN dotnet dev-certs https
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
COPY [...other libraries...]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
# Add the line below ---------------------------------------------------
COPY --from=publish /root/.dotnet/corefx/cryptography/x509stores/my/* /root/.dotnet/corefx/cryptography/x509stores/my/
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]

Related

Docker Container ODBC connection to External SQL Server

I'm attempting to run my ASP NET Core 3.1 API in a Linux Docker container. A component in my API requires a connection via ODBC to a server located on an external server. While in debug mode when I startup my API, the connection is attempted via ODBC but fails with the error "libodbc could not be loaded" I assume the ODBC drivers are missing in the Docker container.
I have searched online and I haven't been able to find how I would go about adding the missing library to my container or if maybe I need to download a different Base image. A copy of my docker file can be found below.
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["Sample.Service/Sample.Service.csproj", "Sample.Service/"]
COPY ["../Libs/ExternalServices.Configure/ExternalServices.Configure.csproj", "../Libs/ExternalServices.Configure/"]
RUN dotnet restore "Sample.Service/Sample.Service.csproj"
COPY . .
WORKDIR "/src/Sample.Service"
RUN dotnet build "Sample.Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Sample.Service.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Sample.Service.dll"]
After looking deeper into the problem, I was missing a couple of key things to make it work. I did need to add the Linux commands to ensure the repositories are updated and then make a request to install the unixodbc library. The resulting Docker file looks like this.
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
RUN apt-get update
RUN apt-get install -y unixodbc
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["Sample.Service/Sample.Service.csproj", "Sample.Service/"]
COPY ["../Libs/ExternalServices.Configure/ExternalServices.Configure.csproj", "../Libs/ExternalServices.Configure/"]
RUN dotnet restore "Sample.Service/Sample.Service.csproj"
COPY . .
WORKDIR "/src/Sample.Service"
RUN dotnet build "Sample.Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Sample.Service.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Sample.Service.dll"]

.Net 6 Web Api docker port forwarding issue

I have an empty .NET 6 web api solution with swagger installed. I have generated a docker file from Visual Studio which looks like this:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
I have created a docker image and pushed to my docker hub account.
I have then run this command to pull and run the image locally:
docker container run -d --name mynewapi -p 8000:443 myusername/myreponame:mycontainername
However when I browse to http://localhost:433/swagger/index.html I get:
This site can’t be reached localhost refused to connect.
I have also tried to make a GET request using postman to the sample endpoint but i just get the same error.
What am i doing wrong here?
When you run the container, you map port 443 in the container to port 8000 on the host. So you need to access it using port 8000.
Since you map port 443 which is the https port, you should be using https. So your URL should be ​https://localhost:8000/swagger/index.html
But that still isn't enough. By default Swagger is only available when your solution is running in Development mode. You control that using the environment variable ASPNETCORE_ENVIRONMENT which needs to be set to 'Development'. You can do that in your docker command when you run the container like this
docker container run -d --name mynewapi -p 8000:443 -e ASPNETCORE_ENVIRONMENT=Development myusername/myreponame:mycontainername
or you can add it to the Dockerfile like this
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Development
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]

Running Sqlite in a docker container

I have a small application that I want to containerize in .Net5.0. It works well outside docker but if added RUN dotnet ef database update to my docker file, it gives a build error. I don't know if I placed it in a wrong place or it doesn't work that way.
Also will COPY --from=publish /app/publish/tomix.db . copy the new database file correctly?
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
RUN dotnet ef database update
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish/tomix.db .
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]
UPDATE This is the error after running dotnet build -t tomix .
AFAIK, the dotnet ef tool is no longer part of the .NET SDK, and needs to be explicitly installed. I.e., before attempting to RUN dotnet ef database update you must RUN dotnet tool install dotnet-ef

When using docker, I get DirectoryNotFoundException in PhysicalFileProvider in blazor

I created a blazor project using .netcore3.1.
I use PhysicalFileProvider in startup.cs.
I ran it with docker using config in docker.
The build was successful, but an exception occurred during execution.
The docker file is as follows:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Web/Web.csproj", "Web/"]
RUN dotnet restore "Web/Web.csproj"
COPY . .
WORKDIR "/src/Web"
RUN dotnet build "Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Web.dll"]
This is a docker file automatically generated by Visual Studio.
Why can't the program find the directory?
Or, is there another way to find the directory?

How to access ASP Core Web Api running in Docker using Postman

I would like to make REST calls to my ASP Core Web Api running in Docker by using Postman. Here is my Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["MyApp.API/MyApp.API.csproj", "MyApp.API/"]
RUN dotnet restore "MyApp.API/MyApp.API.csproj"
COPY . .
WORKDIR "/src/MyApp.API"
RUN dotnet build "MyApp.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApp.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.API.dll"]
Using Visual Studio 2019, I click on the Docker run button which builds the image and creates a container. However, Postman cannot access the API running in Docker. The error message is "Error: connect ECONNREFUSED 127.0.0.1:51005." Is there a port command that I should use? If so, where should I put it? Thanks.

Resources