Visual Studio For Mac Docker

While I was speaking at Techorama in Belgium I met up with fellow speakers and MVP colleagues in the speaker room while prepping for sessions I had to deliver. We sort of captured a table and that’s where all the SharePoint Office Server and Services MVP congregated; often times it’s a combination of Andrew Connell, Liam Cleary, Vincent Biret, Thomas Votchen, Elio Stryuf Spencer Harbar, Paolo Pialorsi and a few others. When you have that many experts and community folks in one area you are going to learn about technologies that you have always wondered about, but based on your discipline [focus area], you may not have time to explore… one of the topics that resonated with me was Docker. Andrew Connell had a few demos he was doing where he spun up Docker containers for a Web App and Database and something else that I cannot remember now, I just know it was 3 connected containers that he had a YML script to spin up and down as needed. Once I heard his story about how he now uses Docker Containers rather than VM’s whenever possible, and I learned about the portability, efficiencies, and ease of use, I was hooked. For me, I was already thinking about how I could leverage this in my Xamarin sessions and demos; I was thinking that by large a Xamarin.Forms app is really rooted in creating an App that has an experience rooted in a Data Store front ended by a Web API that a standard .NET Mobile project taps into. I was thinking this could be another way to do that and reduce some of my Azure cost when I have to spin up an App Service and SQL Azure instance to support my Xamarin mobile app. After getting the Kool Aid from AC, I started off on some Pluralsight courses by Dan Wahlin , Nigel Poulton, and Kevin Dockx (see below links to the courses) to get ramped up and I took this weekend to master it. While I am not completely there yet… I am 65% complete i.e. to say I have my database container, my Web API (not yet in a container) but communicating with my microsoft/mssql-server-linux Docker container… and all I have to do now is get those container talking to each other and then expose the Web API to my Xamarin.Forms app.

This blog post (well series) will walk you through that experience. This post will serve as the anchor for the series and I will be updating it as I go. I am not sure what the final outcome will be. In the end, I expect to have a Xamarin App consuming from a backend built on Docker Containers, as I now work for K2, I also plan to integrate K2 Smart Form as a Non Mobile Front End that will also communicate with the Docker Container Back End.

This week we released the second alpha of Visual Studio for Mac version 7.2, as well as some hands-on labs to try out some of the new features. The alpha version of our next Visual Studio for Mac release includes support for Docker Containers, Azure Functions, and IoT projects. Visual Studio is a new member of the Visual Studio family, enabling developers on macOS to build apps for mobile, Web, and cloud with Xamarin and.NET Core, as well as games with Unity. Use Visual Studio to develop apps for Android, macOS, iOS, tvOS, watchOS, Web, and cloud.

Strap in…

Lets create a legend so you can follow where I am at any point in time. subject to modification as needed

BOLD – Items Done
Italics – Work in Progress (WIP)
Grey Subdued Font – Not Started

The Plan of Action in my mind so far is to:

  1. Install Docker for Mac
  2. Get base Docker Images to host the Web API and Database
    1. microsoft/aspnetcore
    2. microsoft/mssql-server-linux
  3. Create Docker Containers for the Xamarin Backend Solution
    1. ASP.NET Core for the Web API with Entityframework Core to the MSSQL Linux Database
    2. Provision the Database and Tables using Code First EF Core in my MSSQL Linux Database
  4. Modify those base images to include
    1. the Code for my ASP.NET Core Web API
    2. the database and tables for the SQL Database that the Web API will be pulling from
  5. Create a Docker Network and include both of my Modified Docker Containers –
  6. Host and Expose the Web API Container URI in my Azure Tennant or maybe even somewhere else.. that’s the idea right, I can do this anywhere with containers
  7. Create my Xamarin Project that consumes the Web API that is hosted in Docker Containers & Publish my iOS and Android App
  8. Integrating with K2 Smart Forms and Brokers directly into Containers or as REST Broker

