Windows Docker: Running ASP.NET Core MVC Application inside Container

In this wiki, we will see how to run an ASP.NET Core MVC Application inside a Docker Linux Container from Windows. The following assumptions being considered, you have some basic knowledge of Docker, Docker Images/Containers. Some of the commands, might not be explained what each argument of the command does, hoping you will do --help on those.

So let’s start.

So, on this machine, we have Docker for Windows installed, and under settings, we have local C:\ drive shared with Containers.

https://lh3.googleusercontent.com/-5zwZKbbgMdc/WR3khJSb5VI/AAAAAAAAEas/tMfTJaPmmws54BjgrSaDxOqHXxRqhWpyQCHM/image_thumb25?imgmax=800 Shared Drives

Now let’s create a folder (we are creating on the Desktop, make sure the drive you are creating the folder in, is shared like above) and we are naming it as a DockerWebApp.

Open up a PowerShell window and change the directory to DockerWebApp folder.

PS C:\Users\Jaliya\Desktop\DockerWebApp> dotnet new mvc
PS C:\Users\Jaliya\Desktop\DockerWebApp> dotnet restore
PS C:\Users\Jaliya\Desktop\DockerWebApp> code .

Now the project will get opened in Visual Studio Code. We have Docker extension for VS Code installed. If you haven’t installed it, suggest you do so, so we can create Docker-related files pretty easily. So using the extension, Dockerfile is created as follows.

https://lh3.googleusercontent.com/-u3SnKe1gMGg/WR3kzkdlSsI/AAAAAAAAEa0/iqoMledSZLMwosu1-gckIq5JKaYzCqEyQCHM/Dockerfile_thumb2?imgmax=800 Docker for VS Code - Adding Dockerfile

So the Dockerfile looks like this.

FROM microsoft/aspnetcore:1.0.1
LABEL Name=dockerwebapp Version=0.0.1
ARG source=.
WORKDIR /app
EXPOSE 30000
COPY $source .
ENTRYPOINT dotnet dockerwebapp.dll

For now, we are not going to do any changes here, but we are going to delete the other Docker files which got created inside the project which are docker-compose.debug.yml and docker-compose.yml to make things not confusing.

Now let’s publish our application.

PS C:\Users\Jaliya\Desktop\DockerWebApp> dotnet publish -c Release

And since we haven’t set any specific output path, the application will get published to bin\Release\netcoreapp1.1\publish folder. https://lh3.googleusercontent.com/-Fj7RS7BL7-4/WR3k2alwlKI/AAAAAAAAEa8/RIL5FkdnnMQj9TC-Ee6I2V2ethl3itSAwCHM/image_thumb36?imgmax=800

Publish folder

Now let’s move back to our Dockerfile and do the following changes.

FROM microsoft/aspnetcore:1.0.1
LABEL Name=dockerwebapp Version=0.0.1
ARG source
WORKDIR /app
EXPOSE 30000
COPY ${source:-bin/Release/netcoreapp1.1/publish} .
ENTRYPOINT dotnet DockerWebApp.dll

We don’t want to copy the whole project folder to our Image so we set it to only copy the publish folder. Then we rename the DLL name in the ENTRYPOINT to same as our file name in the publish folder.

Now let’s build the Docker Image, so we can create a Container out of it.

For that, we are running the following command.

PS C:\Users\Jaliya\Desktop\DockerWebApp> docker build -t dockerwebapp .

We can see the following output.

https://lh3.googleusercontent.com/-7QY9vyjqQCg/WR3k4oxfcyI/AAAAAAAAEbE/N-nxCUmvC4wdKT4lApzToTzQOPESELNxQCHM/image_thumb26?imgmax=800

docker build -t dockerwebapp .

All seems to be good, there is a warning at the end, we can basically ignore that for now for the sake of this demo. Now let’s run the following command to confirm whether our image is created.

PS C:\Users\Jaliya\Desktop\DockerWebApp> docker images

https://lh3.googleusercontent.com/-pN9Q7fzcK48/WR3k6qtEE3I/AAAAAAAAEbM/OV4d5N1G9zo1mWV5r2KU2dXK_zVugVAnQCHM/SNAGHTML82e020a_thumb2?imgmax=800 docker images

