Home > Software engineering >  Dockerfile fails because I cannot copy the class library it depends on
Dockerfile fails because I cannot copy the class library it depends on

Time:09-10

I'm trying to build an image from a Dockerfile. The problem is that it is not able to find the Messages Class Library which means I have to do something like COPY ["src/MarketData.Subscriber/MarketData.Subscriber.csproj", "MarketData.Subscriber/"] COPY ["src/MarketData.Messages/MarketData.Messages.csproj", "MarketData.Messages/"] but COPY fails when I add these below WORKDIR. How do I solve it?

Folder Structure

 .
  ---src
 |    ---MarketData.Api
 |    ---MarketData.Messages
 |    ---MarketData.Subscriber
 |        ---Dockerfile
  ---MarketData.sln

Dockerfile

# Stage 1 - Build
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder

WORKDIR /app/src

# Restore
COPY *.csproj .
RUN dotnet restore -r linux-x64

# Build
COPY . .
RUN dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore

# Stage 2 - Publish
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app

RUN addgroup --system --gid 101 app \
    && adduser --system --ingroup app --uid 101 app


COPY --from=builder --chown=app:app /app/publish .

USER app
ENTRYPOINT ["./MarketData.Subscriber"]

Error

[ ] Building 4.0s (14/15)
 => [internal] load build definition from Dockerfile                                                                                                                                                        0.0s
 => => transferring dockerfile: 589B                                                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                                                           0.0s
 => => transferring context: 35B                                                                                                                                                                            0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:7.0                                                                                                                                           0.4s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:7.0                                                                                                                                        0.0s
 => [stage-1 1/4] FROM mcr.microsoft.com/dotnet/aspnet:7.0                                                                                                                                                  0.0s
 => [builder 1/6] FROM mcr.microsoft.com/dotnet/sdk:7.0@sha256:66a16cd71b82a2df577e52dfd01d8039ed4c20e3f17fca31b2fb3cf5929eaa7f                                                                             0.0s
 => [internal] load build context                                                                                                                                                                           0.0s
 => => transferring context: 650B                                                                                                                                                                           0.0s
 => CACHED [stage-1 2/4] WORKDIR /app                                                                                                                                                                       0.0s
 => CACHED [stage-1 3/4] RUN addgroup --system --gid 101 app     && adduser --system --ingroup app --uid 101 app                                                                                            0.0s
 => CACHED [builder 2/6] WORKDIR /app/src                                                                                                                                                                   0.0s
 => CACHED [builder 3/6] COPY *.csproj .                                                                                                                                                                    0.0s
 => CACHED [builder 4/6] RUN dotnet restore -r linux-x64                                                                                                                                                    0.0s
 => CACHED [builder 5/6] COPY . .                                                                                                                                                                           0.0s
 => ERROR [builder 6/6] RUN dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore                                                                                         3.5s
------
 > [builder 6/6] RUN dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore:
#14 0.567 MSBuild version 17.4.0-preview-22368-02 c8492483a for .NET
#14 1.270 /usr/share/dotnet/sdk/7.0.100-preview.7.22377.5/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.RuntimeIdentifierInference.targets(219,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [/app/src/MarketData.Subscriber.csproj]
#14 1.532 /usr/share/dotnet/sdk/7.0.100-preview.7.22377.5/Microsoft.Common.CurrentVersion.targets(2082,5): warning : The referenced project '../MarketData.Messages/MarketData.Messages.csproj' does not exist. [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(2,7): error CS0246: The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(3,18): error CS0234: The type or namespace name 'Messages' does not exist in the namespace 'MarketData' (are you missing an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(5,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/Actors/FtxSubscriptionGrain.cs(6,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/ActorSystemConfiguration.cs(1,18): error CS0234: The type or namespace name 'Messages' does not exist in the namespace 'MarketData' (are you missing an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/ActorSystemConfiguration.cs(3,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.416 /app/src/ActorSystemConfiguration.cs(4,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(5,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(6,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(7,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(8,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemConfiguration.cs(9,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(1,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(2,7): error CS0246: The type or namespace name 'Proto' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(10,39): error CS0246: The type or namespace name 'FtxSubscriptionBase' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(12,22): error CS0246: The type or namespace name 'ClusterIdentity' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(18,9): error CS0246: The type or namespace name 'IContext' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(19,9): error CS0246: The type or namespace name 'ClusterIdentity' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(8,22): error CS0246: The type or namespace name 'ActorSystem' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/ActorSystemHostedService.cs(11,37): error CS0246: The type or namespace name 'ActorSystem' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(36,36): error CS0246: The type or namespace name 'SubscriptionRequest' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(51,38): error CS0246: The type or namespace name 'SubscriptionRequest' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
#14 3.417 /app/src/Actors/FtxSubscriptionGrain.cs(57,26): error CS0246: The type or namespace name 'SubscriptionStatus' could not be found (are you missing a using directive or an assembly reference?) [/app/src/MarketData.Subscriber.csproj]
------
executor failed running [/bin/sh -c dotnet publish -c Release -o /app/publish -r linux-x64 --no-self-contained --no-restore]: exit code: 1

CodePudding user response:

Keep your dockerfile at solution level. then only it will be able to find src folder.

your folder structure should look like this.

.
  ---src
 |    ---MarketData.Api
 |    ---MarketData.Messages
 |    ---MarketData.Subscriber
  ---MarketData.sln
  ---Dockerfile

and also add respective copy project fie cmd to dockerfile.

hope it helps.

CodePudding user response:

Thanks to Cisien, the issue was fixed. It was due to how I was running the build.

It should be run from the root and docker build . -f src/Dockerfile. The '.' denotes the build context, or in other words, the parent directory that is made available to the docker build process.

C:\Users\user\Desktop\MarketData>docker build -t marketdata:1.0 -f ./src/MarketData.Subscriber/Dockerfile .

Hope it helps someone else.

Dockerfile

# Stage 1 - Build
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder

WORKDIR /app/src

# Restore
COPY src/MarketData.Messages/MarketData.Messages.csproj MarketData.Messages/
COPY *.csproj .
RUN dotnet restore

# Build
COPY . .
RUN dotnet publish -c Release -o /app/publish --no-self-contained --no-restore

# Stage 2 - Publish
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app

RUN addgroup --system --gid 101 app \
    && adduser --system --ingroup app --uid 101 app

COPY --from=builder --chown=app:app /app/publish .

USER app
ENTRYPOINT ["./MarketData.Subscriber"]

  • Related