How to Containerize Application using Docker

This article will show you how to use Docker to containerize your application so you can run them on any server. When we deploy an application at first everything seems to work fine but when it is moved to different environment faces compatibility issues due to OS, library files etc. We can overcome this by using Docker.

Containerize asp.net Application with Docker.

Let’s implement this. Please walk-through the steps to Containerize asp.net application.

Pre-Requisites

  1. Install Visual Studio 2022. Download here
  2. Install Docker Desktop . Download here. The Docker Desktop is available for Linux, windows and mac.
  3. Create Docker hub Account and Create repository.

 

Create a simple asp.net core web application using visual studio 2022.

Create New Project

Search for ASP.NET Core Web project template and select asp.net core web app MVC template and click Next.

Enter project name

Select Target Framework and Click Create

Edit index.cshtml file in Home folder and add “Welcome to my Image Gallery” html tag

Click the Project Name in the Solution Explorer -> Select Add->Docker support.

You can now see the Dockerfile created in the Solution Explorer.

Dockerfile commands:

FROM – Creates a Layer from Microsoft asp dotnet:6.0 base image. We can have multi stage Builds for dev, test and prod.

WORKDIR – Define Working Directory

COPY – copy your local files/directories to Docker Container

RUN-Builds Container

ENTRYPOINT – Defines command and argument for executing a container.

Check Docker version

C:\>docker –version

Docker version 20.10.12

Build and Run the Application.

Open browser and enter the following URL to get the results.

http://localhost:<portno>

Yes , Now you are running your Application inside the Docker Container locally from Windows Docker Desktop.

Check if your Container and Docker images are running.

Open Docker desktop, click Containers/Apps. You can see a new container named MyimageGallery running as shown below

Select Image tab to view the imagegallery image

Note: You can also use the below commands from windows command prompt or PowerShell.

Open windows command prompt

Check Docker version

C:\>docker –version

Docker version 20.10.12, build e91ed57

View the Container running in Docker desktop Application.

>docker container ls

View all the Docker images

>docker images

Note : You can also push the image to  Docker Hub.

Now View the image in the Remote Repository tab after logging into docker desktop using Docker Hub Username & Password

Docker Commands

docker ps -a

docker start <containerid>

docker stop <containerid>

docker rm -f <containerid>

This container has executable component to run the application. The Container  can be pushed to ant Registry service and pull it to any environment without the need of any installation of operating system, dependencies and application software.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?