DotNet 6 fails to run in a Windows container: Permission denied - windows

I am not familiar with Windows containers, and I am getting this error when trying to debug a hello-world DotNet 6 application in VS2022:
Cannot use file stream for [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.json]: Permission denied
Invalid runtimeconfig.json [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.json] [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.dev.json]
The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program '[13360] dotnet.exe' has exited with code 2147516563 (0x80008093).
Dockerfile (default):
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApplication5/WebApplication5.csproj", "WebApplication5/"]
RUN dotnet restore "WebApplication5/WebApplication5.csproj"
COPY . .
WORKDIR "/src/WebApplication5"
RUN dotnet build "WebApplication5.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication5.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication5.dll"]
I tried to spin the container in an interactive way, to validate if the file was actually there but this command did not work:
docker run -it --entrypoint powershell.exe webapplication5
docker: Error response from daemon: container 8581fd662043dbfad4a2b68e561d21c106e4754b6f21c4d06cfb9b3d5e2b0a39 encountered an error during hcsshim::System::CreateProcess: failure in a Windows system call: The system cannot find the file specified. (0x2).
What can the problem be?
Why I cannot get a PS inside the container?

My docker user needs more permissions. I solved it in an easy way because it is for testing.. don't do this at home, and grant the minimum permissions instead:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
# Here is the risky trick
USER Administrator

Related

Docker build - failed to compute cache key: "/srv/app/www" not found: not found

I'm having a lot of difficulty getting docker to work on osx. I have the following Dockerfile:
FROM node:10.17.0-alpine AS staging
CMD ["mkdir","/srv/app"]
WORKDIR /srv/app
COPY . .
RUN ["npm","install"]
FROM node:10.17.0-alpine AS builder
RUN ["mkdir", "/srv/app" ]
COPY --from=staging /srv/app/ /srv/app/
WORKDIR /srv/app
WORKDIR /srv/app
RUN ["npm","run-script","build-staging"]
FROM nginx:alpine
COPY --from=builder /srv/app/www/ /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/nginx.conf
I call this file like so: docker build -f ./Dockerfile --no-cache -t <my-app>:staging .
In windows this works perfectly, but my team has a mix of Windows and OSX computers, and docker build fails on all of our OSX machines with this error:
=> ERROR [stage-2 2/3] COPY --from=builder /srv/app/www/ /usr/share/nginx/html/
------
> [stage-2 2/3] COPY --from=builder /srv/app/www/ /usr/share/nginx/html/:
------
failed to compute cache key: "/srv/app/www" not found: not found
I came across a possible cause of the situation where I could try adding to "* text=auto" to my .gitattributes file but this didn't fix the issue even after recloning my repo.
How is it possible for my Dockerfile to work on Windows and not on OSX?
EDIT: I should add that I have a nearly identical project with an identical Docker file whose docker push works on all Windows machines and all OSX machines.
I discovered the issue. My docker memory and swap resources available were too small for the project in question. The error message provided by Docker was not very helpful in the case and docker build was getting interrupted once docker reached its memory limit.

An issue with resolving directory names when building a docker container for an asp.net core 3.1 web app

Visual Studio 2019 generates a docker file whose COPY statement looks like the following due to the way that the application's folders are structured:
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["MyWebApp/MyWebApp.csproj", "MyWebApp/"]
COPY ["../../core/Logic/Logic.csproj", "../../core/Logic/"]
COPY ["../../core/Models/Models.csproj", "../../core/Models/"]
RUN dotnet restore "MyWebApp/MyWebApp.csproj"
COPY . .
WORKDIR "/src/MyWebApp"
RUN dotnet build "MyWebApp.csproj" -c Release -o /app/build
I keep getting the following error message when running docker compose command:
=> ERROR [build 4/9] COPY [../../core/Logic/Logic.csproj,
../../core/Logic/] 0.0s
=> ERROR [build 5/9] COPY [../../core/Models/Models.csproj, ../../core/Models/]
We cannot restructure the folders in VS 2019 as it has been set up in that way due to some reasons. The docker file resides in MyWebApp project's folder and the docker compose file is in the parent folder of MyWebApp folder where the solution resides. Even I moved the docker file adjacent to the docker compose file but it was not conclusive.
The following code snippet is the docker-compose file:
version: '3.4'
services:
mywebapp:
image: ${DOCKER_REGISTRY-}mywebapp
build:
context: .
dockerfile: MyWebApp/Dockerfile
ports:
- 8080:80
- 8443:443
What is the workaround or solution to address this problem?
If your file structure is:
docker-compose.yml
MyWebApp/MyWebApp.csproj
core/Logic/Logic.csproj
core/Models/Models.csproj
Your Dockerfile should be:
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["MyWebApp/MyWebApp.csproj", "MyWebApp/"]
COPY ["core/Logic/Logic.csproj", "../../core/Logic/"]
COPY ["core/Models/Models.csproj", "../../core/Models/"]
RUN dotnet restore "MyWebApp/MyWebApp.csproj"
COPY . .
WORKDIR "/src/MyWebApp"
RUN dotnet build "MyWebApp.csproj" -c Release -o /app/build
Why?
Because the current working directory is always the context directory. Context can be set using docker-compose's build.context field. In your example, the context is the solution root. So the Dockerfile is executed from that directory, irrespective or where it's placed.
You cannot access files outside the context directory.
Eg. if context resolves to /a/b/c, Dockerfile can't COPY or use files in /a/b/d

