"Docker build path not found" in docker support Visual Studio Project - visual-studio

I have C# solution, with 4 projects, 3 being Dlls and 1 is the console application uses those DLLs.
I tried to use docker support on visual studio to build my docker image, it fails
My dockerfile as below:
> FROM microsoft/windowsservercore:ltsc2016
> EXPOSE 80
> EXPOSE 1433
> EXPOSE 29051
>
> COPY bin/x64/debug /root/ ENTRYPOINT
> /root/RmsMainConsole.exe
I CD into directory where my dockerfile is at and execute docker build.
Error:
Docker CLI command : docker build -t rmsmainconsole:self-hosted .
Sending build context to Docker daemon 55.61MB
Step 1/6 : FROM microsoft/windowsservercore:ltsc2016
---> 9dbf7f740334
Step 2/6 : EXPOSE 80
---> Using cache
---> ad0ad85fd107
Step 3/6 : EXPOSE 1433
---> Using cache
---> 81ba13dbd4d4
Step 4/6 : EXPOSE 29051
---> Using cache
---> 1fa3db800abf
Step 5/6 : COPY bin/x64/debug /root/
COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder043346063\bin\x64\debug: The system cannot find the path specified.
You can see that PATH couldn't be found.
Whereas i tried to create docker file on my .sln level.
I changed one line in my docker file.
COPY RmsMainConsole/bin/x64/debug /root/
Note: "RmsMainConsole" directory has been added.
i executed the docker build at my .sln level and i built it successfully.
Logs are:
Sending build context to Docker daemon 1.15GB
Step 1/6 : FROM microsoft/windowsservercore:ltsc2016
---> 9dbf7f740334
Step 2/6 : EXPOSE 80
---> Running in fe97cf236d5a
Removing intermediate container fe97cf236d5a
---> c31e236353b6
Step 3/6 : EXPOSE 1433
---> Running in f031fce5ecba
Removing intermediate container f031fce5ecba
---> 96c704c68ffb
Step 4/6 : EXPOSE 29051
---> Running in 365e2be43d0e
Removing intermediate container 365e2be43d0e
---> d30c3fb2214b
Step 5/6 : COPY RmsMainConsole/bin/x64/debug /root/
---> b214c1edc256
Step 6/6 : ENTRYPOINT /root/RmsMainConsole.exe
---> Running in 5c819915532a
Removing intermediate container 5c819915532a
---> 247f01bb9b82
Successfully built 247f01bb9b82
Successfully tagged rmsmainconsole:self-hosted
I noticed two difference between the successful and failed build:
1. Size on the docker context
2. The logs at step 5/6:
COPY failed: CreateFile \?\C:\ProgramData\Docker\tmp\docker-builder043346063\bin\x64\debug: The system cannot find the path specified.
and
---> b214c1edc256
How should i use visual studio docker support to build my docker image. Where did i go wrong ?

Not sure, whether I answer your question - first or second :).
I was also wondering how Visual Studio is using Docker, because Dockerfile is created within project folder, but it contains paths for COPY like it was in the root (sln) folder.
I played with it a bit and answer is pretty simple.
Visual Studio builds images from the solution folder using -f switch to point to Dockerfile.
You can watch Output window to see it running following command:
docker build -f "<path to Dockerfile>" -t <name:tag> --target base --label "com.microsoft.created-by=visual-studio" "<solution dir>"
So, I would recommend having Dockerfile in project folder and run it from the root folder using -f option like Visual Studio does it.
Does it answer your question(s)?

After some thoughts, trials and errors, the problems does not lie on docker support visual studio. Docker build has to be run at the root of my solution directory, as docker build need to be able to locate all the dependencies the image file that is depending on.
but what got me curious is, then how is docker support for visual studio come into play ? I can only Right click -> Add Docker support at my console application project.
My current solution is to manually add a docker file at the root of solution. Would be glad if anyone knows visual studio docker support has a better way to solve this ?
Thanks

I think the actual reason for this is much simpler. If you look at the docker ignore (.dockerignore) file, you'll notice that it ignores everything except for a couple of obj folders. Try removing the * at the top of the docker ignore file to test this out. Then you can selectively ignore things you have in the project folder that you'd like to copy over. I ran into this as well and it took lots of trial and error before I thought to look at the ignore file.

Related

DotNet 6 fails to run in a Windows container: Permission denied

