.NET dev environment inside docker

Updated: 30 June 2023

quick

Establish a development environment inside a running container

docker run -it \
--volume $(pwd):/source \
--workdir /source \
mcr.microsoft.com/dotnet/sdk:6.0

docker run -it \
--volume $(pwd):/source \
--workdir /source \
mcr.microsoft.com/dotnet/sdk:7.0

Create a hello world project

dotnet new console --framework net7.0 --use-program-main --name myapp

Run the app

cd myapp
dotnet run

publish & start an application dll

https://github.com/dotnet/dotnet-docker/blob/main/samples/dotnetapp/Dockerfile

Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore

# copy and publish app and libraries
COPY . .
RUN dotnet publish -c Release -o /app --self-contained false --no-restore

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime:6.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "source.dll"]

Build image based on Dockerfile above, in the same directory

docker build -t dndev .

Run a container

docker run dndev

Leave a comment