You can use containers to establish a .NET development environment with only Docker and an editor. The environment can be made to match your local machine, production or both.
The following examples demonstrate using dotnet run
in a .NET SDK container. It builds an application from source and then launches it. You have to re-launch the container every time you want to observe source code changes.
Alternatively, you can use dotnet watch run
. This command reruns the application within a running container, with every local code change.
The instructions assume that you have cloned the repository locally.
You may need to enable shared drives (Windows) or file sharing (macOS) first.
Container scenarios that use volume mounting can produce conflicts between the bin
and obj
directories in local and container environments. To avoid that, you need to use a different set of obj
and bin
folders for your container environment. The easiest way to do that is to copy a custom Directory.Build.props into the directory you are using (like the dotnetapp
directory in the following example), either via copying from this repo or downloading with the following command:
curl -o Directory.Build.props https://raw.githubusercontent.com/dotnet/dotnet-docker/main/samples/Directory.Build.props
Note
You may need to remove bin
and obj
directories if you run these instructions on Windows in both Windows and Linux container modes.
The following example demonstrates using dotnet run
with a console app in a .NET SDK container. This initial example is demonstrated on macOS. Instructions for all OSes follow.
The instructions assume you are in the samples/dotnetapp
directory (due to the volume mounting -v
syntax).
% docker run --rm -it -v $(pwd):/app/ -w /app mcr.microsoft.com/dotnet/sdk:8.0 dotnet run
42
42 ,d ,d
42 42 42
,adPPYb,42 ,adPPYba, MM42MMM 8b,dPPYba, ,adPPYba, MM42MMM
a8" `Y42 a8" "8a 42 42P' `"8a a8P_____42 42
8b 42 8b d8 42 42 42 8PP!!!!!!! 42
"8a, ,d42 "8a, ,a8" 42, 42 42 "8b, ,aa 42,
`"8bbdP"Y8 `"YbbdP"' "Y428 42 42 `"Ybbd8"' "Y428
OSArchitecture: X64
OSDescription: Debian GNU/Linux 12 (bookworm)
FrameworkDescription: .NET 8.0.0
UserName: root
HostName : 0dea5548ae88
ProcessorCount: 16
TotalAvailableMemoryBytes: 33632350208 (31.32 GiB)
You can test this working by simply editing Program.cs. If you make an observable change, you will see it. If you make a syntax error, you will see compiler errors.
The following instructions demonstrate this scenario in various environments.
docker run --rm -it -v $(pwd):/app/ -w /app mcr.microsoft.com/dotnet/sdk:8.0 dotnet run
This example uses PowerShell.
docker run --rm -it -v ${pwd}:/app/ -w /app mcr.microsoft.com/dotnet/sdk:8.0 dotnet run
This example uses PowerShell.
docker run --rm -it -v ${pwd}:c:\app\ -w \app mcr.microsoft.com/dotnet/sdk:8.0 dotnet run
The following example demonstrates using dotnet run
with an ASP.NET Core app in a .NET SDK container. This initial example is demonstrated on macOS. Instructions for all OSes follow.
The instructions assume you are in the samples/aspnetapp/aspnetapp
directory (due to the volume mounting -v
syntax used).
% docker run --rm -it -p 8000:8080 -v $(pwd):/app/ -w /app -e ASPNETCORE_HTTP_URLS=8080 -e ASPNETCORE_ENVIRONMENT=Development mcr.microsoft.com/dotnet/sdk:8.0 dotnet run --no-launch-profile
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:8080
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app
You can use CTRL-C to terminate dotnet run
. After the application starts, navigate to http://localhost:8000
in your web browser.
Note
This example (and those in the instructions that follow) configure ASP.NET Core via environment variables and disable the use of a launch profile (none of the launch profiles are compatible with this scenario). Instructions are provided later in this document that add and use a new launch profile, which removes the need for specifying environment variables with the Docker CLI.
The following instructions demonstrate this scenario in various environments:
docker run --rm -it -p 8000:8080 -v $(pwd):/app/ -w /app -e ASPNETCORE_HTTP_URLS=8080 -e ASPNETCORE_ENVIRONMENT=Development mcr.microsoft.com/dotnet/sdk:8.0 dotnet run --no-launch-profile
This example uses PowerShell.
docker run --rm -it -p 8000:8080 -v ${pwd}:/app/ -w /app -e ASPNETCORE_HTTP_URLS=8080 -e ASPNETCORE_ENVIRONMENT=Development mcr.microsoft.com/dotnet/sdk:8.0 dotnet run --no-launch-profile
This example uses PowerShell.
docker run --rm -it -p 8000:8080 -v ${pwd}:C:\app\ -w \app -e ASPNETCORE_HTTP_URLS=8080 -e ASPNETCORE_ENVIRONMENT=Development mcr.microsoft.com/dotnet/sdk:8.0 dotnet run --no-launch-profile
The examples above use environment variables to configure ASP.NET Core. You can instead configure ASP.NET Core with a launchSettings.json file. The launchSettings.json file in this app has been updated with a container
profile that can be used instead of specifying environment variables with the docker CLI.
The following JSON segment shows the container
profile that was added to enable this workflow.
"publicdev": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://+:8080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
The following instructions demonstrate this scenario in various environments:
docker run --rm -it -p 8000:8080 -v $(pwd):/app -w /app mcr.microsoft.com/dotnet/sdk:8.0 dotnet run --launch-profile publicdev
The following example uses PowerShell.
docker run --rm -it -p 8000:8080 -v ${pwd}:/app -w /app mcr.microsoft.com/dotnet/sdk:8.0 dotnet run --launch-profile publicdev
The following example uses PowerShell.
docker run --rm -it -p 8000:8080 -v ${pwd}:C:\app -w C:\app mcr.microsoft.com/dotnet/sdk:8.0 dotnet run --launch-profile publicdev