I am not familiar with Windows containers, and I am getting this error when trying to debug a hello-world DotNet 6 application in VS2022:
Cannot use file stream for [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.json]: Permission denied
Invalid runtimeconfig.json [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.json] [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.dev.json]
The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program '[13360] dotnet.exe' has exited with code 2147516563 (0x80008093).
Dockerfile (default):
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApplication5/WebApplication5.csproj", "WebApplication5/"]
RUN dotnet restore "WebApplication5/WebApplication5.csproj"
COPY . .
WORKDIR "/src/WebApplication5"
RUN dotnet build "WebApplication5.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication5.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication5.dll"]
I tried to spin the container in an interactive way, to validate if the file was actually there but this command did not work:
docker run -it --entrypoint powershell.exe webapplication5
docker: Error response from daemon: container 8581fd662043dbfad4a2b68e561d21c106e4754b6f21c4d06cfb9b3d5e2b0a39 encountered an error during hcsshim::System::CreateProcess: failure in a Windows system call: The system cannot find the file specified. (0x2).
What can the problem be?
Why I cannot get a PS inside the container?
My docker user needs more permissions. I solved it in an easy way because it is for testing.. don't do this at home, and grant the minimum permissions instead:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
# Here is the risky trick
USER Administrator

Docker build command creates large amount of folders in windowsfilter folder: what's the purpose and how to control

