Lesson 10 of 40 DevOps Intermediate 55 min

Docker & Container Development

Learn how to containerize an ASP.NET Core application in Visual Studio 2026. You will add Docker support, understand a multi-stage Dockerfile, run and debug containers, use Docker Compose for multi-service apps, manage environment variables and logs, and prepare your project for reliable deployment.

DockerfilePackage your API consistently
ComposeRun API and database together
DebugUse breakpoints inside containers
Visual Studio 2026 · Containers
APIorders-api
SQLsql-server
IMGstoreapp:dev
LOGcontainer logs
F5debug in container
Build image
ready
API health
ok
DB link
compose
Image size
trim

# Container workflow
docker build -t orders-api:dev .
docker run -p 8080:8080 orders-api:dev
docker compose up --build
Lesson overview

What you will learn

Containers make development, testing, and deployment more consistent by packaging your application with the runtime environment it needs. In this lesson, you will learn the practical Docker workflow for .NET developers using Visual Studio: build, run, debug, compose, inspect, and prepare for deployment.

Package the appCreate a clean Dockerfile for an ASP.NET Core API.
Run dependenciesUse Docker Compose for database-backed applications.
Debug safelyAttach the Visual Studio debugger to containerized code.
Part 1

Why containers matter for .NET projects

A container packages your application, runtime, environment settings, and dependencies into a repeatable unit. This reduces the common “works on my machine” problem because development, testing, and deployment environments become more similar.

ProblemHow containers helpExample
Different runtime versionsImage defines the runtime.NET runtime image
Missing dependenciesDockerfile installs and copies what is neededPublished app files
Multi-service setupCompose starts services togetherAPI + SQL Server
Deployment driftSame image can move through environmentsDev → staging → production
Beginner rule: Use containers to improve consistency, not to hide poor configuration. A containerized app still needs clean settings, logs, health checks, and secure secret handling.
Part 2

Prepare Visual Studio for Docker development

Before adding Docker support, make sure the container runtime is installed and running. Open Visual Studio, load your ASP.NET Core project, and confirm the app can run normally before containerizing it.

Install runtime

Install Docker Desktop or another supported runtime and verify it is running before starting Visual Studio container debugging.

Check the app first

Run the API locally with F5 or Ctrl+F5. Fix normal build or runtime errors before adding Docker.

docker --version docker info dotnet --info
Practical habit: Do not troubleshoot Docker and application bugs at the same time. First prove the app runs locally, then prove it runs in a container.
Part 3

Add Docker support in Visual Studio

For an ASP.NET Core project, Visual Studio can add Docker support from the project context menu. This usually creates a Dockerfile and adds a Docker launch profile so you can build and debug the project inside a container.

  1. Right-click the ASP.NET Core project in Solution Explorer.
  2. Choose Add → Docker Support.
  3. Select the target operating system, usually Linux for cloud deployment scenarios.
  4. Build the project and select the Docker launch profile.
  5. Press F5 to start debugging inside the container.
Good sign: If the browser opens your API endpoint and breakpoints are hit, Visual Studio has successfully built and launched the containerized project.
Part 4

Understand the multi-stage Dockerfile

A multi-stage Dockerfile keeps the final image smaller by using one stage to build the app and another stage to run only the published output. This is common for ASP.NET Core applications.

FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base WORKDIR /app EXPOSE 8080 FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build WORKDIR /src COPY ["OrdersApi.csproj", "."] RUN dotnet restore "OrdersApi.csproj" COPY . . RUN dotnet publish "OrdersApi.csproj" -c Release -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY --from=build /app/publish . ENTRYPOINT ["dotnet", "OrdersApi.dll"]
StagePurposeWhy it matters
baseRuntime environmentDefines where the app will run
buildSDK build environmentRestores, builds, and publishes the project
finalFinal imageContains only published app files and runtime
Part 5

Debug an app running inside a container

Visual Studio can build the image, start the container, attach the debugger, and route the browser to the mapped port. From the developer’s point of view, the debugging workflow is similar to local debugging, but the process runs inside the container.

Use normal breakpoints

Place breakpoints in controller actions, Minimal API handlers, services, and middleware.

Inspect variables

Use Watch, Locals, Call Stack, and Exception Helper just as you would for local debugging.

Check logs

Use Output, terminal logs, or the Containers window to see startup and runtime messages.

Restart cleanly

If the container is stale, rebuild the image or remove old containers before testing again.

docker ps docker logs orders-api docker exec -it orders-api /bin/sh
Part 6

Use Docker Compose for multi-service development

