18

I have a 3 Projects in a solution.

[Solution] 'BuySellApi' (3 Projects)
  | 
  +-- [BuySellApi]
  |    |
  |    +--- BuySellApi.csproj (This project holds the Docker file)
  |
  +-- [BuySellApi.Core]
  |    |
  |    +--- BuySellApi.Core.csproj
  |
  +-- [BuySellApi.Data]
       |
       +--- BuySellApi.Data.csproj

 1. BuySellApi.csproj -> API
 2. BuySellApi.Data/BuySellApi.Data.csproj -> Model
 3. BuySellApi.Core/BuySellApi.Core.csproj -> Data Access

I'm trying to build this using Docker by specifying following commands in Dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["BuySellApi.csproj", "./"]
COPY ["BuySellApi.Data/BuySellApi.Data.csproj", "./"]
COPY ["BuySellApi.Core/BuySellApi.Core.csproj", "./"]
RUN dotnet restore "./BuySellApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "BuySellApi.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "BuySellApi.dll", "--server.urls", "http://0.0.0.0:5000"]

After running the following command

docker build -t cog/buysellapi .

I'm getting the error as below:

e:\Apps\trunk\BuySell\BuySellApi>docker build -t cog/buysellapi .
Sending build context to Docker daemon  19.15MB
Step 1/19 : FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
 ---> ce06b36fcba4
Step 2/19 : WORKDIR /app
 ---> Using cache
 ---> 184385dc16fb
Step 3/19 : EXPOSE 5000
 ---> Using cache
 ---> 0e0cdd17e04d
Step 4/19 : ENV ASPNETCORE_URLS=http://+:5000
 ---> Using cache
 ---> 54cee58d679f
Step 5/19 : FROM microsoft/dotnet:2.2-sdk AS build
 ---> a4974ac66bfc
Step 6/19 : WORKDIR /src
 ---> Using cache
 ---> 7f9a2990f973
Step 7/19 : COPY ["BuySellApi.csproj", "./"]
 ---> Using cache
 ---> d526083ece6d
Step 8/19 : COPY ["BuySellApi.Data/BuySellApi.Data.csproj", "./"]
COPY failed: stat /mnt/sda1/var/lib/docker/tmp/docker-builder475321395/BuySellApi.Data/BuySellApi.Data.csproj: no such file or directory

It is not copying Data and Core Layers. When I try the same thing for a Solution with Single Project, It is working fine.

55SK55
  • 621
  • 2
  • 8
  • 23
  • 1
    Can you show proof that BuySellApi.Data/BuySellApi.Data.csproj actually exists at the same level as Dockerfile? Also if you have a .dockerignore can you post the contents? – Mihai May 15 '19 at 14:13
  • Data and Core are separate projects and these are referenced in API project. API project has dockerfile – 55SK55 May 15 '19 at 16:34
  • If you try to copy it then it must exist. Otherwise you get this error. So maybe you need to refactor your project – Mihai May 15 '19 at 16:56
  • Then how it'll be possible with 1 tire - Multi layer architecture? – 55SK55 May 15 '19 at 17:38
  • If you really want help please post your folder structure. Most of the times dockerizing an application means more than just adding a Dockerfile. Other times you just have to be a bit clever and put your Dockerfile at a different level from which to see all the necessary files. – Mihai May 15 '19 at 18:06
  • @Mihai Posted folder structure. – 55SK55 May 16 '19 at 05:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193438/discussion-between-55sk55-and-mihai). – 55SK55 May 16 '19 at 05:25

3 Answers3

17

Based on your input I propose below folder structure and Dockerfile.

[Solution] 'BuySellApi' (3 Projects)
  |
  +-- Dockerfile
  | 
  +-- [BuySellApi]
  |    |
  |    +--- BuySellApi.csproj
  |
  +-- [BuySellApi.Core]
  |    |
  |    +--- BuySellApi.Core.csproj
  |
  +-- [BuySellApi.Data]
       |
       +--- BuySellApi.Data.csproj

Dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
    
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY . .
RUN dotnet restore ". BuySellApi/BuySellApi.csproj"
WORKDIR "/src/BuySellApi"
RUN dotnet build "BuySellApi.csproj" -c Release -o /app
    
FROM build AS publish
WORKDIR "/src/BuySellApi"
RUN dotnet publish "BuySellApi.csproj" -c Release -o /app
    
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "BuySellApi.dll", "--server.urls", "http://0.0.0.0:5000"]
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Wouldn't it be better to first copy only *.csproj files, restore, and then copy rest of the files? – Piotr Perak Jun 14 '22 at 15:00
  • I really don't know dotnet, but you might be right if only *.csproj files are needed for the restore phase – Mihai Jun 29 '22 at 12:36
15

In my case the project file was not found because i was building a linux container, and for some reason, the project filename and it's path had different letter case then in the filesystem.

MiguelSlv
  • 14,067
  • 15
  • 102
  • 169
4

As suggested by @Mihai

I moved my Dockerfile directly under solution file and made some changes to it as below:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["BuySellApi/BuySellApi.csproj", "BuySellApi/"]
COPY ["BuySellApi.Core/BuySellApi.Core.csproj", "BuySellApi.Core/"]
COPY ["BuySellApi.Data/BuySellApi.Data.csproj", "BuySellApi.Data/"]
RUN dotnet restore "BuySellApi/BuySellApi.csproj"
COPY . .
WORKDIR "/src/BuySellApi"
RUN dotnet build "BuySellApi.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "BuySellApi.dll", "--server.urls", "http://0.0.0.0:5000"]
55SK55
  • 621
  • 2
  • 8
  • 23