Toggle Menu

Insights / Tech Tips / Getting Started with Docker and .NET Core on OS X

September 12, 2016

Getting Started with Docker and .NET Core on OS X

8 mins read

In trying out .NET Core, I ran into some versioning and reference issues as it was being built, so I thought it was a good opportunity to try it with Docker to see how it worked. Spinning up a container from a Docker image that has .NET Core installed will avoid versioning, dependency and other conflicts since it’s a fresh, isolated container. In this article, we’ll get a taste of the new .NET Core development process, as well as the development process with Docker. We’re going to run through spinning up a .NET Core container, which takes very little time, and then we’ll spin up a container ready for building ASP.NET Core applications.

The basic idea if you’re new to Docker is, you create containers from images. Images are like templates for these containers. Containers are like isolated virtual directories which share the host machine’s kernel, so they can be quite efficient with resources when compared with VMs, but are still mostly isolated. They can hold all the components needed to run and include your app. They can be deployed in great number in a cloud environment.

Layering images is an important concept. An example would be having a base image based on a particular Linux distribution, with an image containing ASP.NET Core and Kestrel web server on top of that, and an image with your web app on top of that. Then you can create containers from that top-layer image and it will have your app running with the needed components. You can then reuse the ASP.NET Core/Kestrel image for creating ASP.NET Core apps going forward.

We can use images that Microsoft provides that have .NET Core installed. Or we could build our own from a .NET environment we set up. In this article, we’re going to start with a pre-built image from Microsoft that has .NET Core installed. Building images is a another topic that you should explore next!

In minutes you can start a container using an ASP.NET Core image from Docker Hub, and start building and running a .NET app in the container, while you do your work on your host computer as usual, without having to install the platform because it was done for you in the image.

Building a .NET Core app feels closer to the node development pipeline. It’s extremely satisfying to create an entire .NET environment, create a .NET application, and start a web server to run it mostly from the command line. I used OS X for this operation, though it is similar on Linux and Windows. I thought this would also be the perfect opportunity to see how .NET Core behaves on non-Windows environments.

Let’s get started.

Step 1: Learn the Basics of Docker and Install It

Installing Docker
Here are Docker’s instructions for installing Docker on OS X and running a hello world container. This takes a few minutes if nothing goes wrong. I’ve had very smooth experiences with this, at least with the current version.

Learn the basics
Here are some basic tenets of Docker you’ll run into when starting out and most commonly. Keep an eye out for these things – you should encounter some in getting a .NET Core container running, and they will start to make sense. You can use this Docker User Guide as a reference. Docker’s docs are excellent, so when you’re ready to dive deeper into learning it, Docker’s Getting Started Guide is a great place to start. For now, here are some quick tidbits:

  • Images: what containers are made from
  • Layers: images can be layered
  • Containers: created from images, share a kernel, but are isolated environments for applications to be built and run; where the action happens
  • Dockerfile: the file that specifies how to create an image
  • Common operations with Docker
    • Listing images: i.e. docker images
    • Listing containers: docker ps –a [without –a you only see running containers]
    • Starting, stopping containers: i.e. docker run… [stop]
      • -d will run in detached mode (in the background; docker exec -ti <container name> sh will log in)
      • -v will mount host directory
      • -p will expose ports. Necessary if you want container to listen to the outside world.
      • -t is for specifying which image to run
    • Creating images: i.e. docker build –t <tag:imageName> <directory of Dockerfile>
    • Deleting all containers or images: docker rm $(docker ps -a -q) [rmi for images, stop for stopping all]
    • Deleting all untagged images: docker rmi -f $(docker images -q -a -f dangling=true)
    • Delete images for specific tag: docker rmi $(docker images | grep myTag)
  • Layer optimization: you want to keep them as thin as possible, so when they’re layered together to create a container, it is optimized

Step 2: Start a Docker Container with .NET Core from a Microsoft Image & Create an App

Follow these simple steps to start a .NET Core container from an image that Microsoft has set up with .NET Core layered on a Debian Linux base image. You’ll have already done step 1, and the rest only take a few minutes. That also includes steps for the simplest way to create a .NET Core app.

