skip to Main Content

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!

2

Answers


  1. You have to change the context in the docker-compose.yaml-file. A . reference the current directory of the docker-compose.yaml, but since it is in a different sub-folder as your services, it can’t find the Dockerfile.

    Currently, it would look for the Dockerfiles in ./docker-compose/Dockerfile instead of ./contentservice/Dockerfile and ./userservice/Dockerfile.

    The correct context would probably be ../ :

    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"
    

    For more information, see this question about setting the context to a parent folder

    Login or Signup to reply.
  2. 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:

    1. 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.

    2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search