Docker Aspnetcore

Few weeks ago we saw how we could run ASP NET Core application on Ubuntu. This proving that a .NET Core Application can run on a Linux system, today we will be taking it a step further and see how we can deploy our application in a Docker Linux container.

After being installed you should be able to right click on the icon > Settings on the Docker notification icon and see that Docker is running by checking the status at the bottom left, it should say Docker is running.

Now that Docker is installed and we can start build images and running containers, we can look into some of the terminology and commands from the CLI.

An image is a set of layers describe by the Dockerfile. It represents a state of an environment. It is built with docker build. There can be multiple version of the image tagged to different versions. A container is a running instance of an image bootup using docker run. Multiple containers can be started from the same image.

To manage image and containers, we can use the commnad docker image ... and docker container ....

For example to list all the containers running:

1

docker container ls

Or to see all images:

1

docker image ls

A container can be seen as a running process, it can be stopped using the following command:

1

docker container stop [container-id]

Or killed:

1

docker container kill[container-id]

Rogue images and containers can be cleared with prune:

1

docker image/container prune

Images and containers can be removed using rm with

1

docker image/container rm[image or container id]

Standard output are redirected and can be viewed using:

1

docker container logs [container-id]

Lastly if we want to inspect the content of the container, we can get access to a bash interactive shell by executing the following:

The Dockerfile contains multiple step, FROM specifies the image from you will start, WORKDIR specifies the current dictory within the container. COPY is used to copy files and RUN is used to run processes. At the end of the script we defines the ENTRYPOINT as being the dotnet process with the dll as argument just like we would run dotnet DockerWebApp.dll in our command prompt.

One we have the Dockerfile, we can build by running the following in the root of the application:

1

docker build -f DockerWebApp\Dockerfile -t dockertest:dev .\

docker build builds the image, -f specifies the dockerfile paht while -t represents the name of the repository and the tag of the application. We can see the image built with docker image ls.

Now we can build images and run ASP NET Core application containers but we lost are ability to use the Visual Studio debugger because it’s no longer a process that can be easily attached with a debugger.

To fix that Visual Studio and Docker tools provide an extension which gives full integration of the debugger via docker-compose and dcproj.

When we enabled Docker support, a dcproj was created and saved under the solution. The project contains a docker-compose.yml and an override which are used to orchestrate deployment.

This means that everytme we change the file, the image is updated. For development, the container is already running so there’s no need to manually run it. All we have to do is to select the docker project from Visual Studio and set it as a startup project, run it, we will now be able to breakpoint in the project.

And that concludes today post! We now have an ASP NET Core application deployed on Docker Linux container which can be debugged locally via breakpoint.

Conclusion

Today we saw how we could install Docker Linux containers on Windows, we discovered some of the most common features of the Docker CLI to manage images and containers and lastly we saw a concrete example of how to setup an ASP NET Core application running in Docker Linux container and how we could breakpoint to debug code while running in the container. Hope you like this post, see you next time!