Blog Post are not only to help inform others, but also serve as a reminder of how you [the author ie. me] did things, found resources, so I am building a library here of links I have used to get me where I am. If you know of more please leave me comments. Thank you

  1. The new way to do Docker on a Mac does not use Docker Toolbox, it uses Hyperkit and that means you need to download Virtual Box yourself as it is no longer bolted on and Create your Docker Machine Host (linux for me)
  2. Create a Docker Hub Account – optional but makes it easy for you to find Images you can use in your Docker World in order to allow you to create your Containers. My Docker Profile and my stuff are here
  3. I am tracking as much as I can from a Source Code perspective here on my Git Hub Repo
  4. Learning Aids
  5. Run the SQL Server 2017 container image on Docker on Linux, Mac, or Windows – We use this to get the Image needed for the MSSQL-Server-Linux Docker Image that the Web API will call into
  6. Learn how to communicate with SQL Server on Linux: Connect to SQL Server on Linux and run Transact-SQL queries
  7. If you want to work with the SQL Server on Linux and you are on a Mac, I found the SQL CLI easy to use. So I downloaded it and began using it to inspect what EntityFramework Core was doing behind the scene as I did this Code First and using Visual Studio for Mac which doesn’t have the SQL Toolbox in the IDE.
  8. How to build SQL Connection Strings for the Entity Framework Core
  9. GOTCHA: If you are using ASP.NET Core from Visual Studio for Mac and trying to Connect to a SQL Server in a VM (as I am doing with my Docker Container) you may get an error when the connection is being made that is tied to Open SSL. Here is the fix
  10. Documentation on working with SQL Cli in more detail
  11. A Github Gist I created for my Docker Commands – WIP
  12. Sessions I will be doing to showcase this
  • Volume 1: Getting Docker Squared away in my Development Environment
  • Volume 2: Getting MSSQL Server for Linux Squared Away
  • Volume 3: Creating my ASP.NET Core Web API with EntityFramework Core into a MSSQL Linux Docker Container – Consumer Path
  • Volume 4: Building my ASP.NET Core Web API with EntityFramework Core into a MSSQL Linux Docker Container – Developer Path
  • Volume 5: Hosting your Docker Solution in a Cloud Provider (in my case Microsoft Azure)
  • Volume 6: Creating a Xamarin.Forms App consuming from a Docker Container Backend (Ive done a series on Xamarin.Forms using a Web.API for a mobile app I build for a brewery, its the same concept just a different Web.API)

Thank you for taking the time to read through this blog series. The Github Repo where the code is located is here. The Readme.MD file will walk you through a few ways of how to deploy this solution to your environment. It is my hope after you get through the series that you will be convinced that Docker Containers makes sense over:

  1. Creating VMs in your own Local Dev Rig
  2. Creating VMs in your Cloud Hosting Provider (Azure, AWS, etc)
Visual studio for mac docker support

Learning and Developing solutions is cut down dramatically by using Containers and its is a portable solution as well with scaling built in. If you have any questions or comments, please reach out. Cheers.

Docker is a very popular container platform that lets you easily package, deploy, and consume applications and services. Whether you are a seasoned Docker developer or just getting started, Visual Studio Code makes it easy to author Dockerfile and docker-compose.yml files in your workspace. Visual Studio Code even supports generating and adding the appropriate Docker files based on your project type.

Install the Docker extension

Docker support for VS Code is provided by an extension. To install the Docker extension, open the Extensions view by pressing ⇧⌘X (Windows, Linux Ctrl+Shift+X) and search for docker to filter the results. Select the Microsoft Docker extension.

Dockerfiles

Docker

With Docker, you can build images by specifying the step by step commands needed to build the image in a Dockerfile. A Dockerfile is just a text file that contains the build instructions.

VS Code understands the structure of Dockerfiles as well as the available set of instructions, so you get a great experience when authoring these files.

  1. Create a new file in your workspace named Dockerfile
  2. Press ⌃Space (Windows, Linux Ctrl+Space) to bring up a list of snippets corresponding to valid Dockerfile commands. Pressing the 'i' Read More button on the right will show a fly-out with details and a link to the Docker Online documentation.
  1. Press Tab to move between fields within the snippet. For example, with the COPY snippet you can fill in the source and then press Tab to move to the dest field.

In addition to snippets for authoring your Dockerfile, Visual Studio Code will provide you with a description of any Docker command you hover over with the mouse. For example, when hovering over WORKDIR you will see the following.

For more information on Dockerfiles, check out Dockerfile best practices on docker.com.

Docker Compose

Docker Compose lets you define and run multi-container applications with Docker. You define what the shape of these containers look like with a file called docker-compose.yml.

