I need to containerize a ASP.NET Core 6.0 Web API using Docker. The project can be found in this GitHub Repo of mine.
The project has the following structure:
───PlaygroundApiDockerized
│
├───src
│ ├───Playground.API
| | ├───Dockerfile
| | └───Playground.API.csproj
| |
│ └───Playground.Core
| └───Playground.Core.csproj
│
├───test
│ ├───Playground.Tests
│ └───Playground.Tests.csproj
│
├───Playground.sln
└───docker-compose.yml
The API project has a dependancy on a simple C# class library called Playground.Core
and has a Dockerfile
.
The content of the Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Playground.API/Playground.API.csproj", "Playground.API/"]
COPY ["src/Playground.Core/Playground.Core.csproj", "Playground.Core/"]
RUN dotnet restore "src/Playground.API/Playground.API.csproj"
COPY . .
WORKDIR "/src/Playground.API"
RUN dotnet build "Playground.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Playground.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Playground.API.dll"]
The docker-compose
file looks like this:
version: '3.7'
services:
playground-api:
build:
context: ./
dockerfile: src/Playground.API/Dockerfile
restart: always
ports:
- "7987:80"
My expectation is,when the docker-compose up
command is exectuted in the solution root directory, the API is started.
But the command results in the following error below.
I understand that the current working directory where the command is executed is the context directory specified in the docker-compose.yml
. In my example, the context is the solution root. I simply can't figure out how to specify the paths in the Dockerfile
. Does anyone know?
Creating network "playgroundapidockerized_default" with the default driver
Building playground-api
[ ] Building 2.2s (11/18)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 720B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0 0.8s
=> [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0 0.0s
=> [internal] load build context 0.3s
=> => transferring context: 13.35MB 0.2s
=> [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:6.0 0.0s
=> [build 1/8] FROM mcr.microsoft.com/dotnet/sdk:6.0@sha256:fde93347d1cc74a03f1804f113ce85add00c6f0af15881181165 0.0s
=> CACHED [build 2/8] WORKDIR /src 0.0s
=> [build 3/8] COPY [src/Playground.API/Playground.API.csproj, Playground.API/] 0.1s
=> [build 4/8] COPY [src/Playground.Core/Playground.Core.csproj, Playground.Core/] 0.0s
=> ERROR [build 5/8] RUN dotnet restore "src/Playground.API/Playground.API.csproj" 0.9s
------
> [build 5/8] RUN dotnet restore "src/Playground.API/Playground.API.csproj":
#13 0.814 MSBUILD : error MSB1009: Project file does not exist.
#13 0.814 Switch: src/Playground.API/Playground.API.csproj
------
executor failed running [/bin/sh -c dotnet restore "src/Playground.API/Playground.API.csproj"]: exit code: 1
ERROR: Service 'playground-api' failed to build : Build failed
CodePudding user response:
When you copy the project files, your current directory in the image is /src
and you copy it to Playground.API/
so the project file ends up having the absolute path /src/Playground.API/Playground.API.csproj
.
When you then run dotnet restore
, you use a relative path src/Playground.API/Playground.API.csproj
. So it looks for /src/src/Playground.API/Playground.API.csproj
which doesn't exist.
Later when you copy all the files with COPY . .
you get the directory structure from the host, so now your files become /src/src/Playground.API/Playground.API.csproj
etc. At this point you have 2 copies of your project files. One in /src/Playground.API
and one in /src/src/Playground.API
.
I think the best thing to do is keep the host directory structure under the working directory, i.e. /src/src/...
. I'd rename the working directory to something other than /src
to avoid confusion. Something like this
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app
COPY ["src/Playground.API/Playground.API.csproj", "src/Playground.API/"]
COPY ["src/Playground.Core/Playground.Core.csproj", "src/Playground.Core/"]
RUN dotnet restore "src/Playground.API/Playground.API.csproj"
COPY . .
WORKDIR "/app/src/Playground.API"
RUN dotnet build "Playground.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Playground.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Playground.API.dll"]