Containerize An AspNet.Core Application With Docker
Tags: docker, devops
Published: 2020-12-14 11:36:24

In my last two articles,[Demystifying Docker](https://dev.to/gbengelebs/demystifying-docker-26id) and [An Introduction To Kubernetes](https://dev.to/gbengelebs/an-introduction-to-kubernetes-5h05). I explained the concept of Docker and Kubernetes and how they help simplify development, deployment, and operations. This article is part 1 of a two-part series on Deploying An Asp.Net Application with Kubernetes aimed to provide a practical guide to Kubernetes. In this article, we will focus on building a docker image for an asp.netcore web API, run the image in a container, and deploy this container to DockerHub. 

#Prerequisites
1. Docker, VS Code, and the VS Code Docker extension must be installed. Download Docker [here](https://docs.docker.com/get-docker/). Download VS Code [here](https://code.visualstudio.com/download).Go to the VSCode Extensions tab, search for Docker, and install it.

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

VS Code Docker Extension provides a user-friendly way to create, build, and run docker images.

2.For .NET development, you will have to install the .NET Core Software Development Kit.[here](https://dotnet.microsoft.com/download).

##Create the WebApi Project

1. Create a new folder for your project 
2. Create a Dotnet core web API project.
![New DotNet Project](https://dev-to-uploads.s3.amazonaws.com/i/xbgiqco8ehun02w4km4f.png)

As you can see this is the boilerplate web API that comes when you create a new project.
  ![Api Screenshot](https://dev-to-uploads.s3.amazonaws.com/i/3vcl54ydzdd38zfyn8gl.png)

##Create DockerFile

1. Open Command Palette-(Ctrl+Shift+P) or on Mac(⇧⌘P) and use the Docker: Add Docker Files to Workspace... command:
![Docker Files](https://dev-to-uploads.s3.amazonaws.com/i/gr0mdm7pjinr5dxfhnes.png)

2. Use .NET: ASP.NET Core when prompted for the application platform.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wta933s8wmji8041jfmp.png)

3. Choose Windows or Linux when prompted to choose the operating system. Windows is only applicable if your Docker installation is configured to use Windows containers.

4. You will be asked if you want to add Docker Compose files. We will not use Docker Compose in this tutorial, so both "Yes" and "No" answers are fine.

5.Change the port for the application endpoint to 5200.

6.Dockerfile and .dockerignore files are added to the workspace.

###Analyzing The DockerFile
Let's go through this dockerfile together. I have added comments above the line for easy understanding.
1. Open the Dockerfile file.

2. Use ENV instruction to add an environment variable to the service container image. The instruction should be placed in the base stage of the Dockerfile (the first stage in the file). Set the ASPNETCORE_URLS variable to http://*:5200:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ulf7s1o5v85syyrftedw.png)

Only the base image of the last stage, which is the runtime image, will be included in your image.
It is similar to creating a publish folder on your computer and publishing your web API to that folder. You need all the other things in other to publish the web API. But in reality, it is the final published folder that we send to deploy.

##Build the Image for the DockerFile you just Created

Open Command Palette (⇧⌘P) and use the Docker Images: Build Image command
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lzne309yolv5dohsuh9w.png)
See the actual build command
![Docker Build](https://dev-to-uploads.s3.amazonaws.com/i/zsjtjn3ckbzsam4elq8n.png)

Open Docker Explorer and verify that the new image is visible in the Images tree:
![Docker Tree](https://dev-to-uploads.s3.amazonaws.com/i/z5jhln2ami2b90o12erk.png)

##Run the Image in a Container

Right-click on the Image built and click on Run or Run Interactive. The containers will run and you should be able to see it Docker app as shown below. What happens under the hood is that docker provides a container for your system to run the docker image. So the image is actually running in the docker daemon container. See the actual command if you were to run it from the command line.
```csharp
docker run --rm -d  -p 5000:5200/tcp --name mytestWebApi testwebapi:latest 
```
When running a container it is important to give it a name so you can easily refer to it. You can also access the container by its id.

What this command does is to run this docker image (testwebapi) with this tag (latest) and map incoming traffic to the docker port you assigned (5200).

The flag -(rm) is used when you need the container to be deleted after the task for it is complete.
This is suitable for small testing or POC purposes and saves the headache for housekeeping.

The flag-(d) means the container should be detached and can run on the background of your terminal. It does not receive input or display output. If you run containers in the background, you can find out their details using (docker ps) and then reattach your terminal to its input and output.

The flag-(p) is to publish the 5000 port from the host to the container to port 5200 within the container. This means anyone connecting to this host over port 5000 will be routed to the container via port 5200.

The flag-(name) is used to name assign a name to the docker container.

open the web browser and navigate to http://localhost:5200/WeatherForecast. You should see weather data in JSON format. 
![ApiResponse](https://dev-to-uploads.s3.amazonaws.com/i/yo3n94hoyfgu7koogqqh.png).

This data is being loaded from your Docker container. If you stop running the container the URL can no longer be reached.

docker container ls---will show you the list of all containers running on your system
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ieddgr0me0l56esqfisu.png)

docker container stop [containerID or containerName]--can be used to stop containers that are running.

You can get the full list of docker commands [here](https://docs.docker.com/engine/reference/commandline/docker/)

##Publishing your Image to DockerHub
DockerHub is an online repository for docker images. A lot of applications and libraries are available as images for download and you can publish yours there.

To publish to docker image you can run this command

```csharp
docker push [dockerId]/testwebapi:latest 
```
or click on push using docker support as shown in the screenshot below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q6bbp2gz1fsrdhkxajvj.png)

You can log in to DockerHub and view your docker image
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1fxmozmp0hs74nuffdal.png).

If another developer on your team needs access to this API all they need to do is to pull the image and they have the exact webapi. It also helps with easily scaling the application to multiple servers.

##In this article we have

1. Created a.NetCore Api
2. Generated a DockerFile using the vscode docker extension
3. Created, built, and docker image, 
4. Ran the docker image inside a docker container and then published our image to DockerHub.

In the next article, we will be Deploying our Asp.NetCore web API container to Kubernetes.