Creating a simple docker image for .NET Core web application and running it into the local machine as well as in the Azure container service.
Click Create and then select .Net Core as a framework and select .Net Core 2.2 . Next, Select Web Application in the left side panel. In the right-side panel, select Enable docker support and make sure the windows option is selected. This docker image is windows-specific.
Note
If you are missed to select docker for windows support while creating an application, don't worry, you have an alternate option. Right-click on the project in solution explorer and select Add --> there you can see docker support.
Step 4
Once the sample project is created, you can visit DockerFile in the solution explorer of the created project. There you can find a few commands, which states that what are the steps to be followed to create an image of this application. You can also see .dockerignore file in project solution explorer. It is acting like a git ignore file, ignoring a few files while creating a docker image.
For Example:
- # Get base sdk from microsoft
- #Copy the CSPROJ and any dependencies via nuget
-
-
-
-
-
- #Copy our project files and build our release
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Step 5
Finally, our application contains the following files.
Step 6
Let's start with creating a docker image, so to do that open command prompt on project file location. Which means the below folder location:
Step 7
In opened command prompt enter the following command to create an image. docker build -t dockerhubid/projectname: latest .
Command: docker build -t dockerhubid/projectname: latest.
So in our case command is like, docker build -t dockerhubid/dockerdemowebapp .
-t --> means tagging this image with the following name.
dockerhubid --> https://hub.docker.com/ // Here you can create docker hubid.
dockerhubid/dockerdemowebapp --> You can give any name in that place but when you try to host this docker container into Azure or any other cloud service means you should move this docker container image into docker hub and then only you can deploy.
By projectname, it means the full name that we have given.
Finally we have . (dot) symbol which is important with space. We have to follow the docker principle.
Note
In the command part, you can see the latest keyword. It is not mandatory, if not provided, it will take the latest version.
Step 8
When you hit enter, it will try to read the docker file and execute the commands which we have written. Let see!!
Step 9
OOPS, something went wrong, it is because the docker file is not in the correct place. Open the file explorer and navigate into the project location. Move the dockerfile one step backward, which means move the docker file into solution file location and open the command prompt on that location. Like in the below image.
Step 10
Now enter the build command and wait for executing the docker file commands (which means dependencies and all other related). Once the command is successful, you can see the success window.
Step 11
Once the docker image has been created successfully, you can enter the below command to see the images.
command: docker images
There you can see your recently created images.
Step 12
So, we have created the docker image. Now we need to run this image locally. To do that we need a container to run this image. This process can be achieved by running the below command.
command: docker run -p 9090:80 dockerhubid/projectname
-p --> It means port mapping.
9090 --> It is our random port number to run our application (We can give our own).
80 --> This one is we exposed 80 port while creating an image. So that port number we have to map with container that we created now.
So let see by running the command in our case.
Step 13
Success, now our application is running as a docker image in this URL.
http://localhost:9090/
Step 14
That's it !!! We have created a docker image and run it in our local machine.
Now we need to deploy it into Azure. To do that, we need a docker image in a distributed environment. This was we can easily deploy docker image from there into azure container services.
So we can move our docker image into dockerhub repository. To do that execute the following command.
command: docker push dockerhubid/projectname
When you run this command in our command prompt, it looks like below:
Once you've pushed the docker image into the docker hub, you can visit the docker hub login and see your repository as shown below.
Note
You can make this repository public or private. If it is private, then you have to provide the username and credentials while fetching this image from azure service. So for now, we will make it as public. (Click the repository, go to settings and make it public)
Step 15
To deploy into Azure service:
- Need an azure portal subscription.
- Needs to create an azure container instance.
Step 16
- Open https://portal.azure.com in a new tab and enter your credentials.
- Then navigate to add a resource section --> type 'container instance'.
- Create a new container instance resource like below:
Note
Image names should be the same as we gave while creating the docker image.
Imagename: dockerhubid/projectname. Based on this docker hub id, it will fetch and get the image and deploy.
Step 17
In the Azure container service networking tab, you need to provide some DNS name to run our application.
That's it, now you can review + create. It will deploy our application in azure container service.
So, once deployment success, then go to the resource and pickup the DNS name and tab in browser tab.
That is it, our application is running in azure service. I hope this was helpful for you. Enjoy ;).
A few commands in Docker
docker version - Version
docker info - Provides information about installed docker
docker ps - List of containers
docker images - List of images
docker start containerid - Starts the given container
docker stop containerid - Stop the given container
Reference:
https://www.youtube.com/watch?v=f0lMGPB10bM