I'm building a new image like so docker build . -t test and copy contents from host OS folder into it:
Dockerfile
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir test
COPY test /root/test
#USING 'COPY' here instead of 'ADD' because of https://stackoverflow.com/questions/24958140/what-is-the-difference-between-the-copy-and-add-commands-in-a-dockerfile
I get this output in Powershell:
PS D:\Programs> docker build . -t test
Sending build context to Docker daemon 1.644GB
Step 1/7 : FROM mcr.microsoft.com/windows/servercore:ltsc2019
ltsc2019: Pulling from windows/servercore
4612f6d0b889: Pull complete
c67ded6868b6: Pull complete
Digest: sha256:1be9c8378b8c32d31538253a4b96169f5138a5f6d4c0a04e8d8f9a80b9ac8097
Status: Downloaded newer image for mcr.microsoft.com/windows/servercore:ltsc2019
---> d1724c2d9a84
Step 2/7 : SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
---> Running in 1bfefefbe433
Removing intermediate container 1bfefefbe433
---> 37de702deb33
Step 3/7 : RUN mkdir root
---> Running in e26d6b49ced7
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 9/8/2021 1:07 PM root
Removing intermediate container e26d6b49ced7
---> 451c4d3f3ea1
Step 4/7 : RUN cd root
---> Running in 74a228f8118f
Removing intermediate container 74a228f8118f
---> 3f175ac67f1d
Step 5/7 : WORKDIR /root
---> Running in 5f783d5b2332
Removing intermediate container 5f783d5b2332
---> 68b24e033f87
Step 6/7 : RUN mkdir test
---> Running in 5771bb7a593a
Directory: C:\root
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 9/8/2021 1:08 PM test
Removing intermediate container 5771bb7a593a
---> 35fa0b2af157
Step 7/7 : COPY test /root/test
---> 60eab8242865
Successfully built 60eab8242865
Successfully tagged test:latest
It creates EIGHT(!) folders on the host OS in folder "\docker\windowsfilter"
This seems excessive, it looks like a folder for each step in my Dockerfile, so what are all these folders for and could I prevent creation/autodelete the unneeded ones to keep everything tidy?
Host OS: Windows Server 2019 standard Version 1809 17763.2114
Docker: version 20.10.4, build 110e091
Please note that I do NOT use the Docker Desktop application, see here how I installed Docker.
Update 1
I updated my Dockerfile based on #Noam's suggestion like so:
#RUN mkdir root
#RUN cd root
#WORKDIR /root
WORKDIR /root #creates root directory if not exists, then enters it
After this change, 6 folders (instead of 8) are created in windowsfilter, so my hunch that Docker creates a folder for each command in Dockerfile seems to be correct.
Hallo #Flo this issue seems to be a long time issue specifically with docker desktop,
Causes:
One option that some times seems to explain it is upgrading from older versions of docker desktop and the software not cleaning up old directories.
Another I that docker saves built image layers there so if you have many versions of the same image your dangling ones could reside there requesting a run of docker image prune or maybe even docker system prune
Solutions:
An opensource cleanup script has been published and it seems to help a great deal, its called docker-ci-zap and for the most part it appears to handle it quite well.
if the issue still persists there is a fix suggested in the docker for desktop github issue page is adding -removing suffix to the folder names in the windowsfilter directory followed by a restart to allow docker to cleanup the unneeded folders
if it's still causing issues try returning to factory deafults and if that doesn't do it either maybe a fresh docker for desktop install might seal the deal though even then some experienced some difficulties so eventually if it really bothers and you need to use it, reinstalling windows usually looks like the final blow.
suggestion for Dockerfile improvments
in your Dockerfile the lines describing the folders might cause problems up a head du to unabsolute paths and improper use of the Dockerfile command.
replace:
RUN mkdir root
RUN cd root
WORKDIR /root
with:
WORKDIR /root
it achives the same purpose (creating and entering the new (if didn't exist yet) directory)
what does docker-ci-zap do?
You can get a rough sense by looking at the code used to compile the exe file that resides in the GitHub repo:
it uses the hcsshim driver and its DestroyLayer function that deletes the layer files from the host disk, or as the docs put it:
DestroyLayer will remove the on-disk files representing the layer
with the given path, including that layer's containing folder, if
any.

Error "getcwd() failed: No such file or directory" when trying to run from VS in Docker

I have a project set up to run locally in Docker with docker-compose. Until recently, it's been working fine. I don't believe I changed anything that should affect this (except maybe a VS upgrade?), and I've even tried rolling back to an older commit. In all cases, I'm now getting an error message, which appears in Visual Studio's output window as:
docker exec -i f93fb2962a1e sh -c ""dotnet" --additionalProbingPath /root/.nuget/packages --additionalProbingPath /root/.nuget/fallbackpackages "bin/Debug/netcoreapp3.1/MattsTwitchBot.Web.dll" | tee /dev/console"
sh: 0: getcwd() failed: No such file or directory
It was not possible to find any installed .NET Core SDKs
Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
https://aka.ms/dotnet-download
I've tried a variety of different things (changing the base image in the Docker file, deleting old images and containers, and more) but I keep getting the same error message. The weird thing is that when I do a File->New, Visual Studio generates a very similar looking Docker file and it works fine. I have no idea what the problem is, but I'm hoping someone here can spot it.
My full repo is available on Github. Here is the docker for the asp.net core project:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["MattsTwitchBot.Web/MattsTwitchBot.Web.csproj", "MattsTwitchBot.Web/"]
COPY ["MattsTwitchBot.Core/MattsTwitchBot.Core.csproj", "MattsTwitchBot.Core/"]
RUN dotnet restore "MattsTwitchBot.Web/MattsTwitchBot.Web.csproj"
COPY . .
WORKDIR "/src/MattsTwitchBot.Web"
RUN dotnet build "MattsTwitchBot.Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MattsTwitchBot.Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MattsTwitchBot.Web.dll"]
and the docker-compose for the solution (even without the Couchbase stuff I'm getting the same error, but I'm pasting it here for completeness):
version: '3.4'
services:
couchbase:
image: couchbase:6.5.0-beta2
volumes:
- "./couchbasetwitchbot:/opt/couchbase/var" # couchbase data folder
ports:
- "8091-8096:8091-8096" # https://docs.couchbase.com/server/current/install/install-ports.html
- "11210-11211:11210-11211"
mattstwitchbot.web:
image: ${DOCKER_REGISTRY-}mattstwitchbotweb
build:
context: .
dockerfile: MattsTwitchBot.Web/Dockerfile
environment:
Couchbase__Servers__0: http://couchbase:8091/ # Reference to the "couchbase" service name on line 4
depends_on:
- couchbase # Reference to the "couchbase" service name on line 4
command: ["./wait-for-it.sh", "http://couchbase:8091"]
I don't have enough reputation to comment but I think it might be your .csproj file. You mentioned you upgraded Visual Studio. Since the .csproj file contains information about the project (including references to system assemblies), and you are copying it in your Dockerfile, it's possible that:
The .csproj file needs to be updated since you updated VS.
The dotnet core version in your dockerfile 'FROM' statement is a different version than what you're using locally.
Maybe test this by starting a new project and adding your source, then do a diff on the old and new .csproj files. You can also backup the original and try modifying the .csproj file manually. I found a blog post that demonstrates upgrading a vs2015 csproj file to vs2017. Hopefully it helps.
Because I don't have enough reputation I cannot comment on your question.
But one thing that puzzles me is the fact that you are using as base image an image that doesn't have .Net SDK and if you try to run a command that requires an SDK looks it fails
I am assuming, in the container, f93fb2962a1e is using the image created by the docker file you posted in the question
getcwd() error, means the solution lost context to path. I found that completely removing the dock-compose solution and the associated Dockerfile file from the project fixed the problem. It's hacky but works if you're in a bind.
I had a similar issue and am debugging code that has previously worked. Simply closing my terminal and re-opening it resolved the issue for me. I am using WSL, and have changed some environment variables that appear to have caused the initial issues.
I belive that your current working directory got deleted or path to working directory was reseted. But it will be the first option because update of VS may removed directory /tmp on your docker machine so its not there anymore, and it will be created on some external event.
Or set port to blocking of connection to your docker machine.
Check connection to docker machine
Check existence of folder that is used as working directory on docker machine
If you didnt found problem then continue with this:
docker exec --it {containerID} /bin/sh
you can use this official docker debugging article
with this, follow the directories that is docker trying to access and check
for their existence.
With this debugging you shoud be able to discover problems.
I hope that it helped you

The docker image build is stuck

I need to build a windows docker image for a window app on windows 10 with docker desktop installed on vm, but when the build process is stuck when running the installation of this app. the log looks like something bellow,
PS C:\docker> docker image build . -t app
Sending build context to Docker daemon 144.1MB
Step 1/6 : FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8
---> 9b87edf03093
Step 2/6 : COPY . /app
---> ac4b1124d856
Step 3/6 : WORKDIR /app
---> Running in cf4bd2345d26
Removing intermediate container cf4bd2345d26
---> d4f28097afd9
Step 4/6 : RUN .\ELSA1.0_2.6.6.243.exe
---> Running in b9356f975aa6
(And the process is stuck for several hours here and is terminated by me)
the docker file is
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 # this is a base image because asp.net 3.5 is a prerequisite for the app
COPY . /app
WORKDIR /app
RUN .\ELSA1.0_2.6.6.243.exe # I am stuck here!
I have tried to do something in the base image like so, but it seems that I cannot do anything
PS C:\docker> docker container run -it
mcr.microsoft.com/dotnet/framework/aspnet:4.8
Service 'w3svc' has been stopped
Service 'w3svc' started
Are there any good ideas to debug this issue? By the way the installer can work normally on windows 10.
You should never execute the command in the RUN statement that not terminate. I see in Docker build logs that you started exe file in RUN command. This will keep stuck your docker build process and will wait for SIGINT. The same will happen like if you execure RUN npm start so it will hang the build process.
Add your executable at entrypoint or CMD.
Another thing that can be the issue in such cased
Considerations for using CMD with Windows
On Windows, file paths specified in the CMD instruction must use
forward slashes or have escaped backslashes \. The following are
valid CMD instructions:
dockerfile
# exec form
CMD ["c:\\Apache24\\bin\\httpd.exe", "-w"]
# shell form
CMD c:\\Apache24\\bin\\httpd.exe -w
You can further read about Window CMD here
However, the following format without the proper slashes will not work:
dockerfile
CMD c:\Apache24\bin\httpd.exe -w

Docker COPY fails in Rider when running VS generated dockerfile

I'm running into problems when trying to run a dockerfile that I created in VS2017 in Rider 2019.2. The Dockerfile works fine in VS2017, but since the company I work for also allows developers to use other IDE's we need the Dockerfile to work in Rider (for example) too.
I added a new Docker project wihtin my solution by adding Container Orchestrator Support in VS2017.
My .sln project structure is:
Bram.Website
> Bram.Website // contains the Dockerfile
> docker-compose // contains docker-compose.yml
The generated Dockerfile looks like this:
FROM microsoft/aspnet:4.7.2-windowsservercore-1803
ARG source
WORKDIR /inetpub/wwwrootDock
COPY ${source:-obj/Docker/publish} .
The generated docker-compose.yml is:
version: '3.4'
services:
bram.website:
image: ${DOCKER_REGISTRY-}bramwebsite
build:
context: .\Bram.Website
dockerfile: Dockerfile
I changed my JetBrains build configuration according to the information in this article: https://blog.jetbrains.com/dotnet/2018/07/18/debugging-asp-net-core-apps-local-docker-container/
My config now looks like:
My build command is:
docker build -t <image_tag> . && docker run -p 8000:80 --name bramwebsite <image_tag>
When I try to build this I get the following output:
Deploying 'bram-website Dockerfile: Bram.Website/Dockerfile'...
Building image...
Step 1/4 : FROM microsoft/aspnet:4.7.2-windowsservercore-1803
---> d58c3fe3a54e
Step 2/4 : ARG source
---> Using cache
---> 7be7e1854e6f
Step 3/4 : WORKDIR /inetpub/wwwroot
---> Using cache
---> 6d53dbdcf2cd
Step 4/4 : COPY ${source:-obj/Docker/publish} .
Error: ResponseItem.ErrorDetail[code=<null>,message=COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder371292226\obj\Docker\publish: The system cannot find the file specified.]
Failed to deploy 'bram-website Dockerfile: Bram.Website/Dockerfile': COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder371292226\obj\Docker\publish: The system cannot find the file specified.
I'm not sure if this is a problem with my config or if this is an issue in Rider or in the way VS generates the files. The folder at 'C:\ProgramData\Docker\tmp' appears to be empty. My best guess at the moment is that Rider is having difficulties with the 'source' macro that is injected by VS2017? Any help or insight is greatly appreciated.

Resources