DEV Community

Elegberun Olugbenga
Elegberun Olugbenga

Posted on • Edited on

Containerize An AspNet.Core Application With Docker

In my last two articles,Demystifying Docker and An Introduction To Kubernetes. 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. Download VS Code here.Go to the VSCode Extensions tab, search for Docker, and install it.

Docker Extension

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.

Create the WebApi Project

  1. Create a new folder for your project
  2. Create a Dotnet core web API project. New DotNet Project

As you can see this is the boilerplate web API that comes when you create a new project.
Api Screenshot

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

  2. Use .NET: ASP.NET Core when prompted for the application platform.
    Alt Text

  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

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
See the actual build command
Docker Build

Open Docker Explorer and verify that the new image is visible in the Images tree:
Docker Tree

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.

docker run --rm -d  -p 5000:5200/tcp --name mytestWebApi testwebapi:latest 
Enter fullscreen mode Exit fullscreen mode

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.

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

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

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

docker push [dockerId]/testwebapi:latest 
Enter fullscreen mode Exit fullscreen mode

or click on push using docker support as shown in the screenshot below.

Alt Text

You can log in to DockerHub and view your docker image
Alt Text.

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.

Top comments (0)