Simple .Net 6.0 Web Api and Docker and Azure Web App Deployment Steps
One of my student clients ask me if I can whip up a simple demo of a dot net project that is pushed to an azure web app using Docker technology. Here, I present a very simple, bare bones, nothing fancy deployment flow of a dot net web api project.
First up, you have to go ahead and create a simple web api project in visual studio. The standard template which gives you the weather controller information on the standard swagger is good enough.
Of course, you can also, get the code from my code repository.
but I strongly recommend you create a new project in Visual Studio and go from there. Just remember to enable both HTTPS and Docker when creating the new project.
I strongly recommend you use Visual Studio 2022 with .Net 6, of course. Also, you have to ensure that Docker is installed, and running. Also, make sure you have a Docker account created before hand.
Note: If you are using Windows Home (like how I am, as Pro is expensive), you have to install WSL2 as well.
Otherwise, deployment wont happen at all.
Also, Remember to do the following, as you do with any, dot net core app. If you dont do this, swagger wont be visible on the deployed app.
// Configure the HTTP request pipeline.//if (app.Environment.IsDevelopment())//{//}app.UseSwagger();app.UseSwaggerUI();
Simply build and run the Docker image. Visual Studio already gives you a ready to use ‘Docker’ button, along with the standard ‘run app’ button that you get when you create a project without Docker.
Once you have debugged your simple web api, you MUST do the Release build. Otherwise, the proper docker image wont be generated.
Also, because of HTTPS, it wont run outside of visual studio. You can run it, but you will keep getting SSL related errors.
Next, you need to push your local Docker image to the Docker Hub. Let’s do that.
docker image listdocker login -u yourusernamehere [and press enter for the password prompt in terminal][now enter password][Login Succeeded]docker tag webapidockerwsl2demo:latest jaydaakarru/dotnetcoreb:firstimagepushdocker push jaydaakarru/dotnetcoreb:firstimagepushThe push refers to repository [docker.io/jaydaakarru/dotnetcoreb]0097e1b6d252: Pushed5f70bf18a086: Pushed37252fa6e7df: Pushed17aff088b762: Pushed9a515fdf7f03: Pushedc4d9ca739af5: Pushed3f94255da7c2: Pushed608f3a074261: Pushedfirstimagepush: digest: sha256:eb72ff460aea382c7a2795bc3bf2f118910ba0accb3bbc8004ca0e34fe9a0c35 size: 1995
Now, go online to your Docker hub, the image you pushed should be visible.
Now, deploy it into azure web app. You already should know by now, about Azure Portal. So, I want you to focus on this specific page.
Here, all you have to do in the portal.
- select Linux Web App
- select Docker Hub (there are other container options as well)
- Put the Public name tag of your docker hub. for example, from the above push we did, jaydaakarru/dotnetcoreb . Azure will pull the container during deployment, no problem.
- You can also use private docker hub by logging in, no problem.
And, assuming all goes as planned, you have your web app running off the docker image.
Final Note
Of course, this is the ‘most’ simplistic usage of containers/docker and so on. Ideally, you would
- put your code in GitHub.
- Link that GitHub to your Docker image.
- That Docker image would then link to Azure Dev Ops
- The Azure Dev Ops would then talk to your web app.
That way, every time, you commit a code change, GitHub will trigger docker, and docker will trigger Azure and you can see the changes in your web app instantly.
But, I dont know those things yet. I know this much. Hope it helps.
I work as a full time freelance coding tutor. Hire me at UpWork or Fiverr or Stack Overflow. My personal website is here. Find more of my art at Behance and Unsplash. Also, I have a Podcast about everyday life. And, a 2nd Podcast, where I talk about freelancing.