How find file in Docker?

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

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" ]
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

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/..\..\..\..\..\mock\programminglanguages\programminglanguage.js'
2024-09-21 04:40:15 Unhandled exception. System.IO.FileNotFoundException: Could not find file '/app/..\..\..\..\..\mock\programminglanguages\programminglanguage.js'
Here’s the code I’m using to read the file for seeding during migration:
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string relativePath = @"..\..\..\..\..\mock\programminglanguages\programminglanguage.js";
string fullPath = Path.GetFullPath(Path.Combine(baseDir, relativePath));
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string relativePath = @"..\..\..\..\..\mock\programminglanguages\programminglanguage.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.
5 Replies
jcotton42
jcotton423mo ago
@TotechsStrypper Set the file to copy to the output dir on build, then Path.Combine the filename with AppContext.BaseDirectory. From your Dockerfile, COPY --from=build /app/src/out ., only the publish output has been copied into your final container, which doesn't include that js file. (Also, is it actually javascript, or just json?)
TotechsStrypper
TotechsStrypperOP3mo ago
my bad it's a json file not a js file 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 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/LightningLanes/API src/LightningLanes/API
COPY src/LightningLanes/Core/LightningLanes.Core src/LightningLanes/Core/LightningLanes.Core
COPY src/Contracts src/Contracts
WORKDIR /app/src/LightningLanes/API
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", "LightningLanes.dll" ]
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 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/LightningLanes/API src/LightningLanes/API
COPY src/LightningLanes/Core/LightningLanes.Core src/LightningLanes/Core/LightningLanes.Core
COPY src/Contracts src/Contracts
WORKDIR /app/src/LightningLanes/API
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", "LightningLanes.dll" ]
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3mo ago
ARG DOTNET_VERSION=8.0
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS build
WORKDIR /sln

COPY ./*.sln ./nuget.config ./

# Copy the main source project files
COPY src/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p src/${file%.*}/ && mv $file src/${file%.*}/; done

# Copy the test project files
COPY test/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p test/${file%.*}/ && mv $file test/${file%.*}/; done

RUN dotnet restore

# possible FROM .... for multi stage here
COPY . .
RUN dotnet build

RUN dotnet publish --project src/Your.Project/Your.Project.csproj -c Release -o /publish --no-restore

# final stage/image
ARG DOTNET_VERSION=8.0
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}
WORKDIR /app
COPY --from=build /publish .
ENTRYPOINT ["dotnet", "Your.Project.dll"]
ARG DOTNET_VERSION=8.0
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS build
WORKDIR /sln

COPY ./*.sln ./nuget.config ./

# Copy the main source project files
COPY src/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p src/${file%.*}/ && mv $file src/${file%.*}/; done

# Copy the test project files
COPY test/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p test/${file%.*}/ && mv $file test/${file%.*}/; done

RUN dotnet restore

# possible FROM .... for multi stage here
COPY . .
RUN dotnet build

RUN dotnet publish --project src/Your.Project/Your.Project.csproj -c Release -o /publish --no-restore

# final stage/image
ARG DOTNET_VERSION=8.0
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}
WORKDIR /app
COPY --from=build /publish .
ENTRYPOINT ["dotnet", "Your.Project.dll"]
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server