how to build docker image file and push Vsts - docker

How to address this error when I push my code on VSTS platform. I am using Visual Studio 2017.
I am new to the concept of Docker ,Azure (VSTS).
FROM microsoft/aspnetcore:1.1 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:1.1 AS build
WORKDIR /src
COPY ["aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj", "aspnet-core-dotnet-core/"]
RUN dotnet restore "aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj"
COPY . .
WORKDIR "/src/aspnet-core-dotnet-core"
RUN dotnet build "aspnet-core-dotnet-core.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "aspnet-core-dotnet-core.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "aspnet-core-dotnet-core.dll"]

Based on the DockerFile and error message, it failed to copy aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj file.
You need to specify DockerFile's parent folder path as the build context (Uncheck Use Default Build Context option and specify Build Context), then it can find the aspnet-core-dotnet-core/aspnet-core-dotnet-core.csproj file

Related

How to build dockerfile with multiple dependent assemblies to asp.net core webapi, as i am getting docker build error?

1. MY Docker file is below.
#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:3.1 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["TestAPI/TestAPI.csproj", "TestAPI/"]
RUN dotnet restore "TestAPI/TestAPI.csproj"
COPY . .
WORKDIR "/src/TestAPI"
RUN dotnet build "TestAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TestAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestAPI.dll"]
2. My solution structure is below.
Solution Structure
3.Folder structure
Folder structure
4.Error I am getting is below:
Docker build error

Creating Docker image for a dotnet 6 application - Type '' already defines a member called '' with the same parameter types

I am trying to create a docker image for a .net 6 web api application, and whenever I run the command:
~ sudo docker build -t aspnetapp .
It shows me the following error:
/src/AmplifyAPI.Application/Controllers/Product/ProductController.cs(22,38): error CS0111: Type 'ProductController' already defines a member called 'SaveProduct' with the same parameter types [/src/AmplifyAPI.Application/AmplifyAPI.Application.csproj]
And that error is getting showed for every method of every controller. This is my docker file:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore "AmplifyAPI.Application/AmplifyAPI.Application.csproj" --disable-parallel
WORKDIR "/src/AmplifyAPI.Application"
COPY . .
RUN dotnet build "AmplifyAPI.Application.csproj" -c Release -o /app
FROM build AS publish
WORKDIR "/src/AmplifyAPI.Application"
RUN dotnet publish "AmplifyAPI.Application.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "AmplifyAPI.Application.dll"]
And the application structure is:
--src
-----Dockerfile
-----AmplifyAPI.Data
-----AmplifyAPI.Domain
-----AmplifyAPI.Application
---------Controllers
--tests
-----AmplifyAPI.UnitTests
-----AmplifyAPI.IntegrationTests
Locally the application builds just fine. I am using Linux as operating system.
You copy the source code into the container twice, in different locations. You also both build and publish to /app, which might cause issues.
The normal Dockerfile for a .NET project first copies the .csproj file, then does a restore and then copies the rest of the code and builds, like this
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY AmplifyAPI.Application/AmplifyAPI.Application.csproj ./AmplifyAPI.Application/
COPY AmplifyAPI.Data/AmplifyAPI.Data.csproj ./AmplifyAPI.Data/
COPY AmplifyAPI.Domain/AmplifyAPI.Domain.csproj ./AmplifyAPI.Domain/
RUN dotnet restore "AmplifyAPI.Application/AmplifyAPI.Application.csproj" --disable-parallel
COPY . .
RUN dotnet build "AmplifyAPI.Application/AmplifyAPI.Application.csproj" -c Release
FROM build AS publish
WORKDIR "/src"
RUN dotnet publish "AmplifyAPI.Application/AmplifyAPI.Application.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "AmplifyAPI.Application.dll"]

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?

can't build .net core API image in docker

I am trying to build image for .net core 3.1 web api project but unable to build image
i get stuck at restoring project packages via dotnet restore
i get stuck at when i use dotnet build command ( does not find the solution or project file)
Directory Structure
https://i.stack.imgur.com/J7sMq.png
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as base
#FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
#copy all project and solution files
COPY ./SSFS.Service/.csproj ./SSFS.Service/
COPY ./SSF.EDM/.csproj ./SSF.EDM/
COPY ./API/.csproj ./API/
COPY ./.sln ./
WORKDIR /app/API
RUN dotnet restore "SSFAPI.csproj"
#copy rest of files
COPY . .
WORKDIR /app
#build the project to restore packages
RUN dotnet build --source "./SSFAPI.sln" -c Release -o /publish
#public the project to a folder
RUN dotnet publish --source "SSFAPI.sln" -c Release -o /publish
FROM base AS final
EXPOSE 80
WORKDIR /app
COPY --from=base /app/publish .
ENTRYPOINT ["dotnet", "SSFAPI.dll"]
output of above is as below
https://i.stack.imgur.com/xqoKv.png
If i build the project using dotnet command then out is as shown below
Output is as below
https://i.stack.imgur.com/t9bV5.png
The Dockerfile is set up to be run from the solution root. You're probably running docker build from the project directory (i.e. where the Dockerfile actually is).

Docker image doesnt work - probably wrong paths on build

I'm trying to move ASP.NET Core project to docker linux container, however I cannot build up a proper image. When I create default dockerfile (through VisualStudio Add -> DockerSupport) it's setting up paths for COPY to all of the projects. However when I run docker build -t . it pop's out error "no such file or directory". I've tried to stick with official docker documentation with moving asp.net core to docker containers and here is the code I ended with:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ./ ./
RUN dotnet restore "myproject.csproj"
COPY . .
WORKDIR "/src/myproject"
RUN dotnet build "myproject.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]
This is the command im using to build up an image:
docker build -t myapp -f myproject/Dockerfile .
I'm building it from the parent directory, because when I was using current working directory it wasn't finding all the .csproj files. When I run this image it doesn't work - I know there are wrong paths, however I have no idea where is the error in logic. Current error is "Program does not contain a static 'Main' method suitable for an entry point" and "A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/'. Failed to run as a self-contained app. If this should be a framework-dependent app, specify the appropriate framework in /app/myproject.runtimeconfig.json"
Try the following based off this documentation replace myproject with project name
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", “myproject.dll"]
Then build and run with the following commands:
docker build -t myproject .
docker run -d -p 8080:80 --name myapp myproject
You should not specify RUN dotnet build "myproject.csproj" -c Release -o /app with -o /app which will output in the same folder with build layer.
Try
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["myproject/*.csproj", "myproject/"]
RUN dotnet restore "myproject/myproject.csproj"
COPY . .
WORKDIR /src/myproject
RUN dotnet build "myproject.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]

Resources