Yes, it is created, now let’s try to run it.

PS C:\Users\Jaliya\Desktop\DockerWebApp> docker run dockerwebapp

Now getting an error ���The specified framework 'Microsoft.NETCore.App', version '1.1.2' was not found.”.

https://lh3.googleusercontent.com/-TgdIOaDlKUk/WR3k8u6EFjI/AAAAAAAAEbU/BSl_alV0kzM9h0BGf4B0NxHw7UzcRT8rACHM/image_thumb27?imgmax=800 docker run dockerwebapp

Of course, we have been using an older aspnetcore image, let's change the Dockerfile as follows.

FROM microsoft/aspnetcore:1.1.2
LABEL Name=dockerwebapp Version=0.0.1
ARG source
WORKDIR /app
EXPOSE 30000
COPY ${source:-bin/Release/netcoreapp1.1/publish} .
ENTRYPOINT dotnet DockerWebApp.dll

Here is the link where you can find the list ASP.NET Core Docker image information. https://hub.docker.com/r/microsoft/aspnetcore/

Now let’s run a docker build again.

PS C:\Users\Jaliya\Desktop\DockerWebApp> docker build -t dockerwebapp .

https://lh3.googleusercontent.com/-Rj7lYiC6Hm8/WR3k-6fiCII/AAAAAAAAEbc/gGeHMvthaekcymc6EnfHeXdioov0KUiDgCHM/image_thumb28?imgmax=800 docker build -t dockerwebapp .

Now let’s run the image again.

https://lh3.googleusercontent.com/-_SZILw0xYhc/WR3lAOosQpI/AAAAAAAAEbk/UF6n78yvBR8hQgwApLdcJmij5reRXRRSQCHM/image_thumb29?imgmax=800 docker run dockerwebapp

Yes, it seems to be working. Let’s run the following command in a new PowerShell Window to see the status of all the running Containers.

PS C:\Users\Jaliya> docker ps

Here note that we are running the command from another directory. You can run docker ps from sitting inside any directory.

https://lh3.googleusercontent.com/-jJALvZM4XTg/WR3lBfs7AxI/AAAAAAAAEbs/iDpaCklD5V8mZxuuLcc1hup3lRWRpaX7wCHM/image_thumb30?imgmax=800 docker ps -a

Now from our host machine let’s try to access the site http://localhost:30000 from a browser.

https://lh3.googleusercontent.com/-yIB84awmsDM/WR3lCRSgSUI/AAAAAAAAEb0/LTEGhmGE5XEUCIT21NErry2onRI1E07lQCHM/image_thumb31?imgmax=800 The site can't be reached

That’s because we haven’t mapped Container ports and host machines ports. We can easily fix it by running docker run specifying port mappings.

PS C:\Users\Jaliya\Desktop\DockerWebApp> docker run -p 30000:80 dockerwebapp

https://lh3.googleusercontent.com/-MDkivW_k4q0/WR3lD7loBFI/AAAAAAAAEb8/UZDA7BIEFi8Ved1s_P-P-nGAdkWNAvFNQCHM/image_thumb32?imgmax=800 docker run -p 30000:80 dockerwebapp

And now if we check for running Containers, we can see the ports are mapped.

https://lh3.googleusercontent.com/-xDnxsmUSJPs/WR3lFMHguEI/AAAAAAAAEcE/c9SYRf0R-O0CxWOut69rggKOz91gqcXBQCHM/image_thumb33?imgmax=800 docker ps

Now let’s browse to http://localhost:30000 back again. And this time, it works!

https://lh3.googleusercontent.com/-_LQIeaFV0Kg/WR3lGkPEp8I/AAAAAAAAEcM/XsYmkRX9dKUCsnaiYpU9B_QN2NhQ4NeegCHM/image_thumb34?imgmax=800 Site is running

In this post, we have used the following commands in the background.

# List running containers
docker ps
 
# List all containers
docker ps -a
 
# List all images
docker images
 
# Stop given container
docker stop {container_name}
 
# Delete given container
docker rm {container_name}
 
# Delete given image
docker rmi {image_id}
 
# Delete all containers
docker rm $(docker ps -a -q)
 
# Delete all images
docker rmi $(docker images -q)

More info

Running and Exploring a Docker Linux Container