Hello World .NET Core and Docker with VS

Today I want to share with you how you can create a Hello World API with .NET Core but running inside of a Docker container and not just that, we will be able to even debug the API with Visual Studio (VS) and then I'll explain how easy is to push an image that is ready for a production environment, so let's get started.

What I need to have first?

In order for this demo to work you need to have the following installed in your computer (I'm using Windows 10):

  1. Visuatl Studio 2017 RC, you can download it here
  2. Docker for Windows, you can download it here
  3. If you're using more than one drive in your computer and you installed docker in a different drive than your project files, you need to share that drive in Docker, you can do it following this guide here. Read this part again, you might not be able to debug the API in VS if you don't do this.

Once you have everything installed/configured, you can continue.

Create a Hello World API

We're not just going to create a simple API (you might know how to do this already) but I'm going to focus on how you can integrate Docker to your project.

  1. Open your VS as an Administrator (right click and "Run as Administrator")
  2. Go to File -> New -> Project
  3. Choose Templates -> Visual C# -> .NET Core and then "ASP.NET Core Web Application (.NET Core)"
  4. Put a name to the project (in my case "HelloDocker"), choose the location of files and then click "OK"

Create Web API in .NET Core

After this, you should be seeing another screen where you need to enable docker support (you can do it later too).

  1. Choose "Web API" under "ASP.NET Core Templates 1.1"
  2. Check the "Enable Docker Support" box and click "OK"

Enable Docker Support in VS

Let's wait a little bit and you should be seeing something like this:

Project Structure with Docker enabled

What's new here? If you notice you now have a new section in the solution called "docker-compose" and inside your project you have a "Dockerfile". I'm not going to explain why you have those files and what they do (for more details go here). Now, let's see the magic when debugging the API.

  1. Open the ValuesController.cs file (In the web project, Controllers/ValuesController.cs)
  2. Put a break point in the line where return a list of values (line #16)
  3. In the solution explorer, go to "docker-compose", right click and then Debug -> Start new instance. If you do it in the project, it will launch the IIS Express and you don't want to do that right now. You should start seeing in the output how the image is being built by copying all the binaries inside the container.
  4. When it finishes, it will launch a new window in your browser
  5. In the same window, go to the /api/values URL and VS will catch that call in the break point we put

Debug your Web API inside Docker

Now, this where you can start screaming excited about what just happened (I did). If you don't get it, let me explain what just happened: VS built a container with the code you have in the project, then it launched the container and attached it to your VS so you can debug it. This means that you're not just testing locally but you're testing in the same way that the API will run in the production server. That's really awesome, I loved it.

Close your mouth now please, we need to do some things to finish the demo :) ... In the same line that you put the breakpoint, let's add a new value, we're gonna push this to the Docker Hub.

What about deployment to Production server?

I know, that was my first question, so let's open the terminal window (I use Powershell) and let's do it, but first, we need to build the API in release mode.

  1. In VS, change the configuration from Debug to Release mode and build the solution again. Why? It will start creating the image for a production environment (you never release your APIs in Debug mode, right?) and it chooses the best container base image that Microsoft team have configured for those environments, so it's not from the same image base we used to debug our code. More info here
  2. Open your terminal window and run "docker images"

Docker Images

Now, you see that I remarked the one with the "latest" tag, that's the one we need to use for production, the other one (with the tag "dev") it's the one just for development. Ok, let's push it to your docker registry.

  1. Login to your Docker Hub account here (If you don't have one, create it please)
  2. Now, let's create a public repository, mine is called "hellodocker"
  3. Go back to your terminal and run the "docker images" command
  4. Copy the IMAGE ID of the hellodocker with the latest tag and tag it with the name of your repository that you just created. In my case I need to run this "docker tag 95904eeb5775 christianhxc/hellodocker"
  5. List the images and you should see the one you just created
  6. Login to your docker repository by running "docker login"
  7. Push the image, in my case I ran "docker push christianhxc/hellodocker", wait a little bit and you should see the image in Docker Hub (it might take some time to see it)

Docker Images

Docker Images

How to run the container without VS?

Just open your terminal and run the following command:

docker run -d -p 8000:80 christianhxc/hellodocker

Open your browser in http://localhost:8000/api/values and see the latest version of the API

So that's all, now you can easily create .NET Core apps with Docker support :)

Would you like to be notified of any new post?

Subscribe to my mailing list by filling the following form and I'll be sending you an email when I publish a new entry

* indicates required