Home > Blockchain >  Dockerize .net app with nested solution file
Dockerize .net app with nested solution file

Time:12-09

I want to dockerize multi-project solution with nested solution file. This is what I tried to do:

Folder structure:

-Project1
--project1.csproj
-Project2
--project2.csproj
-ProjectApi
--DockerFile
--projectApi.sln
--projectApi.csproj

My dockerfile:

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 ["ProjectApi.csproj", "."]
COPY ["../Project1/Project1.csproj", "../Project1/"]
COPY ["../Project2/Project2.csproj", "../Project2/"]
RUN dotnet restore "./ProjectApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "ProjectApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProjectApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectApi.dll"]

My jenkins configuration:

docker rmi $(docker images -q) || true
sh stop_containers.sh || true

docker build --no-cache -t project_server_api -f ./project_Api/Dockerfile .
docker run -d --log-opt max-size=5m --log-opt max-file=3 --name projectApi
cd ..

Error I get:

COPY failed: stat /var/lib/docker/tmp/docker-builder193332682/ProjectApi.csproj: no such file or directory

I tried with different paths or dockerfile in upper folder but I've got: Forbidden path outside the build context What should I change?

CodePudding user response:

You can't move up a directory like you do with the source file in COPY ["../Project1/Project1.csproj", "../Project1/"].

That takes you outside the build context and that's not allowed for security reasons.

You need to move your Dockerfile up a directory and adjust the paths in the Dockerfile. That way all the files you want to copy are in the same directory as the Dockerfile or in directories below that.

Here's my guess at how your Dockerfile should look based on your directory structure

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 ["ProjectApi/ProjectApi.csproj", "ProjectApi/"]
COPY ["Project1/Project1.csproj", "Project1/"]
COPY ["Project2/Project2.csproj", "Project2/"]
RUN dotnet restore "ProjectApi/ProjectApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "ProjectApi/ProjectApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProjectApi/ProjectApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectApi.dll"]

For simplicity's sake, it's nice to have the Dockerfile in the directory that is also the build context, so you might consider moving the Dockerfile up one directory.

  • Related