The foundation of modern microservices starts with isolated, lightweight environments
Containers are the standard way to run microservices reliably and consistently.
They let you package your app and all its dependencies into a single, lightweight unit and run it anywhere.
โ๏ธ Why Containers?
๐ Fast startup
No full OS needed like virtual machines. Containers boot in seconds.๐งณ Portable
Build once and run anywhere: on a laptop, in the cloud or in CI pipelines.๐งฑ Isolated
Each container has its own filesystem, environment and runtime.๐ง Customizable stacks
Mix .NET, Node.js, Java, Python and even legacy systems side by side.๐ก Better resource usage
Share the host OS kernel without duplicating entire machines.
๐งฐ Docker in a Nutshell
Docker is the most popular container engine. Here is what it offers:
Concept | Description |
---|---|
๐งช Image | Snapshot of your app and its environment |
๐ฆ Container | A running instance of an image |
๐ Registry | A place to store or pull container images (like Docker Hub) |
๐งฑ Layering | Images can be built on top of other images |
๐ Dockerfile | Script that defines how to build your image |
๐ Example: Dockerfile for ASP.NET Core
# Use Microsoft's base image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build -c Release -o /app/build
# Publish stage
FROM build AS publish
RUN dotnet publish -c Release -o /app/publish
# Final image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]
This setup:
Builds your app in a clean environment
Publishes only the needed output
Runs it on a minimal runtime image
โ๏ธ Docker Compose Example
If you need to run multiple services together use Docker Compose:
version: "3.4"
services:
web:
image: myapp
ports:
- "8080:80"
redis:
image: redis:latest
Compose lets you:
Run and connect multiple containers such as your app and a database
Map internal and external ports
Use shared volumes and networks
๐งช Local Dev with Docker in Visual Studio
Visual Studio has built-in Docker support:
Right-click your project, select Add, then Container Orchestrator Support
Choose Docker Compose to manage multiple containers
Visual Studio generates a
Dockerfile
and adocker-compose.yml
for youPress F5 to run your app in a container
Make sure Docker Desktop is running and the whale icon is visible in your taskbar.
Containers make your microservices fast, portable and production-ready.
Once your services are containerized, you can move on to orchestration tools such as Kubernetes or Azure Container Apps.