In my last two articles,Demystifying Docker and An Introduction To Kubernetes. I explained the concept of Docker and Kubernetes and how they help simplify development, deployment, and operations. This article is part 1 of a two-part series on Deploying An Asp.Net Application with Kubernetes aimed to provide a practical guide to Kubernetes. In this article, we will focus on building a docker image for an asp.netcore web API, run the image in a container, and deploy this container to DockerHub.
- Docker, VS Code, and the VS Code Docker extension must be installed. Download Docker here. Download VS Code here.Go to the VSCode Extensions tab, search for Docker, and install it.
VS Code Docker Extension provides a user-friendly way to create, build, and run docker images.
2.For .NET development, you will have to install the .NET Core Software Development Kit.here.
- Create a new folder for your project
- Create a Dotnet core web API project.
Choose Windows or Linux when prompted to choose the operating system. Windows is only applicable if your Docker installation is configured to use Windows containers.
You will be asked if you want to add Docker Compose files. We will not use Docker Compose in this tutorial, so both "Yes" and "No" answers are fine.
5.Change the port for the application endpoint to 5200.
6.Dockerfile and .dockerignore files are added to the workspace.
Let's go through this dockerfile together. I have added comments above the line for easy understanding.
Open the Dockerfile file.
Use ENV instruction to add an environment variable to the service container image. The instruction should be placed in the base stage of the Dockerfile (the first stage in the file). Set the ASPNETCORE_URLS variable to http://*:5200:
Only the base image of the last stage, which is the runtime image, will be included in your image.
It is similar to creating a publish folder on your computer and publishing your web API to that folder. You need all the other things in other to publish the web API. But in reality, it is the final published folder that we send to deploy.
Right-click on the Image built and click on Run or Run Interactive. The containers will run and you should be able to see it Docker app as shown below. What happens under the hood is that docker provides a container for your system to run the docker image. So the image is actually running in the docker daemon container. See the actual command if you were to run it from the command line.
docker run --rm -d -p 5000:5200/tcp --name mytestWebApi testwebapi:latest
When running a container it is important to give it a name so you can easily refer to it. You can also access the container by its id.
What this command does is to run this docker image (testwebapi) with this tag (latest) and map incoming traffic to the docker port you assigned (5200).
The flag -(rm) is used when you need the container to be deleted after the task for it is complete.
This is suitable for small testing or POC purposes and saves the headache for housekeeping.
The flag-(d) means the container should be detached and can run on the background of your terminal. It does not receive input or display output. If you run containers in the background, you can find out their details using (docker ps) and then reattach your terminal to its input and output.
The flag-(p) is to publish the 5000 port from the host to the container to port 5200 within the container. This means anyone connecting to this host over port 5000 will be routed to the container via port 5200.
The flag-(name) is used to name assign a name to the docker container.
open the web browser and navigate to http://localhost:5200/WeatherForecast. You should see weather data in JSON format.
This data is being loaded from your Docker container. If you stop running the container the URL can no longer be reached.
docker container stop [containerID or containerName]--can be used to stop containers that are running.
You can get the full list of docker commands here
DockerHub is an online repository for docker images. A lot of applications and libraries are available as images for download and you can publish yours there.
To publish to docker image you can run this command
docker push [dockerId]/testwebapi:latest
or click on push using docker support as shown in the screenshot below.
If another developer on your team needs access to this API all they need to do is to pull the image and they have the exact webapi. It also helps with easily scaling the application to multiple servers.
- Created a.NetCore Api
- Generated a DockerFile using the vscode docker extension
- Created, built, and docker image,
- Ran the docker image inside a docker container and then published our image to DockerHub.
In the next article, we will be Deploying our Asp.NetCore web API container to Kubernetes.