The goal of the following content is to help you leverage the power of Visual Studio and Visual Studio Code as you develop with Docker
- Windows 10 Professional or Enterprise with Anniversary Update (version 1607) or later
- Hyper-V enabled
- Visual Studio 2017 (v 15.7 or later) or Visual Studio 2019
- Docker Desktop for Windows
- Visual Studio Code
- Latest .NET Core 2.2 SDK
-
Open Visual Studio 2017 and use
Ctrl+Shift+Nto open the New Project dialogue -
Create a new Visual C#, ASP.NET Core Web Application and name it
WebFrontEnd. Click 'OK' to continue. -
Configure your project to use
.NET Core, theASP.NET Core 2.2 SDKand theWeb Application (Model-View-Controller)template. You will notice you can selectEnable Docker Supportwhen creating a new project. In this lab, we will be adding Docker Support at a later time, but keep in mind you can leverage this feature in the future to add Docker Support during project creation. Click 'OK' to create.Note: For this lab, we will not configure for HTTPS.
-
Open the Solution Explorer using
Ctrl+Alt+Lto view your project files. Your output should mirror the image below. -
Use
F5orCtrl+F5to run your project and confirm it is working as expected.Now for the fun stuff! Once you have confirmed that your application is running locally, you can get started with Docker tools for Visual Studio
Important: Before continuing with the lab, ensure Docker For Desktop is in Linux-mode. To confirm you are in Linux-mode, use the following steps:
-
Right-click the Docker Desktop icon located in the lower right-hand corner of your taskbar
-
If you are in Linux-mode already, you will see
Switch to Windows containers. Otherwise, you will need to selectSwitch to Linux containers.This switch will tell Docker Desktop to use the MobyLinuxVM in Hyper-V to run Linux containers. You can view this VM by opening
Hyper-V Manager. -
Once you have confirmed you are in Linux-Mode, return to Visual Studio. Right-click your
WebFrontEndproject in the Solution Explorer, selectAddfrom the menu and then proceed to selectDocker Support. You will then be prompted to select a target operating system: Windows or Linux. SelectLinuxand then click 'OK' to continue.Note: .NET Core is cross-platform, therefore the
WebFrontEndapplication can run on Windows and Linux. You can adapt the lab instructions to target Windows if you desire. -
After adding Docker Support, validate that a
Dockerfilewas added to theWebFrontEndproject.This lab assumes prior experience with Dockerfiles. If you would like to learn more about Dockerfiles, check out the following resources:
Recently, Microsoft changed the hosting location for Microsoft .NET Core container images. This change is not yet reflected in the auto-generated Dockerfile. Therefore, we will update it manually to adhere to best practices. For more information on the Microsoft Container Registry:
-
Modify the initial
FROMstatement in the Dockerfile to use the MCR hosted ASP.NET Core Runtime image:mcr.microsoft.com/dotnet/core/aspnet:2.2. -
Modify the
FROMstatement online 5in the Dockerfile to use the MCR hosted .NET Core SDK image:mcr.microsoft.com/dotnet/core/sdk:2.2. -
Now, you can use
F5orCtrl+F5to run your application in a Docker container!Note: With Docker Support, you can debug your application as if it is running locally! To see this in action, place a breakpoint on line 15 in
HomeController.csand refresh the application homepage! -
With your Containerized Docker application running in Visual Studio, open a Command Prompt window. Run the
docker pscommand to view the running container via the Docker CLI. -
For the sake of not recreating the wheel, reference the following documentation to access hands-on-labs for working with multicontainer applications, container orchestators, Azure Dev Spaces and more.
-
Open a New Visual Studio Code window and use
Ctrl+K Ctrl+Oto open the File Explorer -
Create a new folder called
WebApiand open the folder in VS Code -
Toggle the Terminal using
Ctrl+`and rundotnet --list-sdksto ensure you have the latest .NET Core SDK (2.2.203 as of April 2019). If you need to download the latest version, click here -
Once you have confirmed your SDK version, use the
dotnet --list-runtimescommand to confirm you have the latest .NET Core Runtime (2.2.4 as of April 2019)Note: If you have the .NET Core 3.0 preview installed, run
dotnet new globaljson --sdk-version 2.2.203to ensure your Web Api project targets the .NET Core 2.2 SDK -
Create a new .NET Core Web Api project by running the following command
dotnet new webapi -
Once your project has been created, navigate to the
Startup.csand useCtrl+K Ctrl+Cto comment out line 44. If you would like to learn more HTTPS with Docker and .NET Core, leverage the following sources: -
Test out the application by running
dotnet runin the Terminal window and navigating to http://localhost:5000/api/values or https://localhost:5001/api/values. To stop the application, return to the terminal andCtrl+C
-
Now, it's time to generate a Dockerfile. Using VS Code and the Docker Extension, we can
Ctrl+Shift+Pand type inDocker:Add Docker Files to Workspace. For ASP.NET Core applications, docker-compose files are not automatically generated. Enter the below information to build the Dockerfile:- Application Platform: ASP.NET Core
- OS: Linux
- Port: 80
Remember: The Dockerfile generated should be viewed as a starting place. You may have to make modifications as you develop your custom solution. Use the built-in Dockerfile syntax support to your advantage!
-
Once the Dockerfile has been added to your Workspace, you can leverage it to build your container image. Don't forget to update the Dockerfile with the new MCR-hosted base OS images. To build an image, you can do any of the following:
- Work with the Docker CLI in the Terminal to run a
docker build -f "Dockerfile" -t webapi:1.0(For more information on tagging your images, click here) - Right-click the Dockerfile and select
Build Image Ctrl+Shift+Pand typeDocker:Build Image
- Work with the Docker CLI in the Terminal to run a
-
To confirm your image was built successfully, you can run the
docker imagescommand, or you can leverage the Docker:Explorer view in VS Code. Navigate to the Docker Explorer by selecting the Docker icon in the left-hand Activity BarThe Docker Explorer comes with the Docker Extension and allows you to interact with your images, containers and registries in a UI-driven fashion
-
Now that you have created a container image for the application, you can leverage that image to create containers. To run a container in VS Code:
- Work with the Docker CLI in the Terminal to run a
docker runcommand targeting thewebapi:1.0image - Right-click the image in Docker Explorer and select
Run Ctrl+Shift+Pand typeDocker: Run
- Work with the Docker CLI in the Terminal to run a
-
Now, you should confirm your container is up and running using one of the following methods:
- Run
docker psin the VS Code Terminal to view all running containers - Check for the container under the
Containerssection in the Docker Explorer - Navigate to http://localhost:80/api/values (You may have mapped a different port on localhost to the container, you can check this in the "PORTS" column after running
docker ps)
Note: The Docker Extension includes "-rm" by default when executing a
docker runcommand. This means if there is an issue with the main process in the container and the process exits, the container will be removed. This makes troubleshooting extremely difficult. If your container did not appear after runningdocker ps, rerun thedocker runcommand without the-rmflag. This will allow you to view the failed container usingdocker ps -a. You can then usedocker logsto view the container logs for troubleshooting - Run
-
Now that you have confirmed your application is up and running in the container, you will want to configure container debugging in VS Code. You can navigate to the Debug window using
Ctrl+Shift+D -
To add the necessary Debug Configuration, select
Add Configurationfrom the Debug drop-down menu and then right-click on the Launch.json tool icon. SelectDocker:Launch .NET Core (Preview). While the auto-generated json should be enough to begin debugging, you may want to override default dockerBuild and dockerRun settings as you dig deeper -
To keep digging deeper with VS Code and Docker, use the following lab