VS React-Redux container connecting to api-sqlserver containers

Got a problem with VS React-Redux template deployed as a docker container connecting to api docker container. Below are the given facts:
Fact 1. I've got 3 Docker Windows containers in docker hub (https://hub.docker.com/repository/docker/solomiosisante/test):
solomiosisante/test:sqlserver
solomiosisante/test:api
solomiosisante/test:react
Fact 2. I managed to make the api connect to sqlserver and make them communicate by creating a docker nat network. API container can get and display data from the sqlserver container.
Fact 3. I also run the react container using the same nat network.
Fact 4. I can successfully docker run the react container.
Fact 5. They are all running Net 5.0 (VS projects), but not sure with sqlserver because I just got it from microsoft/mssql-server-windows-developer image.
Fact 6. I can run the react project from visual studio and load the pages in the browser with no problem. (and it connects to api container)
Problem: I could not make the react container browse to any of my pages. Browser says it can't be reached connection timed out.
React project Dockerfile:
# escape=`
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
###########################################################################################
FROM mcr.microsoft.com/powershell:nanoserver-1903 AS downloadnodejs
RUN mkdir -p C:\nodejsfolder
WORKDIR C:\nodejsfolder
SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop';$ProgressPreference='silentlyContinue';"]
RUN Invoke-WebRequest -OutFile nodejs.zip -UseBasicParsing "https://nodejs.org/dist/v15.6.0/node-v15.6.0-win-x64.zip"; `
Expand-Archive nodejs.zip -DestinationPath C:\; `
Rename-Item "C:\node-v15.6.0-win-x64" c:\nodejs
###########################################################################################
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
###########################################################################################
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
RUN mkdir -p C:\nodejs
COPY --from=downloadnodejs C:\nodejs\ C:\nodejs
# needs to use ContainerAdministrator to be able to setx path
USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:\nodejs"
USER ContainerUser
RUN echo %PATH%
#RUN echo "%PATH%"
#RUN echo $PATH
#RUN echo {$PATH}
WORKDIR /src
#COPY ["Consequence.React/Consequence.React.csproj", "Consequence.React/"]
#COPY ["Consequence.API/Consequence.API.csproj", "Consequence/"]
#COPY ["Consequence.EF/Consequence.EF.csproj", "Consequence.EF/"]
#COPY ["Consequence.Repositories/Consequence.Repositories.csproj", "Consequence.Repositories/"]
COPY . .
RUN dotnet restore "Consequence.React/Consequence.React.csproj"
#WORKDIR "/src/Consequence.React/ClientApp"
#RUN npm install
#RUN npm audit fix
WORKDIR "/src/Consequence.React"
RUN dotnet build "Consequence.React.csproj" -c Release -o /app/build
WORKDIR /src
RUN dir /s
WORKDIR "/src/Consequence.React"
###########################################################################################
FROM build AS publish
RUN dotnet publish "Consequence.React.csproj" -c Release -o /app/publish
###########################################################################################
FROM base AS final
RUN mkdir -p C:\nodejs
COPY --from=downloadnodejs C:\nodejs\ C:\nodejs
# needs to use ContainerAdministrator to be able to setx path
USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:\nodejs"
USER ContainerUser
RUN echo %PATH%
WORKDIR /app
COPY --from=publish /app/publish .
RUN dir /s
ENV ASPNETCORE_URLS="https://+;http://+"
ENV ASPNETCORE_HTTP_PORT=8089
ENV ASPNETCORE_HTTPS_PORT=44319
ENV ASPNETCORE_Kestrel__Certificates__Default__Password="P#ssw0rd123"
ENV ASPNETCORE_Kestrel__Certificates__Default__Path=/src/certs/consequence.pfx
ENTRYPOINT ["dotnet", "Consequence.React.dll"]
Any ideas, questions, please comment. Thank you in advance.
I finally solved this problem by using isolation=process. I thought I've got so much trouble with Hyper-V and can do without it. I followed the article Docker on Windows without Hyper-V by Chris and updated my docker hub test repo. I also put the instructions there on how to make it work. Please check this out. I now have docker images I can use as a base image for my future Docker-React-Redux docker deployment using Windows Containers. I hope this helps those who have encountered the same problems like me.