Real applications often need more than one container. Docker Compose lets you describe the API, database, cache, message broker, or background worker in a single file, then start them together.

services: orders-api: build: . ports: - "8080:8080" environment: - ASPNETCORE_ENVIRONMENT=Development - ConnectionStrings__Default=Server=sql;Database=OrdersDb;User Id=sa;Password=${SA_PASSWORD};TrustServerCertificate=True depends_on: - sql sql: image: mcr.microsoft.com/mssql/server:2022-latest environment: - ACCEPT_EULA=Y - SA_PASSWORD=${SA_PASSWORD} ports: - "1433:1433" volumes: - sql-data:/var/opt/mssql volumes: sql-data:
Security note: The example shows configuration style for learning. In real deployments, use secret management rather than hardcoding passwords in source-controlled files.
Part 7

Manage environment variables, ports, and volumes

Containers should be configurable without rebuilding the image. Use environment variables for settings, mapped ports for local access, and volumes for data that should survive container restarts.

ItemUse caseExample
Environment variableChange app settings per environmentASPNETCORE_ENVIRONMENT=Development
Port mappingAccess container service from host8080:8080
VolumePersist database or file datasql-data:/var/opt/mssql
NetworkAllow services to reach one anotherorders-api calls sql
Part 8

Inspect containers, images, logs, and files

Visual Studio’s container tooling helps you see running containers, images, ports, environment values, logs, and file systems. This is useful when a service starts locally but behaves differently inside the container.

1

Check status

Confirm the container is running and the expected ports are mapped.

2

Read logs

Look for startup failures, missing configuration, connection errors, or unhandled exceptions.

3

Inspect environment

Verify that connection strings and environment names are what you expect.

4

Rebuild when needed

If code or Dockerfile changes are not reflected, rebuild the image and restart the containers.

Part 9

Where .NET Aspire fits

.NET Aspire is useful when a .NET solution has several services and infrastructure dependencies. Instead of manually remembering how every service is started, an Aspire AppHost can describe the application model and help you run the distributed system during development.

var builder = DistributedApplication.CreateBuilder(args); var database = builder.AddSqlServer("sql") .AddDatabase("OrdersDb"); builder.AddProject<Projects.OrdersApi>("orders-api") .WithReference(database) .WaitFor(database); builder.Build().Run();
Practical use: Docker Compose is simple and portable. Aspire is helpful when you want a richer .NET-centered development experience for distributed apps, service discovery, dashboards, and local orchestration.
Part 10

Prepare a containerized API for deployment

Before deployment, the container image should be predictable, secure, and easy to monitor. The goal is not merely to make the image run locally, but to make it safe enough for staging and production environments.

Checklist itemWhy it matters
Use production app settingsAvoid running production with development behavior
Keep secrets outside the imagePrevents passwords or tokens from being copied into image layers
Add health endpointsLets platforms detect whether the service is ready
Log to standard outputContainer platforms can collect logs cleanly
Use small runtime imagesReduces attack surface and deployment time
Scan dependencies and imagesFinds known security issues before deployment
Part 11

Use Copilot to review container configuration

Copilot can explain Dockerfiles, suggest improvements, and help identify risky configuration. It should not replace manual review, especially for secrets, networking, ports, and production settings.

Review this Dockerfile for an ASP.NET Core API. Look for unnecessary image layers, missing production settings, exposed ports, and security risks. Do not rewrite it yet; explain the issues first.
Review this docker-compose.yml file. Check whether the API can reach the database, whether secrets are handled safely, and whether volume usage is appropriate for local development.
My API works locally but fails inside Docker. Here are the logs and environment variables. Help me identify whether the problem is ports, connection strings, HTTPS, or missing dependencies.

Hands-on exercise: Containerize the Orders API

  1. Open the Orders API project from Lessons 5–9.
  2. Run it normally first and confirm the API endpoint works.
  3. Add Docker support from Visual Studio.
  4. Read the generated Dockerfile and identify the build, publish, and final stages.
  5. Run the app using the Docker launch profile and hit a breakpoint.
  6. Add Docker Compose with a database service.
  7. Move connection strings into environment variables.
  8. Use container logs to confirm the API connects to the database.
  9. Ask Copilot to review the Dockerfile and Compose file, then verify suggestions manually.
Visual Studio 2026 Made Easy book cover
Recommended companion book

Visual Studio 2026 Made Easy

Visual Studio 2026 Made Easy gives you a structured path for learning Visual Studio, C#, .NET, debugging, testing, web development, databases, profiling, Git source control, deployment, containers, and AI-assisted workflows.