Building ASP.NET Core API Docker Image failed in windows - asp.net

I am new in Docker world. I create a simple ASP.NET Core API. I try to build a Docker image but I won't build.
This is my docker file.
FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk-nanoserver-1803 AS build
WORKDIR /src
COPY ["FirstDemo/FirstDemo.csproj", "FirstDemo/"]
RUN dotnet restore "FirstDemo/FirstDemo.csproj"
COPY . .
WORKDIR "/src/FirstDemo"
RUN dotnet build "FirstDemo.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "FirstDemo.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "FirstDemo.dll"]
and here is my error
Step 1/17 : FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803 AS base
2.2-aspnetcore-runtime-nanoserver-1803: Pulling from microsoft/dotnet
e46172273a4e: Pull complete
68aae72b77f3: Pull complete
02db7ef764ae: Pull complete
b85c87d0746a: Pull complete
8ded5310ea16: Pull complete
50f3c06b2324: Pull complete
57b0e03264f9: Pull complete
Digest: sha256:7b0c816859dca7eeab5ae92fea96090fb37fc966622649db0567f0c752552d4a
Status: Downloaded newer image for microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803
---> 422d1790b708
Step 2/17 : WORKDIR /app
---> Running in 8ae8be6cb8c0
**re-exec error: exit status 1: output: time="2019-02-19T23:15:48-05:00" level=error msg="hcsshim::ImportLayer failed in Win32: The system cannot find the path specified. (0x3) path=\\\\?\\C:\\ProgramData\\Docker\\windowsfilter\\***** folder=C:\\ProgramData\\Docker\\tmp\\hcs815654479"
hcsshim::ImportLayer failed in Win32: The system cannot find the path specified. (0x3) path=\\?\C:\ProgramData\Docker\windowsfilter\****** folder=C:\ProgramData\Docker\tmp\hcs815654479**
I also find this question but it does not help me. Please advice me.

For those who might have the same issue.
the problem was our security application ( CHECKPOINT- ENDPOINT Security)
it blocks the access of Docker to file system. for now, we remove it, then everything is going well.

Related

How to run an ASP .NET 6 API in a docker container on a raspberry pi / armv7

If I deploy my API directly I can start it via dotnet <app.dll>.
I want to deploy it in a container tho. However, the container always exits with an error code 132 or 130 depending on the Dockerfile used. I will post it here:
FROM mcr.microsoft.com/dotnet/aspnet:3.1-bullseye-slim-arm32v7 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build -r linux-arm
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish -r linux-arm
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]
The first line of this Dockerfile is what I have been changing up in order to "find" the correct runtime. I tried many containers listed here at the Linux arm32 Tags section. All containers I tried exit with an error immediately:
I would greatly appreciate any help on how to resolve this issue :)
EDIT:
I use a raspberry pi 4 Model B with 8GB of RAM

Docker errror: dotnet-file.dll does not exist

I have successfully builded my app and there is image listed my-app:2.7
But when I try to run it error says:
Any idea where is wrong?
docker run -it --rm -p 5000:80 --name my-app:2.7 uploadcore Unable to find image 'uploadcore:latest' locally docker: Error response from daemon: pull access denied for uploadcore, repository does not exist or may require 'docker login': denied: requested access to the resource is denied. See 'docker run --help'.
docker run -p 5000:80 my-app:2.7 Could not execute because the specified command or file was not found. Possible reasons for this include: You misspelled a built-in dotnet command. You intended to execute a .NET program, but dotnet-uploadcore.dll does not exist. You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Dockerfile:
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0 AS base
WORKDIR /app
EXPOSE 59518
EXPOSE 44364
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0 AS build
WORKDIR /src
COPY UploadCore/UploadCore.csproj UploadCore/
RUN dotnet restore UploadCore/UploadCore.csproj
COPY . .
WORKDIR /src/UploadCore
RUN dotnet build UploadCore.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish UploadCore.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "uploadcore.dll"]
Files structure
Context
Controllers
Migrations
Models
Properties
Service
Views
wwwroot
Program.cs
Startup.cs
UploadCore.csproj
appsettings.Development.json
appsettings.json
libman.json
Linux is case sensitive, so your ENTRYPOINT has to be
ENTRYPOINT ["dotnet", "UploadCore.dll"]
Your second docker run command works. You can add a container name with the --name option like this
docker run -p 5000:80 --name mycontainer my-app:2.7
The image name has to be the first parameter that isn't an option. In your first docker run command, the first one is uploadcore, so docker looks for an image called that and it can't find it.

This site can’t be reached - Docker

I have created the Containers / Apps
But when I run it, I got this error message: This site can’t be reached. localhost unexpectedly closed the connection.
But when I run from Visual Studio, I can run it.
Extra troubleshooting steps is below
This is docker file
FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src
COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]
RUN dotnet restore "WebApplication3/WebApplication3.csproj"
COPY . .
WORKDIR "/src/WebApplication3"
RUN dotnet build "WebApplication3.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication3.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication3.dll"]
By default, Swagger is only available when the app runs in development mode. Docker containers, by default, don't run in development mode.
Try accessing the API directly on http://localhost:49155/WeatherForecast
You can run the container in development mode by setting the environment variable ASPNETCORE_ENVIRONMENT to Development by adding the option -e ASPNETCORE_ENVIRONMENT=Development to your docker run command. Then Swagger should be available.

How to access ASP.NET Swagger API published in Docker?

I have made an ASP.NET API with scaffolding. I have generated a docker file in Visual Studio 2019:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["CoinAPI.csproj", "CoinAPI/"]
RUN dotnet restore "CoinAPI/CoinAPI.csproj"
WORKDIR "/src/CoinAPI"
COPY . .
RUN dotnet build "CoinAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CoinAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CoinAPI.dll"]
I am building the project with command:
docker build -t coinapi .
And running it with:
docker run -dp 12344:12344 coinapi
The container is running in docker desktop in Windows, but when I access localhost:12344 I get an error 404. After adding this line of code webBuilder.UseUrls("http://localhost:12344"); in Program.cs file, I am getting an ERR_EMPTY_RESPONSE when accessing the URL. What can I do to successfully access my Swagger API run in a docker container?
The application listens to ports 80 and 443 by default, try this
docker run -dp 12344:80 coinapi
And you should access the web with http://localhost:12344
The thing that did the trick was to change webBuilder.UseUrls("http://localhost:12344"); to webBuilder.UseUrls("http://+:12344"); and now the API works and I can connect to it via the browser with http://localhost:12344/

Docker fails to build in Azure Pipelines with "error MSB1001"

Am facing strange issue. When i build my Dockerfile on windows machine(Laptop) getting below error and also getting same error when i tried to deploy on Azure DevOps.
Step 7/17 : COPY ["WebApp.csproj", ""]
---> 3f3e198d00aa
Step 8/17 : RUN dotnet restore "/WebApp.csproj"
---> Running in 51f16f947ffb
MSBUILD : error MSB1001: Unknown switch.
Switch: /WebApp.csproj
For switch syntax, type "MSBuild -help"
The command '/bin/sh -c dotnet restore "/WebApp.csproj"' returned a non-zero code: 1
PS C:\Users\user\Desktop\Directory>
But the same Dockerfile works fine when we build it using visual studio it builds and starts running without any errors.
Docker file looks like:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["WebApp.csproj", ""]
RUN dotnet restore "/WebApp.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "WebApp.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebApp.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApp.dll"]
Because in your dotnet restore there is / so MSBuild think it's a switch (like /p:) and fails, you just need to remove it:
RUN dotnet restore "WebApp.csproj"

Resources