Visual Studio Code's experience for authoring docker-compose.yml is also very rich, providing IntelliSense for valid Docker compose directives and it will query Docker Hub for metadata on public Docker images.

  1. Create a new file in your workspace called docker-compose.yml
  2. Define a new service called web:
  3. On the second line, bring up IntelliSense by pressing ⌃Space (Windows, Linux Ctrl+Space) to see a list of all valid compose directives.
  1. For the image directive, you can press ⌃Space (Windows, Linux Ctrl+Space) again and VS Code will query the Docker Hub index for public images.

VS Code will first show a list of popular images along with metadata such as the number of stars and description. If you continue typing VS Code will query the Docker Hub index for matching images, including searching public profiles. For example, searching for Microsoft will show you all the public Microsoft images.

Generating Docker files

Writing Docker and docker-compose files by hand can be tricky and time consuming. To help you, VS Code can generate the necessary Docker files for your project. From the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), run the Docker: Add Docker files to Workspace command to generate Dockerfile, docker-compose.yml, and docker-compose.debug.yml files for your workspace type:

Docker commands

Many of the most common Docker and docker-compose commands are built right into the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

Docker view

The Docker extension contributes a Docker view to VS Code. Within the view, the Explorer lets you view and manage your Images, Containers, and browse your Docker Hub registry. If the Azure Account extension is installed, you can browse your Azure Container Registries as well.

The right click context menu provides quick access to the same rich set of commands found in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

You can move the Docker view up or down by dragging the Docker icon and you can hide the view by right clicking on the icon and choosing Hide. To bring it back, right click on the Activity Bar area and check the Docker item.

Logging into Docker Hub

The first time you expand the Docker Hub node, you'll be prompted to log into your Docker Hub account.

Your user name and password are stored in your operating system credentials vault (for example macOS keychain, Windows Credential Store) so that you don't need to log in every time. You can log out of Docker Hub by right clicking on the Docker Hub label and choosing Docker Hub Logout. This will delete the credentials from the OS store.

Mac

Configuration settings

The Docker extension comes with a number of useful configuration settings allowing you to customize your workflow. You can control such things the default tag for an image (docker.defaultRegistry), the shell to use when attaching to a Docker container (docker.attachShellCommand), or various docker-compose settings.

Bring up the Settings editor (⌘, (Windows, Linux Ctrl+,)) and type 'docker' in the search box to filter on the docker extension settings.

Linting

The Docker extension language service analyzes your Docker files and provides you with errors and warnings.

You can control specific linting rules to either be a warning, error, or ignored through the docker.languageserver.diagnosticssettings

Deploying images to Azure App Service

Visual Studio For Mac Docker Tools

With the Docker Explorer, you can deploy images from Docker Hub or Azure Container Registries, directly to an Azure App Service instance. This functionality requires installing the Azure Account extension and an Azure Subscription. If you do not have an Azure subscription, sign up today for a free 30 day account and get $200 in Azure Credits to try out any combination of Azure services.

To log into Azure, use the Azure: Sign In command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). You will then sign into your account using the Device Login flow. Click on Copy & Open to open your default browser.

Paste in the access code and continue the sign in process.

You can now right click on an image in Docker Hub or an Azure Container Registry and choose Deploy Image to Azure App Service.

From here, you will be prompted for an Azure Resource Group, a location, an App Service Plan, and a globally unique website name.

To see the full workflow, there is a detailed Deploy using Docker tutorial that creates a web application, puts it into a Docker container, pushes it to an Azure Container Registry, and then deploys to Azure.

Connecting to docker-machine

Visual Studio 2017 Mac Docker

The default connection of the extension is to connect to the local docker daemon. You can connect to a docker-machine instance if you launch VS Code and have the DOCKER_HOST environment variable set to a valid host.

Running commands on Linux

By default, Docker runs as the root user on Linux, requiring other users to access it with sudo. This extension does not assume root access, so you will need to create a Unix group called 'docker' and add users to it. Instructions can be found here: Create a Docker group.

Visual Studio For Mac Docker Extension

Next steps

Microsoft Visual Studio For Mac

  • Azure Extensions - The VS Code Marketplace has hundreds of extensions for Azure and the cloud.
  • Deploying to Azure - Learn step-by-step how to deploy your application to Azure.
  • Working with MongoDB - Create, manage and query MongoDB databases from within VS Code.