Trying to create docker image at command prompt getting docker build error

Created dotnet core mvc application. Tried to create docker image for Windows containers. When I execute docker build command getting below error.
C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [C:\src\dockertestwindows\dockertestwindows.csproj]
C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error : No such host is known. [C:\src\dockertestwindows\dockertestwindows.csproj]
The command 'cmd /S /C dotnet restore "dockertestwindows/dockertestwindows.csproj"' returned a non-zero code: 1
My docker file is
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["DockerWindowsCore/DockerWindowsCore.csproj", "DockerWindowsCore/"]
RUN dotnet restore "DockerWindowsCore/DockerWindowsCore.csproj"
COPY . .
WORKDIR "/src/DockerWindowsCore"
RUN dotnet build "DockerWindowsCore.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerWindowsCore.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerWindowsCore.dll"]
After connecting to VPN issue is resolved. I think its firewall issue.

Dockerfile doesn't seem to run while debugging in Visual Studio 2019

The past few weeks i've been working on a project which is built in C# .NET Core in Visual Studio 2019. I enabled Docker support and am using docker-compose to spin up 2 containers (it starts an identityserver4 and webapi). I've created dockerfiles for both projects and created a docker-compose file for starting up the service stack.
The issue i'm running into is that when i run the docker-compose in Visual Studio Debugging mode, it doesnt seem to run my Dockerfile. In the lasts steps in my Dockerfile, I copy some files around and execute a command. These do not get run. However when I use docker build in my commandline, it DOES execute those Dockerfile commands.
Attached my 2 docker files & docker compose.
Web API Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Api/MyApp.Api.Core/MyApp.Api.Core.csproj", "Api/MyApp.Api.Core/"]
COPY ["Api/MyApp.Api.Base/MyApp.Api.Base.csproj", "Api/MyApp.Api.Base/"]
COPY ["Base/MyApp.Base.Contracts/MyApp.Base.Contracts.csproj", "Base/MyApp.Base.Contracts/"]
COPY ["Base/MyApp.Base.Model/MyApp.Base.Model.csproj", "Base/MyApp.Base.Model/"]
COPY ["Data/MyApp.Data.EntityFramework/MyApp.Data.EntityFramework.csproj", "Data/MyApp.Data.EntityFramework/"]
COPY ["ContactpersoonService.cs/MyApp.Services.csproj", "ContactpersoonService.cs/"]
COPY ["Apps/MyApp.Apps.Settings/MyApp.Apps.Settings.csproj", "Apps/MyApp.Apps.Settings/"]
RUN dotnet restore "Api/MyApp.Api.Core/MyApp.Api.Core.csproj"
COPY . .
WORKDIR "/src/Api/MyApp.Api.Core"
RUN dotnet build "MyApp.Api.Core.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "MyApp.Api.Core.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
COPY Api/MyApp.Api.Core/Security/Certificates /app/Security/Certificates
RUN mkdir -p /usr/local/share/ca-certificates/identityserver
RUN chmod -R 777 /usr/local/share/ca-certificates/identityserver
RUN cp /app/Security/Certificates/* /usr/local/share/ca-certificates/identityserver
RUN update-ca-certificates
ENTRYPOINT ["dotnet", "MyApp.Api.Core.dll"]
IdentityServer Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Api/MyApp.Api.IdentityServer/MyApp.Api.IdentityServer.csproj", "Api/MyApp.Api.IdentityServer/"]
COPY ["Api/MyApp.Api.Base/MyApp.Api.Base.csproj", "Api/MyApp.Api.Base/"]
COPY ["Base/MyApp.Base.Contracts/MyApp.Base.Contracts.csproj", "Base/MyApp.Base.Contracts/"]
COPY ["Base/MyApp.Base.Model/MyApp.Base.Model.csproj", "Base/MyApp.Base.Model/"]
COPY ["Data/MyApp.Data.EntityFramework/MyApp.Data.EntityFramework.csproj", "Data/MyApp.Data.EntityFramework/"]
COPY ["Apps/MyApp.Apps.Settings/MyApp.Apps.Settings.csproj", "Apps/MyApp.Apps.Settings/"]
RUN dotnet restore "Api/MyApp.Api.IdentityServer/MyApp.Api.IdentityServer.csproj"
COPY . .
WORKDIR "/src/Api/MyApp.Api.IdentityServer"
RUN dotnet build "MyApp.Api.IdentityServer.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "MyApp.Api.IdentityServer.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
COPY Api/MyApp.Api.Core/Security/Certificates /app/Security/Certificates
RUN mkdir -p /usr/local/share/ca-certificates/identityserver
RUN chmod -R 777 /usr/local/share/ca-certificates/identityserver
RUN cp /app/Security/Certificates/* /usr/local/share/ca-certificates/identityserver
RUN update-ca-certificates
ENTRYPOINT ["dotnet", "MyApp.Api.IdentityServer.dll"]
docker-compose.yaml:
version: '3.4'
services:
myapp.api.core:
image: ${DOCKER_REGISTRY-}myappapicore
build:
context: .
dockerfile: Api/MyApp.Api.Core/Dockerfile
links:
- myapp.identity.api:identityserver
ports:
- "52008:80"
volumes:
- "C:/Projects/User/MyApp.Api/Api/MyApp.Api.Core/Security/Certificates/ca.crt:/usr/local/share/ca-certificates/identityserver/identityserver.crt:ro"
networks:
app_net:
ipv4_address: 192.168.1.200
myapp.identity.api:
image: ${DOCKER_REGISTRY-}myappapiidentityserver
build:
context: .
dockerfile: Identity/MyApp.Identity.Api/Dockerfile
ports:
- "5000:80"
- "5001:443"
volumes:
- "C:/Projects/User/MyApp.Api/Api/MyApp.Api.IdentityServer/Security/Certificates/ca.crt:/usr/local/share/ca-certificates/identityserver/identityserver.crt:ro"
networks:
app_net:
ipv4_address: 192.168.1.201
networks:
app_net:
external: true
I'm using Visual Studio 2019 (16.3.4)
This is apparently by design as a "Fast mode" optimization in Visual Studio 2019. See the documentation for debugging in containers here.
What it states is that "Fast mode" is the default behavior when debugging containers in VS 2019. In this mode, only the first stage (base) of a multi-stage build is built according to the Dockerfile. VS then handles the rest on the host machine, ignoring the Dockerfile, and shares the output to the container by using volume mounting. This means that any custom steps you add to other stages will be ignored when using the Debug configuration in VS 2019. (The reason given for this non-obvious, and therefore potentially frustrating, optimization is that builds are much slower in a container than on the local machine.) Note that this optimization only happens when using the Debug configuration. The Release configuration will use the entire Dockerfile.
Your options are:
Place your custom steps in the first (base) step of the Dockerfile.
or
Disable this optimization by editing the project file like this:
<PropertyGroup>
<ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
</PropertyGroup>
Also keep in mind that it will try to reuse a previously built container if possible, so you may need to perform a Clean or Rebuild in order to force the build to create a new version of the container.
Good luck!
** EDIT **
It seems that there is an issue when trying to use the ContainerDevelopmentMode flag after Container Orchestration Support (in this case, Docker Compose) is added. See this issue. It is suggested in the issue discussion that this flag could be used on the docker-compose.dcproj file, but there is a bug (still not fixed) that keeps that approach from working.
A third option, hinted at in my previous answer but not made explicit, would be:
Switch your solution configuration from Debug to Release.
This works, but clearly isn't ideal when you're trying to debug your application.

Resources