0

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?

1
  • you missed copying sln file Commented Dec 7, 2021 at 11:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Also, need to put the docker file into the same location as the sln file
@samnaction The solution file isn't used for anything in this Dockerfile, since OP builds the csproj file.
Doesnt this -f ./project_Api/Dockerfile . open nested dockerfiled but leaves context in upper folder?
@Gohini Yes, your build context is the current directory in that case. So your COPY statements should look something like COPY ["Project1/Project1.csproj", "Project1/"].
Ok, I made stupid mistake, I left COPY ["ProjectApi/ProjectApi.csproj", "."] instead of COPY ["ProjectApi/ProjectApi.csproj", "ProjectApi/"] Thank you very much, it works great.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.