Introduction:
Docker is an open, lightweight platform for developing, shipping and running applications using container technology. Docker provides container solutions for developers, architects, DevOps, and IT People. It can run on Linux, Windows, Mac OS and most the cloud providers like AWS, Azure, Google, etc.
What is Docker?
Docker is all about running a single program inside separated environments. It is an open source platform with can be used to package, distribute and run applications in different environments.
Let start understanding by an example,
Applications 1 & 3 will stop working because they need a different framework to run. What can be done now? The above diagram shows, we have three applications running; however, all of the application use the same framework. What will happen if App 1 required different Framework version like 1.0, app 2 needs v2.0, app 3 needs v.3.0. As a result only App 2 will work because our framework installed is v2.0.
One way is to use three different systems for all three applications, but it will be very expensive and maintening them will also be very difficult.
This is how “DOCKER” comes into play. Docker will make sure each application has its own facilities rather than common facilities.
Docker will do something like this.
Build the ASP.Net Core and Docker Packages
Docker will create a package that can run in different environments.
To build an app in Docker, first we need an app to Dockerize.
Docker allows you to build an application in pretty much the same way you would create an application to run on your local machine.
When we create application make sure enable the Docker support and also make sure you have it installed by going to:
https://www.docker.com/products/docker-desktop
Note: Make sure Docker is installed .Otherwise you’ll get the below error:
Installing this (mac or pc) will run docker and allow you to create and host docker containers. Microsoft Visual Studio will use Docker Desktop to host your app inside of during development to allow you to develop or test in.
The trick is to get a new item in your launchSettings.json:
This, in conjunction with a docker file in your project (just called “Dockerfile” without an extension) that builds a container, hosts it, and allows you to debug it remotely.
Writing a Dockerfile:
What is Dockerfile?
In short, Dockerfile contains a series of instructions which define how to construct an image:
Below are the common commands:
FROM |
This command specifies the base image, eg: Microsoft/aspnetcore:2.0 |
WORKDIR |
Changes the working directory for subsequent instructions in the Dockerfile |
COPY |
Copies files from source to destination |
RUN |
Executes a command directly in the container |
EXPOSE |
Sets a specific port internally to the container |
VOLUME |
Usually, it is used to map a physical directory on our machine to the logical directory in the container instead. |
ENTRYPOINT |
Specifies which application will run in the container created from the image. |
To use some of the commands, First create Dockerfile in your application:
Then write Dockerfile for ASP.NET Core app, you must first know if you want to use the Development Environment or the Production Environment.
In development environment you need an image that contains a compiler and allows compiling directly in the container, and for that, use Microsoft/aspnetcore-build directly from the DockerHub (Docker Repository).
In Production environment you do not need a compiler because it will have to build the application externally from the image, integrate all built files in the image, and just use a lightweight image containing the .NET Core Runtime Microsoft/aspnetcore to execute your application.
The Dockerfile is pretty simple (if you’re familiar with Docker) though it’s very automated, so you don’t have to know how it works:
It creates a container from a slim image with ASP.NET Core 2.2 , builds the project and publishes to get a copy of the project in a container. To test/debug with docker, just pick the ‘Docker’ option in the debugging dropdown:
After that, in cmd type:
docker build. –t rootandadmin/DockerApp –f Dockerfile
Now, you have your image and check the type:
docker images
You have your image rootandadmin/DockerApp but as this image is dead so you have to make it alive. For this you will need to create a container. There is a big difference between Image and Container; a container is a process that contains an image and executes it.
Then to create containers from our image type this.
docker create command –p 555:80 –name rootandadmin/DockerApp01 rootandadmin/DockerApp
docker start rootandadmin/ DockerApp01
You can try to access your DockeApp in the browser using the following address.
localhost://555/ DockerApp/Home