Create an ASP.NET Core API and containerize with Docker
Tags: dotnet, docker, csharp
Published: 2020-07-05 09:05:46

Hey! You feel frustrating to want to learn easily and quickly about _Docker_ and don't reach it? And you think what learn _Docker **with ASP.NET Core**_ is harder? Stop that, no more!

---

This post enums a lot of steps to make a Docker container in Windows contains an own ASP.NET Core application.

# Prerequisites

If you are using Windows like me, you must:
- Install [Docker Desktop for Windows](https://www.docker.com/products/docker-desktop) (login could be required).
Is necessary virtualization features enabled in the BIOS? If you don't enable them don't worry, the Docker setup can this for you.
- Install (or upgrade) [Visual Studio, at least 2019](https://visualstudio.microsoft.com/vs/).

_Why don't install a lower version? Because in this version Microsoft add Docker support and works fine._

# Starting with an existent solution

**_Note:_** _If you haven't an existent solution, you can create any solution in Visual Studio, with ASP.NET Core any project as a unique requirement._

You can try with any solution, but only testing purposes, I use [my example](https://github.com/rruizdev/meli-dotnet) with a WebAPI in _ASP.NET Core_, and select the `Api` project as **StartUp Project**.

After that, you need to make right-click in the same project and select `Add > Docker Support…`

**_Note:_** _You must Docker Desktop running._

## Step by step

- Setting the API project (WebAPI) as the StartUp project.

![Setting the API project (WebAPI) as the StartUp project.](https://dev-to-uploads.s3.amazonaws.com/i/e1y8c95efyc7ma06oj5o.png)

- Select the option "Docker Support" to create a Dockerfile.

![Select the option "Docker Support" to create a Dockerfile.](https://dev-to-uploads.s3.amazonaws.com/i/szdk0xga08ir10dhdpep.png)

- After, you select a target. You will select "Windows" and confirm that dialog.

![After, you select a target. You will select "Windows" and confirm that dialog.](https://dev-to-uploads.s3.amazonaws.com/i/nfqze9pdhsjk0wmxyg4d.png)

**Congrats!** You have a _Dockerfile_ (and `.dockerignore`) into your StartUp project. But… no! It isn't a file ready to dockerize.

## What I need do to dockerize?

First, take care. In the Dockerfile, I see this in the first lines: `EXPOSE 80`. Now, only you can modify to see `EXPOSE 1811` (port specified to launch the current API).

![Expose line.](https://dev-to-uploads.s3.amazonaws.com/i/vh3q4r6kt84u3azo37ix.png)

In the tools bar, you can see a "Docker" button instead of "IIS Express" or simply "Run". You only click in this button, and wait to debug. 

![Docker button.](https://dev-to-uploads.s3.amazonaws.com/i/lkaq6xipphib4woya5g7.png)

When it finished, you can see the WebAPI into your navigator (if you don't configure this yet) at `10000 port`. It works!

![Swagger preview.](https://dev-to-uploads.s3.amazonaws.com/i/ki1aiz472vfntelu7chg.png)

# Troubleshooting

- **Visual Studio error: "Can not find docker container"**
You can delete the hidden `.vs` folder, and retry. If you don't see this folder, please enable the option "Hidden items" into Windows Explorer and now you see that folder.

![Hidden items in Windows Explorer.](https://dev-to-uploads.s3.amazonaws.com/i/toydhodnprofwsquytkx.png)

And, if you suggest an enhancement, please contact me! ;)

_(Versión en español disponible en [Medium](https://medium.com/@rruizdev/how-to-create-a-asp-net-core-application-and-containerize-with-docker-49cf4c017e48))_