Dockertools: How to set build context in Visual Studio

Created on 18 Jan 2019  路  3Comments  路  Source: microsoft/DockerTools

We have s solution with a directory structure as this (partially):

\Source
\Source\*.sln
\Source\Web\WebApp1\WebApp1
\Source\Web\WebApp1\WebApp1\WebApp1.csproj
\Source\Web\WebApp1\WebApp1\Dockerfile

When building from command-line and VSTS, the build context is set to \Source, like:

docker build . -f Web\WebApp1\WebApp1\Dockerfile

This works well. However, when trying to debug with Docker, or right-click the Dockerfile and select "Build Docker Image", Visual Studio sends it's own build context. It's not the directory of the *.sln file (as we want), nor is it the directory of the Dockerfile (as you would suspect). It's the parent directory of the Dockerfile:

docker build -f "C:\<whole_path_to_source>\Source\Web\WebApp1\WebApp1\Dockerfile" -t WebApp1:dev --target build "C:\<whole_path_to_source>\Source\Web\WebApp1"

So, is there a way to control how VIsual Studio sets the build context path? It seems like the tool only supports the default solution template where the project is the immediate child to the solution.

Most helpful comment

Hey @SWarnberg,

Yes, this is possible! You can set a property in your WebApp1.csproj, DockerfileContext, to accomplish this. For example, with the directory structure you have:

...
  <PropertyGroup>
    <DockerfileContext>..\..\..</DockerfileContext>
  </PropertyGroup>
...

Regrettably the documentation around properties like this is lacking, and we are working on that.

All 3 comments

Hey @SWarnberg,

Yes, this is possible! You can set a property in your WebApp1.csproj, DockerfileContext, to accomplish this. For example, with the directory structure you have:

...
  <PropertyGroup>
    <DockerfileContext>..\..\..</DockerfileContext>
  </PropertyGroup>
...

Regrettably the documentation around properties like this is lacking, and we are working on that.

I'll mark this issue as closed now, please let us know if you think it should be reopened.

This was most helpful. There seems to be an disagreement between the Visual Studio docker build environment, and the Azure DevOps build environment, as to how the docker build command should be initiated. The default Visual Studio generated docker file is placed in the default project folder location, underneath the solution folder. The docker file contents is careful to explicitly copy the csproj solution file to the docker image folder with this generated line:
COPY ["MyTest1/MyTest1.csproj", "MyTest1/"]
The dockerfile context is the project folder.

Meanwhile, over in Azure DevOps, the default azure-pipelines.yaml file seems to want the dockerfile to be in folder above the csproj folder.

After many hours of searching and trying things out, I couldn't make DevOps happy with the dockerfile in the project folder, but eventually with the help of this post, plus the one defining the dockerfile location I could set up my project with the generated VS Dockerfile moved to the solution folder, and then able to run with the default yaml in DevOps, and the default visual studio Docker build. Hoorah!

(Note, without this <DockerfileContext>, I could actually build and run with VS, but it was passing in the context of my entire VIsualStudio2019 folder, meaning 100s of MB of my various projects, rather than just the few megs of my single project. If it helps, my entry is <DockerfileContext>.</DockerfileContext>

If I'm right (and not missing something), it would be really helpful for the VS and DevOps Docker teams to decide on consistent defaults for the Dockerfile location and Context, so the simple newbie experience of building a simple project locally for Docker, and in DevOps for Docker, is way smoother than my experience has been!

Was this page helpful?
0 / 5 - 0 ratings