Create Docker Image of Web Application in .NET Core and Deploy using Docker Desktop on Windows OS

What is Docker

Docker is an open-source platform that allows you to automate application deployment, scaling, and management using containerization. Containerization is a lightweight virtualization technology that enables you to package an application along with its dependencies into a standardized unit called a container.

Key Concepts and Components of Docker

  1. Docker Image: An image is a read-only template containing all the files, libraries, and dependencies required to run an application. Images are built using a Dockerfile, which defines the instructions to create the image. Docker images are stored in a registry, such as Docker Hub, where they can be easily shared and downloaded.
  2. Container: A container is an instance of an image that can be run, started, stopped, and deleted. It encapsulates the application and its dependencies, providing a consistent and isolated environment for running it across different systems. Containers are lightweight, portable, and can be easily replicated.
  3. Dockerfile: A Dockerfile is a text file that contains a set of instructions for building a Docker image. It specifies the base image, copies files, installs dependencies, and configures the container environment. Dockerfiles enable reproducibility and version control of the image creation process.
  4. Docker Engine: The Docker Engine is the runtime that runs and manages Docker containers. It includes the Docker daemon, a background process responsible for building, running, and managing containers, and the Docker client, which provides a command-line interface (CLI) for interacting with the Docker daemon.
  5. Docker Compose: Docker Compose is a tool that allows you to define and manage multi-container applications. It uses a YAML file to specify the services, networks, and volumes required for an application. Docker Compose simplifies the orchestration of complex multi-container setups and enables easy scaling and configuration.
  6. Docker Swarm and Kubernetes: Docker Swarm and Kubernetes are orchestration tools that provide clustering and container management capabilities for deploying and scaling applications across multiple nodes or machines. They allow you to define desired states, manage container placement, and ensure high availability and fault tolerance.

Advantages of using Docker

  • Portability: Docker containers can run consistently on different environments, such as development machines, testing servers, or production servers, without worrying about compatibility issues.
  • Isolation: Containers provide isolation between applications and their dependencies, ensuring that changes made to one container do not affect others. This allows for more efficient resource utilization and avoids conflicts between different software stacks.
  • Scalability: Docker makes it easy to scale applications horizontally by running multiple containers across multiple machines, distributing the load, and improving performance.
  • Reproducibility: Docker images and Dockerfiles enable version control and reproducibility of application deployments. This ensures that the same image can be used to deploy the application in different environments or at different points in time.
  • Ecosystem: Docker has a vast ecosystem of pre-built images available on Docker Hub, making it easy to leverage existing containers and speed up the development process.
  • Steps to create and run a Docker image and push it to Docker Hub:

Steps 1. Create a Web Application in .net Core and check the “Enable Docker” option.

Docker image of web application 

Once the project gets created, a new Docker file will be available.

Docker image of web application

Step 2. Once your project is done and good to deploy, then open the file and explore where DockerFile has saved or copy the DockerFile to the same solution path of the project.

Docker image of web application

Step 3. Open Docker Desktop and connect to your Docker Hub account.

Docker image of web application

In case you are facing an issue while connecting Docker Desktop to Docker Hub, then try running the below command.

Firstly, change the working directory to c:\Program Files\Docker\Docker.

Next, in order to switch the daemon, run .\DockerCli.exe -SwitchDaemon; note that you will likely need to run this command twice to switch back to the installed(preferred) daemon.

.\DockerCli.exe -SwitchDaemon

Step 4. Open the cmd prompt as administrator and map to the project file explorer path.

Docker image of web application

Step 5. let's create/ build an image for your working application using the below command.

Docker image of web application

docker build -t <imagename>: <tag> .

This command will build the Docker image based on the Dockerfile in the current directory. The -t flag is used to tag the image with a name (firstdockerimg in this case). Don't forget to add a dot (.) at the end of the command.

<tag> is optional. If you didn't mention the tag, then it would consider the latest version.

Docker image of web application

Step 6. Open the Docker desktop, and you can see that image in your local tab. You can also able to check the list of images by executing the below command on the cmd prompt.

Docker images

Docker image of web application

Docker image of web application

Step 7. Run that image on the Docker desktop by clicking on run action, which prompts up container details where we need to give the port number to run the application.

Docker image of web application

So for the above example, I have set a port as 8081 and clicked run. Open the web browser and hit the localhost URL, i.e., "http://localhost:8081", which displays my application in a running state.

Docker image of web application

You can stop the container from running that image by clicking the stop action.

Docker image of web application

Step 8. Another way to run that image is using the cmd prompt command like "docker run –p 8090:80 firstdockerimg:1.0".

docker run -p 8090:80 <imagename>: <tag>
  • -p --> It means port mapping.
  • 8090 --> It is our random port number to run our application (We can give our own).
  • 80 --> This one is we exposed 80 ports while creating an image. So that port number we have to map with a container that we created now.

Docker image of web application

Open the web browser and hit URL i.e. "http://localhost:8090".

Docker image of web application

The image has been created, and able to run that image using Docker.

Now let's push that image to Docker Hub to make it available over the cloud to access it in different environments or nodes where you need to access an application.

Step 9. Log in to Docker Hub using the command prompt. It might ask you to enter credentials or authenticates by itself if you have connected to Docker Desktop.

Docker image of web application

Step 10. Create a tag for an image to push over the hub using the Docker Hub username which you have used to log in.

docker tag <imagename>:<tag> <hubUsername>/<imagename>

A separate image creates, which we can push by using the below commands.

docker push <imagename> : <tag>

Docker image of web application

Steps 11. Login to Docker Hub, and you can see a new repository will be created, under which you may have your pushed image with the respective tag.

The same can be seen on Docker Desktop under the Hub tab.

Docker image of web application

Docker image of web application

Later, every new image with a different tag will be enlisted within it.

To download the specific tagged image to run on a specific environment or node, use the below command.

docker image pull <imagename>:<tag>

Summary

Overall, Docker simplifies the process of packaging, deploying, and managing applications, making it a popular choice for developers and system administrators in modern software development workflows.

I hope you like it and could be able to pull the attempt to create and host the first Docker image.


Similar Articles