DEV Community

Cover image for Create an ASP.NET Core API and containerize with Docker
Roberto Ruiz
Roberto Ruiz

Posted on • Updated on

Create an ASP.NET Core API and containerize with Docker

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:

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 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.

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

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

  • 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.

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.

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.

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.

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.

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

(Versión en español disponible en Medium)

Top comments (3)

Collapse
 
juniordevforlife profile image
Jason F

Thanks for this succinct yet informative article Roberto! Going to give this a shot when I have some down time.

Collapse
 
remusgarcia profile image
Remus Garcia

Docker + Windows? It is not something I would choose.

Collapse
 
rruizdev profile image
Roberto Ruiz

And It's fine!
However, you can choose any OS or distribution to work with Docker.
But, if you need work with Docker and Windows 10, you have here a quick tutorial to make it possible 😉.