-->
- Visual Studio For Mac Add Docker Support
- Docker Tools For Visual Studio
- Visual Studio For Mac Os X
- Visual Studio For Mac Docker Tutorial
- Visual Studio For Mac Docker Compose
- Visual Studio For Mac Docker Debugging
Docker for Mac. For macOS only: If you are using Docker for macOS, this is the recommended connection option. For Linux only: This is the recommended connection option for Linux. Docker Machine. If you are using Docker Toolbox for Windows or macOS, this is the recommended option for connecting to Docker API. I wanted to test out the docker support for Visual Studio for Mac. I've installed the Alpha channel version and found the 'Add Docker Support' button. Docker users can use a single CLI to build apps for both Windows or Linux. The Docker client and Kitematic GUI are now supported for Windows desktop environments. Meanwhile the Microsoft developer can use their favorite IDE like Visual Studio to build and ship apps built in Docker.
This tutorial shows how to run an ASP.NET Core app in Docker containers.
In this tutorial, you:
- Learn about Microsoft .NET Core Docker images
- Download an ASP.NET Core sample app
- Run the sample app locally
- Run the sample app in Linux containers
- Run the sample app in Windows containers
- Build and deploy manually
ASP.NET Core Docker images
For this tutorial, you download an ASP.NET Core sample app and run it in Docker containers. The sample works with both Linux and Windows containers.
The sample Dockerfile uses the Docker multi-stage build feature to build and run in different containers. The build and run containers are created from images that are provided in Docker Hub by Microsoft:
dotnet/core/sdkThe sample uses this image for building the app. The image contains the .NET Core SDK, which includes the Command Line Tools (CLI). The image is optimized for local development, debugging, and unit testing. The tools installed for development and compilation make this a relatively large image.
dotnet/core/aspnetThe sample uses this image for running the app. The image contains the ASP.NET Core runtime and libraries and is optimized for running apps in production. Designed for speed of deployment and app startup, the image is relatively small, so network performance from Docker Registry to Docker host is optimized. Only the binaries and content needed to run an app are copied to the container. The contents are ready to run, enabling the fastest time from
Docker runto app startup. Dynamic code compilation isn't needed in the Docker model.
Prerequisites
Docker client 18.03 or later
- Linux distributions
Download the sample app
Download the sample by cloning the .NET Core Docker repository:
Run the app locally
Navigate to the project folder at dotnet-docker/samples/aspnetapp/aspnetapp.
Run the following command to build and run the app locally:
Go to
http://localhost:5000in a browser to test the app.Press Ctrl+C at the command prompt to stop the app.
Run in a Linux container
In the Docker client, switch to Linux containers.
Navigate to the Dockerfile folder at dotnet-docker/samples/aspnetapp.
Run the following commands to build and run the sample in Docker:
The
buildcommand arguments:- Name the image aspnetapp.
- Look for the Dockerfile in the current folder (the period at the end).
The run command arguments:
- Allocate a pseudo-TTY and keep it open even if not attached. (Same effect as
--interactive --tty.) - Automatically remove the container when it exits.
- Map port 5000 on the local machine to port 80 in the container.
- Name the container aspnetcore_sample.
- Specify the aspnetapp image.
Go to
http://localhost:5000in a browser to test the app.
Run in a Windows container
- In the Docker client, switch to Windows containers.
Navigate to the docker file folder at dotnet-docker/samples/aspnetapp.
Run the following commands to build and run the sample in Docker:
For Windows containers, you need the IP address of the container (browsing to
http://localhost:5000won't work):Open up another command prompt.
Run
docker psto see the running containers. Verify that the 'aspnetcore_sample' container is there.Run
docker exec aspnetcore_sample ipconfigto display the IP address of the container. The output from the command looks like this example:
Copy the container IPv4 address (for example, 172.29.245.43) and paste into the browser address bar to test the app.
Build and deploy manually

In some scenarios, you might want to deploy an app to a container by copying to it the application files that are needed at run time. This section shows how to deploy manually.
Navigate to the project folder at dotnet-docker/samples/aspnetapp/aspnetapp.
Run the dotnet publish command:
The command arguments:
- Build the application in release mode (the default is debug mode).
- Create the files in the published folder.
Run the application.
Windows:
Linux:
Browse to
http://localhost:5000to see the home page.
To use the manually published application within a Docker container, create a new Dockerfile and use the docker build . command to build the container.
The Dockerfile
Here's the Dockerfile used by the docker build command you ran earlier. It uses dotnet publish the same way you did in this section to build and deploy.

Additional resources
- ASP.NET Core Docker sample (The one used in this tutorial.)
Next steps
Visual Studio For Mac Add Docker Support
In this tutorial, you:
Docker Tools For Visual Studio
- Learned about Microsoft .NET Core Docker images
- Downloaded an ASP.NET Core sample app
- Run the sample app locally
- Run the sample app in Linux containers
- Run the sample with in Windows containers
- Built and deployed manually
The Git repository that contains the sample app also includes documentation. For an overview of the resources available in the repository, see the README file. In particular, learn how to implement HTTPS:
Overview
Microsoft offers great support for building apps deployed to Docker containers in Visual Studio for Mac, that can be easily deployed and managed in Azure. With the power of the cloud behind your solution, you can easily compose powerful constellations of these containers known as 'microservices' that are independently managed and scaled. In this hands-on lab you will create a microservices solution that employs two ASP.NET Core apps hosted in separate Docker containers.
Objectives
- Create an ASP.NET Core web site
- Deploy the site in a Docker container
- Create and integrate an ASP.NET Core web API project in a Docker container
Prerequisites
- Visual Studio for Mac (https://www.visualstudio.com/vs/visual-studio-mac)
- Note that this lab currently requires switching to the Alpha channel in Visual Studio for Mac, as Docker support is available as a preview
- Docker (https://go.microsoft.com/fwlink/?linkid=847268)

Intended Audience
This lab is intended for developers who are familiar with C# and ASP.NET, although deep experience is not required.
Exercise 1: Getting started integrating with Docker containers in Visual Studio for Mac
Task 1: Creating an ASP.NET Core web site and enabling it for Docker
Launch Visual Studio for Mac.
Select File > New Solution.
Select the .NET Core > App category and the ASP.NET Core Web App template. Click Next.
If presented with a Target Framework selection screen for .NET Core, select the target framework you want to use, such as .NET Core 1.1. Click Next.
Enter a Project Name of 'DockerLab' and click Create.
The created project contains all the basics you need to build and run an ASP.NET Core web site.
In Solution Explorer, right-click the DockerLab project node and select Add > Add Docker Support. This will add a new Docker-specific project to the solution (docker-compose), along with a Docker configuration file to the project.
Open the newly added Dockerfile from the DockerLab project.
Dockerfile describes the application, including the base container, the port number to expose the application on, the entry point of the application, and more. You can learn more about this format here.
From the docker-compose project, open docker-compose.yml.
docker-compose.yml describes how the application should be composed of the required containers to set up a given solution. Right now, there's just one service for the DockerLab project created earlier. More will be added here as additional container apps become part of the solution.
Note that the docker-compose project has also become the default startup project. This makes it easy to launch multiple projects as part of the same debugging session.
Select Run > Start Debugging or press F5 to build, deploy, and run the project in a Docker container. This may take a minute or so to complete.
Once the application has launched in Safari, note that the URL is to the specific localhost port the container is listening on (it may vary from the screenshot below).
Open a new instance of Terminal.
Execute the command below to get a list of all Docker containers running.
Unless you have other containers running, there should be only one. Note that the data wraps into three lines in the screenshot below. A key point of interest is the port relay that indicates that the container is listening on a given port (which should be the same as used by the browser above) and using port 80 to reach its internal web server. As far as the app knows, it's listening on port 80.
Visual Studio For Mac Os X
Task 2: Creating an ASP.NET Core Web API and enabling it for Docker
Return to Visual Studio for Mac and stop debugging.
In Solution Explorer, right-click the DockerLab solution node (the top-most node) and select Add > Add New Project.
Select the .NET Core > App category and the ASP.NET Core Web API project template. Rather than including some basic web application files to render HTML, this template includes a controller designed to handle RESTful requests. Click Next.
If presented with a Target Framework selection screen for .NET Core, select the target framework you want to use, such as .NET Core 1.1. Click Next.
Enter a project name of 'api' and click Create.
The project structure of the API project is similar to the web site project, except that it has fewer files since it doesn't need views or some of the client-side web components. It still uses MVC, so the Controllers folder is where the magic happens.
Right-click the api project node and select Add > Add Docker Support. This will run the same process as before, but will now merge new settings for this project alongside the existing settings for the DockerLab project.
Close and reopen docker-compose.yml to refresh the changes.
Now you will see that the second API project has been added alongside the web application. When built and run, they will be deployed to separate Docker containers and able to access each other as configured.
Task 3: Integrating two container apps
From the api project, open Controllers/ValuesController.cs. This is the default controller that contains a configured API you can edit.
Replace the first Get method with the code below. This is just a minor change to make the API easier to consume for our lab purposes. Do not remove the [HttpGet] attribute.
Your inserted code should look like this.
Set a breakpoint on the return line of code. You can do this by clicking in the margin or by setting the cursor somewhere on the line and pressing F9.
From the DockerLab project, open Controllers/HomeController.cs. This controller manages the content rendered for the default pages in the project.
In the About method, replace the first line referencing ViewData with the code below. This code integrates with the API you just edited in the API project and displays the result. This is a practical approach for the purposes of this lab and not a proper practice for more robust applications.
Set a breakpoint on the return line of this method.
Task 4: Debugging multi-container solutions
Visual Studio For Mac Docker Tutorial
Press F5 to build and run the project. When the browser window loads, two Docker containers will be running.
In Terminal, execute the command below to view the containers.
The two containers with their details will be displayed. Note that the data wraps to three lines each.
In the browser, navigate to the About page.
Visual Studio for Mac will hit two breakpoints during this request. Press F10 to step through each execution as the API request is returned to the web app (across Docker containers!) and rendered. You will eventually see the result of 'API' that was returned from the API displayed from the web app.
Refresh the page in the browser. This will hit the first breakpoint once again.
This time, open the Locals pad and locate the message variable. It should have the value of 'API' since that was the most recently executed line of code.
Double-click the value and change it to 'changed' (with quotes). Press Enter to apply.
Press F5 to continue execution.
The next stop will be in the container hosting the web app, where you will see the returned value. This time it should reflect 'changed'. Press F5 to continue execution once again.
The new API value should render out as HTML in the browser. This multi-container microservices solution is now ready to be extended and deployed.
Visual Studio For Mac Docker Compose
Summary
Visual Studio For Mac Docker Debugging
In this lab, you've learned how to get started integrating with Docker containers with Visual Studio for Mac.