❔ 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
Error message that displays is in the screenshot
28 Replies
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
Aha, I though docker didn't care about the other projects and that it would find everything when I run the command after copying the .csproj file
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
@Becquerel I will try to do what you explained and see if it works.
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
it would be worth a try if I cleaned the cache?
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 workingPost your dockerfile
Thanks for that advice. @Becquerel
@jcotton42 sure 🔝
It looks like you're only copying Api into the image
But not it's referenced projects
yes, because i thought dotnet restore would restore all dependecies
Nuget references yes
Project references no
Where would it even restore them from?
You need to copy the entire source tree into the image
From the csproj file, that file has them registred no?
dotnet can't see that
You're inside a different filesystem, isolated from the host
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.
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
this is what docker support gives me
yeah, ok
you should be able to run that from the solution root and have it Just Work
the dockerfile is not in the solution i belive its in the api directory.
that's fine
you can use the
-f
flag i mentioned to specify where the dockerfile is@Becquerel That acutally worked i successfully created an image "sort-it" with the following command. From the solution directory.
to the wild and sometimes frustrating world of docker
Haha. next wild thing is to run the image in a container. I really hope will be enough.
@Becquerel and @jcotton42 I like to thank you for actually giving me deeper context on understanding how docker works.
no prob 🙂 i went through similar frustrations only a little while ago
so glad to help
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.