skip to Main Content

This is my Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:8.0 as build
WORKDIR /app
EXPOSE 80

COPY TotechsThunder.sln TotechsThunder.sln
COPY mock/programminglanguages/programminglanguage.js mock/programminglanguages/programminglanguage.js
COPY src/LightningLanes/API/LightningLanes.csproj src/LightningLanes/API/LightningLanes.csproj
COPY src/LightningLanes/Core/LightningLanes.Core/LightningLanes.Core.csproj src/LightningLanes/Core/LightningLanes.Core/LightningLanes.Core.csproj
COPY src/LightningLanes/Jobs/LightningLanes.HangfireJobHost/LightningLanes.HangfireJobHost.csproj src/LightningLanes/Jobs/LightningLanes.HangfireJobHost/LightningLanes.HangfireJobHost.csproj
COPY src/Features/GitHubFeatures/Logics/Logics.csproj src/Features/GitHubFeatures/Logics/Logics.csproj
COPY src/Features/GitHubFeatures/Tests/Tests.csproj src/Features/GitHubFeatures/Tests/Tests.csproj
COPY src/Technologies/Technologies.csproj src/Technologies/Technologies.csproj
COPY src/Projects/Projects.csproj src/Projects/Projects.csproj
COPY src/Contracts/Contracts.csproj src/Contracts/Contracts.csproj
COPY src/IdentityServer/IdentityServer.csproj src/IdentityServer/IdentityServer.csproj
COPY src/Gateway/Gateway.csproj src/Gateway/Gateway.csproj

RUN dotnet restore TotechsThunder.sln

COPY src/Technologies src/Technologies
COPY src/Contracts src/Contracts
WORKDIR /app/src/Technologies
RUN dotnet publish -c Release -o /app/src/out

FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/src/out .
ENTRYPOINT [ "dotnet", "Technologies.dll" ]

I’m having trouble with the programminglanguage.js file. This file contains master data, which my ASP.NET Core app uses during initialization to seed data into the database migration. Everything works fine in my development environment, but when I run the project in a Docker environment, the file can’t be found.

When I run docker compose build, the service builds successfully. However, when the project runs in Docker, I encounter the following error:

2024-09-21 04:40:15 Unhandled exception. System.IO.FileNotFoundException: Could not find file '/app/..........mockprogramminglanguagesprogramminglanguage.js'

Here’s the code I’m using to read the file for seeding during migration:

string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string relativePath = @"..........mockprogramminglanguagesprogramminglanguage.js";
string fullPath = Path.GetFullPath(Path.Combine(baseDir, relativePath));

In Docker, the issue seems to stem from the relative file path not being resolved correctly. I’d appreciate any help in resolving this so the app can find the file in the Docker environment.

Files location in my project

enter image description here

What I did try:

Trying to check inside the docker container if there’s a programminglanguage.js file in there, but the release + docker environment file system is just too unfamiliar for me to do an investigation

What I’m expecting:

programminglanguage.js is copied inside the container and the path

string relativePath = @"..........mockprogramminglanguagesprogramminglanguage.js";

that retrieve the file should not result no file

2

Answers


  1. Chosen as BEST ANSWER

    Solution

    FROM mcr.microsoft.com/dotnet/sdk:8.0 as build
    WORKDIR /app
    EXPOSE 80
    
    COPY TotechsThunder.sln TotechsThunder.sln
    COPY src/LightningLanes/API/LightningLanes.csproj src/LightningLanes/API/LightningLanes.csproj
    COPY mock/programminglanguages/programminglanguage.json mock/programminglanguages/programminglanguage.json
    COPY src/LightningLanes/Core/LightningLanes.Core/LightningLanes.Core.csproj src/LightningLanes/Core/LightningLanes.Core/LightningLanes.Core.csproj
    COPY src/LightningLanes/Jobs/LightningLanes.HangfireJobHost/LightningLanes.HangfireJobHost.csproj src/LightningLanes/Jobs/LightningLanes.HangfireJobHost/LightningLanes.HangfireJobHost.csproj
    COPY src/Features/GitHubFeatures/Logics/Logics.csproj src/Features/GitHubFeatures/Logics/Logics.csproj
    COPY src/Features/GitHubFeatures/Tests/Tests.csproj src/Features/GitHubFeatures/Tests/Tests.csproj
    COPY src/Technologies/Technologies.csproj src/Technologies/Technologies.csproj
    COPY src/Projects/Projects.csproj src/Projects/Projects.csproj
    COPY src/Contracts/Contracts.csproj src/Contracts/Contracts.csproj
    COPY src/IdentityServer/IdentityServer.csproj src/IdentityServer/IdentityServer.csproj
    COPY src/Gateway/Gateway.csproj src/Gateway/Gateway.csproj
    
    RUN dotnet restore TotechsThunder.sln
    
    COPY src/Technologies src/Technologies
    COPY src/Contracts src/Contracts
    WORKDIR /app/src/Technologies
    RUN dotnet publish -c Release -o /app/src/out
    
    FROM mcr.microsoft.com/dotnet/aspnet:8.0
    WORKDIR /app
    COPY --from=build /app/src/out .
    COPY --from=build /app/mock/programminglanguages/programminglanguage.json ./mock/programminglanguages/programminglanguage.json
    ENTRYPOINT [ "dotnet", "Technologies.dll" ]
    

  2. The problem may reside in using backslashes instead of normal ones, since Docker is running Linux-based images, Windows file paths will not be recognized well

    Posting as an answer because comments require more reputation(???)

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