I am very new to docker and did a good bit of searching through other questions asked here as well as reddit. The issue I am having is that docker can’t find my .csproj files of the services I am trying to containerize. When I am inside the solution of a given service, running docker build in the terminal creates an image successfully. However, when I try to do "docker-compose -f docker-compose.yml up –build" it gives me the following error:
"RUN dotnet restore "contentservice.csproj, error MSb1009: project file does not exist"
For reference, here’s my project structure
- solution
- contentserviceprj
- Dockerfile
- userserviceprj
- Dockerfile
- docker-compose
- docker-compose.yml
And here’s one of the Dockerfiles (they are basically the same apart from names) and my docker-compose.yml file:
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 *.csproj .
RUN dotnet restore "contentservice.csproj"
COPY . .
RUN dotnet publish "contentservice.csproj" -c Release -o /publish
FROM mcr.microsoft.com/dotnet/aspnet:6.0 as final
WORKDIR /app
COPY --from=build /publish .
ENTRYPOINT ["dotnet", "contentservice.dll"]
docker-compose.yml file:
version: '3.4'
services:
userservice:
image: ${DOCKER_REGISTRY-}userservice
build:
context: .
dockerfile: userservice/Dockerfile
ports:
- "9000:80"
environment:
- GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
contentservice:
image: ${DOCKER_REGISTSTRY-}contentservice
build:
context: .
dockerfile: contentservice/Dockerfile
ports:
- "9001:80"
Thank you in advance!
>Solution :
Your context is .
for both Dockerfile builds. This means the root of the context is the docker-compose
folder, not each individual project folder. When Docker tries to execute COPY *.csproj .
, which is searching only in the top-level directory in the context for the file, it cannot find the file to copy.
There are a few ways of changing this:
-
Change the context to the name of the project folder.
version: '3.4' services: userservice: build: context: ../userservice dockerfile: userservice/Dockerfile contentservice: build: context: ../contentservice dockerfile: contentservice/Dockerfile
Use this option if you don’t want to change the Dockerfile.
-
Keep the context as
.
, move the docker-compose.yml file to the Solution directory, and change the Dockerfile to copy the csproj file relative to the Solution directory.COPY contentservice/*.csproj .
I’d suggest you stick with option 2, because if you ever have a project that has a project reference, you’ll need to copy in multiple csproj files. Narrowing the scope to a single project directory won’t allow you to do that.