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.
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.
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.
- # Get base sdk from microsoft
- #Copy the CSPROJ and any dependencies via nuget
- #Copy our project files and build our release
Finally, our application contains the following files.
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:
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.
In the command part, you can see the latest keyword. It is not mandatory, if not provided, it will take the latest version.
When you hit enter, it will try to read the docker file and execute the commands which we have written. Let see!!
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.
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.
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.
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.
Success, now our application is running as a docker image in this URL.
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.
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)
To deploy into Azure service:
- Need an azure portal subscription.
- Needs to create an azure container instance.
- 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:
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.
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