This process of running a container looks for the specified image which it won’t find, so it will look on Docker Hub and find it there. It will then download it, and immediately create a container from it. This is a quick way to get a container running that has .NET Core installed.

The dotnet command is .NET Core’s new command-line interface (CLI) where you can do a lot of your work, such as building an app (init, restore, build, run, deploy, pack, and it’s extendible).

Step 3: Manage Docker

While you have a running container, grab the opportunity to learn the basics around Docker image and container management. Try deleting the container and Microsoft’s image that was downloaded automatically in step 2. Trying deleting the image with the container there and see what happens. For example:

Show you all containers (without -a you’ll only see the running ones):

[pcsh lang=”csharp” tab_size=”4″ message=”” hl_lines=”” provider=”manual”]

docker ps -a

[/pcsh]

Show you your images. You should see the microsoft/dotnet image that was downloaded earlier:

[pcsh lang=”csharp” tab_size=”4″ message=”” hl_lines=”” provider=”manual”]

docker images

[/pcsh]

A clever way to remove all containers at once is to use this. You can do similar things with other commands:

[pcsh lang=”csharp” tab_size=”4″ message=”” hl_lines=”” provider=”manual”]

`docker rm $(docker ps –a –q)’

[/pcsh]

The Docker User Guide has quick access to deleting and other common operations.

Step 4: Create a Docker Image & Container with ASP.NET Core Installed

To develop an ASP.NET web app in a Docker, we’re going to create a container from an image that has .NET Core installed, with a port exposed for web traffic in and out of the container. We will also mount an app directory into the container. This way, the container can use .NET Core to build and run the app, while you work on it on your host computer.

You could build a new Docker image that did the needed steps just mentioned, using Microsoft’s .NET Core base image that you used in step 2, but that is beyond the scope of this article. I have done that already in this image. You can go to the GitHub repo associated to the image if you would like to try building an image from it yourself – there are instructions there.

So, we’ll use my image to start an ASP.NET development environment with Docker doing the building and running.

  1. Go to a command line
  2. Create a new directory and go into it
  3. Enter the following command:

[pcsh lang=”csharp” tab_size=”4″ message=”” hl_lines=”” provider=”manual”]

docker run -i -t -p 5000:5000 -v $(pwd):/app -w "/app" wyntuition/aspnetcore-development-env

[/pcsh]

This will first download that image, since you won’t have it locally yet. Then, it will start a container from it, exposing port 5000 (which is the default port an ASP.NET Core app exposes), and it will map the current directory you’re in to the directory /app in the container.

Since we used the –i flag, we’ll be in the container’s bash.

  1. In the bash, enter the follow, which will create a new ASP.NET Core app from a template,

[pcsh lang=”csharp” tab_size=”4″ message=”” hl_lines=”” provider=”manual”]

dotnet new –t Web

[/pcsh]

  1. Then run this, which will run the application, exposing ‘Hello World!’ on https://localhost:5000:

[pcsh lang=”csharp” tab_size=”4″ message=”” hl_lines=”” provider=”manual”]

dotnet run

[/pcsh]

You should be able to browse to your app now. You can get dotnet-watch which will rebuild and re-run the app whenever you save code changes.

Well, you’re well on your way to understanding Docker and how it can help in development situations. You can check out Docker Hub to find prebuilt containers, or share images with your colleagues or peers.

Check out this great article on understanding Docker architecture for an explanation of its guts.  Some other next steps could be to publish the container to Azure, or learn how to use Docker for development like their engineers. Check out this article for building, deploying and testing an ASP.NET Core app in Docker.

For more next steps, check out Docker’s Learn By Example, including,

You Might Also Like

Resources

Simplifying Tech Complexities and Cultivating Tech Talent with Dustin Gaspard

Technical Program Manager, Dustin Gaspard, join host Javier Guerra, of The TechHuman Experience to discuss the transformative...

Resources

How Federal Agencies Can Deliver Better Digital Experiences Using UX and Human-Centered Design

Excella UX/UI Xpert, Thelma Van, join host John Gilroy of Federal Tech Podcast to discuss...