9

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 ?

csamleong
  • 769
  • 1
  • 11
  • 24
  • Here is a very easy way to pass this error in Visual Studio 2019 https://stackoverflow.com/questions/44025671/can-not-find-docker-container/55259280# – Hamit YILDIRIM Mar 20 '19 at 11:10

3 Answers3

7

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)?

Waldemar Mękal
  • 101
  • 1
  • 5
4

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

csamleong
  • 769
  • 1
  • 11
  • 24
3

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.

GetFuzzy
  • 2,116
  • 3
  • 26
  • 42
  • That worked for me. I deleted the previously, manually created `.dockerignore` file, and let Visual Studio 2022 created it for me automatically. – Pine Code Mar 23 '22 at 10:22