Building and Dockerizing a .NET Microservice using Gitpod and Ubuntu 22.04
When it comes to microservices, the combination of .NET and Docker offers a powerful and scalable approach. In this article, we’ll walk you through creating a .NET microservice using Gitpod (running on Ubuntu 22.04), and then packaging it as a Docker container. By the end, you’ll have a neat little service ready for deployment!
Setting Up Your Environment
To kick things off, we’ll get the .NET SDK set up in Gitpod. Assuming you’ve launched Gitpod and are greeted by the terminal:
- Update the package lists for upgrades and new packages:
sudo apt-get update
- Install the .NET SDK 7.0:
sudo apt-get install -y dotnet-sdk-7.0
- To ensure that dotnet tools are accessible from any directory, add them to the path variable:
cat << \EOF >> ~/.bash_profile
# Add .NET Core SDK tools
export PATH="$PATH:/home/gitpod/.dotnet/tools"
EOF
- Reload your bash_profile to apply the changes:
source ~/.bash_profile
Crafting Our Microservice: A Simple Weather Service
For our tutorial, let’s imagine we’re building a weather microservice that returns the current weather for a given city. This is a simple yet practical use case which can be expanded upon.
1. Create a new Web API project:
dotnet new webapi -n WeatherService
cd WeatherService
2. Add a new controller named WeatherController
:
dotnet add package Microsoft.AspNetCore.Mvc --version 7.0.0
Now, create a new file named WeatherController.cs
in the Controllers
folder. Populate it with a simple action that returns weather details:
using Microsoft.AspNetCore.Mvc;
namespace WeatherService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class WeatherController : ControllerBase
{
[HttpGet("{city}")]
public ActionResult<string> Get(string city)
{
// For the sake of simplicity, we'll return a hardcoded response.
// In a real-world application, you might fetch this from a database or an external API.
return $"Weather in {city}: Sunny, 25°C";
}
}
}

3. Run the service:
dotnet run
You can now test the service by navigating to https://localhost:5001/api/weather/{cityname}
.
Dockerizing Our Microservice
Now, let’s package our microservice into a Docker container.
1. Create a Dockerfile
in the root of your project:
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["WeatherService.csproj", "./"]
RUN dotnet restore "WeatherService.csproj"
COPY . .
# WORKDIR "/src/WeatherService"
RUN dotnet build "WeatherService.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WeatherService.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WeatherService.dll"]

2. Build the Docker image:
docker build -t weatherservice .
3. Run the container:
docker run -d -p 5001:80 --name myweatherservice weatherservice
Your microservice is now running inside a Docker container! Access it the same way as before, but this time, it’s containerized.
Wrapping Up
In this article, we took a whirlwind tour of creating a simple .NET microservice using Gitpod on Ubuntu 22.04, and then we dockerized it for easy deployment. With this foundation, you can expand the functionality, connect to databases, integrate with other services, and scale it to serve millions. The world (or at least the weather) is now at your fingertips! 🌤️🌧️🌩️🌪️
Addendum: Useful Docker Commands
List all Docker images:
sudo docker images
List all running containers:
sudo docker ps
To see all containers, including the stopped ones:
sudo docker ps -a
View logs of a specific container:
sudo docker logs [CONTAINER_ID_OR_NAME]
For example, if you want to see the logs of a container named myweatherservice
:
sudo docker logs myweatherservice
If you want to follow the logs in real-time, you can use:
sudo docker logs -f myweatherservice
Inspect detailed information about a container:
sudo docker inspect [CONTAINER_ID_OR_NAME]
Inspect detailed information about an image:
sudo docker inspect [IMAGE_ID_OR_NAME]
Remove a Docker image:
sudo docker rmi [IMAGE_ID_OR_NAME]
Stop a running container:
sudo docker stop [CONTAINER_ID_OR_NAME]
Start a stopped container:
sudo docker start [CONTAINER_ID_OR_NAME]
Remove a container:
sudo docker rm [CONTAINER_ID_OR_NAME]
For demonstration purposes, I’ll assume you want to start another instance of the weatherservice
image with a different name:
sudo docker run -d -p 5002:80 --name myweatherservice2 weatherservice
This will start a second container named myweatherservice2
exposed on port 5002
.
Now, to shut both containers down, you can use the docker stop
command:
- Stop the first container:
sudo docker stop myweatherservice
- Stop the second container:
sudo docker stop myweatherservice2
If you have many containers and want to stop them all at once, you can use the following command:
sudo docker stop $(sudo docker ps -q)
This command retrieves the IDs of all running containers with docker ps -q
and then stops them all with docker stop
.