Getting started with Docker for C# developers

Docker has come a long way since I first came across it in 2015, and now that Microsoft have lot's of support and with the addition of in-built docker tools in Visual Studio, I thought it was time for me to pick up it up.

What is Docker in simple terms?

My interpretation of Docker in a nutshell is:

Docker is a tool that allows you to build, package and deploy your applications.

The statement can be rather vague as there are many tools out there that can achieve the same thing. The difference is that Docker provides a platform where you can package an application such as WordPress, MySQL, Redis, or almost any application (within reason and depending on the application) into whats called an image. You can kind of think it like an iso image, or a zip file as an analogy. You can then transfer this image to another machine, which also is running Docker. Then you can use the host machine to execute or start the image which will in turn start the application, and Docker will run it in whats called a container. That's the gist of it really.

You do not necessarily have to create a Docker image. There are many Docker images that exist and have been packaged already, many which are hosted on Docker hub.

I suppose Docker can be broken down into two areas:

  1. The packaging of an application into a docker image. This is referred to as "dockerizing an application".
  2. Deploying and running the dockerized application, that is, running the docker image in a container.

The easiest way to absorb this is to perhaps start with demonstration.

Install Docker for Windows

Prerequisite:

You will need Windows 10 Pro, Enterprise or Education - As stated in the what you need to know before you install Docker, it requires Microsoft Hyper-V to run. Fortunately, Windows 10 Pro, Enterprise and Education have this enabled. If you have Windows 10 Home, you will unfortunately have to find alternatives.

To check if you have Hyper-V installed, Windows Task Manager will have this:
windows10-task-manager-virtualization-enabled-1

Side note: I have a Macbook Pro (2015 model) running Boot Camp with Windows, and upon installing Docker, I ran into an issue where it was complaining about virtualization. The error message was "Hardware assisted virtualization and data execution protection must be enabled in the BIOS." What the biscuit?

docker_error

A bit of Googling I discovered that you need to boot back into Mac OS, and then restart back into Windows, and tada, it's virtualization is enabled.👍

For any troubleshooting, you can visit the Docker For Windows Github Issues

Download and Install

Download Docker from the Docker Store for free. At the time of writing there are two versions, Docker CE (Community Edition) and Docker EE (Enterprise Edition). Docker EE provides commercial support and certified containers. Docker CE is the free version 🙂 and the one we need.

Once installed, Docker will be running in the background and can be found in the Windows task tray and you should have something like this:

docker-version

Make sure the C: drive is enabled as shown:
docker-shared-drives

Also, in the Advanced menu, you can tailor the desired computing resources:

docker-computing-resources

Dockerize an ASP.NET Core application

I'm using Visual Studio 2017 Community Edition 15.3, but all you need is a Visual Studio version where Docker Tools is installed.

Make sure you are running in Administrator mode.

Create a new ASP.NET Core project

Create a new ASP.NET Core project in Visual Studio, and give it a suitable name.

docker-demo-vs-project

On the next screen, make sure you..

  • Select "Enable Docker support"
  • Select the relevant OS where Docker containers have been set to. I have selected "Linux".
  • Do not select "Configure HTTPS" - it keeps things simple for now.

docker-demo-enable-docker-support-1

Visual Studio will then create two projects:

  • An ASP.NET Core web project, which includes a Docker file.
  • A Docker Compose project which instructs Visual Studio how to start the project in a docker container.

docker-demo-solution-view

Make sure the docker-compose project is marked as the startup project. To do this, just right-click on the project and select "Set as Startup Project".

Start the project by pressing F5 or Debug > Start Debugging.

Side-note
You may get an error such as:
'The DOCKER_REGISTRY variable is not set. Defaulting to a blank string.'

If so, restart Visual Studio in Administrator mode.

Great! You now have a standard sample ASP.NET Core web site running in a docker container.

Note You can run your application in docker mode or standard IIS mode. Simply set the web project as the start-up project in order to run in IIS mode, and set the docker-compose project as the startup project in order to run in docker mode.

How can I tell if Docker is actually running?

You can verify this by launching command prompt or terminal of your choice, and typing:

docker ps

The result will be something like:

CONTAINER ID        IMAGE                      COMMAND               CREATED             STATUS              PORTS                   NAMES
8b49df34dd03        dockeraspnetcoredemo:dev   "tail -f /dev/null"   4 hours ago         Up 4 hours          0.0.0.0:32769->80/tcp   dockercompose8900117491649835616_dockeraspnetcoredemo_1

This command lists running containers and verifies that the ASP.NET application is running in a docker container.

The amazing thing is that breakpoints work as usual in Visual Studio and you can step into your code just like before!

You can checkout the code sample in github:
https://github.com/ch-lee/DockerAspNetCoreDemo

Newsletter

Receive new content like this and more. No spam. Ever. Unsubscribe whenever you want.

or subscribe via RSS with Feedly!