C
C#2y ago
Hercules

❔ My web api works fine when i run it from VS but i can't publish it?

I have an web api project that work perfectly fine and as expected but when im trying to create an image or containerize it i keep getting errors when i try to publish the .csproj file. With this following command
RUN dotnet publish "./Api.csproj" -c Release -o out
RUN dotnet publish "./Api.csproj" -c Release -o out
Error message that displays is in the screenshot
28 Replies
Becquerel
Becquerel2y ago
when i got these errors it was because of the docker build context basically, docker only 'sees' files and folders from whatever folder you run it in if you run docker build from within your project directory it can't see the other projects in your solution the answer is to run docker build in your solution directory, and pass in your project's dockerfile with the -f flag docker build . -f YourProject/YourProject.csproj
Hercules
HerculesOP2y ago
Aha, I though docker didn't care about the other projects and that it would find everything when I run the
dotnet restore
dotnet restore
command after copying the .csproj file
Becquerel
Becquerel2y ago
if you use a dockerfile generated by visual studio, they leverage a docker feature called 'multi-stage builds' which might be the cause of the issue - they make the resulting images smaller but sometimes work unintuitively in my experience
Hercules
HerculesOP2y ago
@Becquerel I will try to do what you explained and see if it works.
Becquerel
Becquerel2y ago
dotnet restore might have also worked just once in the past and then been cached so it's not even run when you do docker build now
Hercules
HerculesOP2y ago
it would be worth a try if I cleaned the cache?
Becquerel
Becquerel2y ago
you can add --no-cache to docker build to achieve that i would try changing the build context first but no-cache is very useful for when you're debugging why docker isn't working
jcotton42
jcotton422y ago
Post your dockerfile
Hercules
HerculesOP2y ago
Thanks for that advice. @Becquerel
Hercules
HerculesOP2y ago
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

# Copy everything
COPY . .

# Restore as distinct layers
RUN dotnet restore "./Api.csproj"
RUN dotnet publish "./Api.csproj" -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY --from=build /src/out .

ENTRYPOINT ["dotnet", "Api.dll"]
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

# Copy everything
COPY . .

# Restore as distinct layers
RUN dotnet restore "./Api.csproj"
RUN dotnet publish "./Api.csproj" -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY --from=build /src/out .

ENTRYPOINT ["dotnet", "Api.dll"]
Hercules
HerculesOP2y ago
@jcotton42 sure 🔝
jcotton42
jcotton422y ago
It looks like you're only copying Api into the image But not it's referenced projects
Hercules
HerculesOP2y ago
yes, because i thought dotnet restore would restore all dependecies
jcotton42
jcotton422y ago
Nuget references yes Project references no Where would it even restore them from? You need to copy the entire source tree into the image
Hercules
HerculesOP2y ago
From the csproj file, that file has them registred no?
jcotton42
jcotton422y ago
dotnet can't see that You're inside a different filesystem, isolated from the host
Hercules
HerculesOP2y ago
I could change the build context send my dockerfile one level up where my solution file is. Just as suggested @Becquerel and modifiy the paths to ./Api/Api.csproj.
Becquerel
Becquerel2y ago
if you right-click on your project, go to 'add', then 'add docker support', what does the dockerfile VS generates look like? build context will work, just might be a tad slower. VS's dockerfile, in my experience, will try to intelligently copy over just the csprojs rather than everything in the entire context also, no, keep the dockerfile in the same place run the docker command from the solution root
Hercules
HerculesOP2y ago
this is what docker support gives me
Becquerel
Becquerel2y ago
yeah, ok you should be able to run that from the solution root and have it Just Work
Hercules
HerculesOP2y ago
the dockerfile is not in the solution i belive its in the api directory.
Becquerel
Becquerel2y ago
that's fine you can use the -f flag i mentioned to specify where the dockerfile is
Hercules
HerculesOP2y ago
@Becquerel That acutally worked i successfully created an image "sort-it" with the following command. From the solution directory.
docker build -t sort-it -f ./Api/Dockerfile .
docker build -t sort-it -f ./Api/Dockerfile .
Hercules
HerculesOP2y ago
Becquerel
Becquerel2y ago
thumbsupsmiley to the wild and sometimes frustrating world of docker
Hercules
HerculesOP2y ago
Haha. next wild thing is to run the image in a container. I really hope
docker run sort-it
docker run sort-it
will be enough. @Becquerel and @jcotton42 I like to thank you for actually giving me deeper context on understanding how docker works.
Becquerel
Becquerel2y ago
no prob 🙂 i went through similar frustrations only a little while ago so glad to help
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server