Create ASP.NET Core Application with Docker Interactively


In this article, we will create the dotnet core application interactively by the running the following command.

$docker run -p 8000:80 -e "ASPNETCORE_URLS=http://+:80" -it --rm microsoft/dotnet

This command will first pull the latest dotnet image from Docker registry and run the container. The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container.

You will see the images will be downloaded and output as shown below:

Unable to find image 'microsoft/dotnet:latest' locally
latest: Pulling from microsoft/dotnet
06b22ddb1913: Already exists 
336c28b408ed: Downloading [=============>                                     ]  3.109MB/11.11MB
1f3e6b8d80c3: Downloading [==================>                                ]  1.621MB/4.411MB
5ccc640979f6: Downloading [==>                                                ]  2.539MB/50MB
b51d754091ec: Waiting 
64a24a32acd8: Waiting 
31abe630980c: Waiting 

After downloading the images into the host, the console will log into the container stdin

root@00f779ffa10a:/# 

Now we will create an AspNetCore application using the dotnet commands.

$ mkdir aspnetcore-sample
$ cd aspnetcore-sample
$ dotnet new webapi

The above commands will create a new webapi application.  Now we run the application in our container

$ dotnet restore
$ dotnet run

Interactive Image

Now we can see the webapi running and you can the output at http://localhost:8000/api/values 

Output

To exit from the interactive shell of the container

$ exit

One thing to keep in mind, Docker containers are ephemeral so data created in the container is not persisted and will be destroyed when the container is stopped. You can see in the shell when you created the aspnetcore application it says

Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. 
      Protected data will be unavailable when container is destroyed.

non-persistent data

 

Leave a comment