Scott Hanselman

Trying out Container Tools in Visual Studio 2019

January 24, '20 Comments [9] Posted in Docker
Sponsored By

I've been doing more and more work in Docker containers (rather than on the metal) and I noticed recently that Visual Studio 2019 added updated support for containers within VS itself so gave it a try.

When you make a new ASP.NET Core web app, make sure to check "enable docker support" when you click create.

Enable docker support

You'll need Docker for Windows first, of course. I'm using the new Docker Desktop for Windows that uses WSL2 for its backend rather than a utility VM that's visible in Hyper-V.

Now, within Visual Studio 2019, go to the View Menu and click "Other Windows | Containers." I like to dock this new tool window at the bottom.

Container Tool Window in Visual Studio 2019

Note in my screenshot above I'm starting up SQL Server on Linux within a container. This window is fantastic and includes basically everything you'd want to know and see when developing within a container.

You can see the ports exposed, the container's local file system, the environment, and the logs as they happen.

Docker Environment Variables

You can even right-click on a container and get a Terminal Window into that running container if you like:

Terminal in a running Container

You can also see to understand how Visual Studio uses your multistage Dockerfile (like the one below) to build your images for faster debugging.

FROM AS base

FROM AS build
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish

FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

Go read about the new Container Tools in Visual Studio. Chances are you have a dockerfile in your project but you haven't brought this Containers Tool Window out to play!

Sponsor: Organizations that scan their code more than 300 times a year have 5x less security debt than those with sporadic testing processes. The 2019 SOSS X report from Veracode digs into this data—and more.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Wednesday, January 29, 2020 5:50:16 PM UTC
Unfortunately installing Docker for Windows involves enabling Hyper-V. That in turn completely hoses any VMs for VMWare or VirtualBox that I need. So basically, non-starter for me.
Robert G
Wednesday, January 29, 2020 8:17:07 PM UTC
While it may not be "the" answer for your situation @RobertG, I have setup my boot to have Hyper-V on or off as like you I have VirtualBox but I was able to work in an "or" situation. I realize some may not be able to.

How I did it-> Switch easily between virtual box and HyperV with BCDEdit Boot entry from Scott Hanselman. :-)

Kevin LaBranche
Thursday, January 30, 2020 4:20:52 AM UTC
@RobertG - I had the same issue, and got around it by running ubuntu in a virutal box VM. You can mount your code into the VM, so you can develop in windows, but anytime you need to work with docker you switch to the VM.

Also, Visual Studio Code has a REALLY nice add-in for docker (ms-azuretools.vscode-docker). I use this to manage my containers and images within Ubuntu.
Daniel Mackay
Thursday, January 30, 2020 6:11:41 AM UTC
Now only if the new C# .net core app project template will let you build either inside or outside a container and not default to build inside a container always.

Thursday, January 30, 2020 7:45:29 AM UTC
Thank you for sharing a really nice topic
Thursday, January 30, 2020 10:49:53 AM UTC
i don't have any knowledge about
George Thomson
Saturday, February 01, 2020 7:42:28 PM UTC
It is among the most useful information I have read for a long time. Good information is given about the current life. Thank you
Monday, February 03, 2020 2:39:24 PM UTC
when i start visual studio 2019 and tries to create a new project from templates i cant find windows form VB that always have been there :( can anyone help me??

Thursday, February 06, 2020 12:33:40 PM UTC
Docker n00b here, but why build into the build layer and then turn right around and publish into the publish layer? Does the publish step not accomplish both? Asking to learn rather than to challenge.
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.