DEV Community

loading...

Simplest sample of DotNetCore in Docker

Mohsen Kokabi
・1 min read

Step 1 - create a DotNetCore web app

dotnet new webapp -o TestDotNetCoreWebApp

Step 2 - build

The way Visual Studio does this is

  • creating a build image docker
  • copy the project files there
  • making the build
  • copying the files back to your final image

here the purpose is to make it in the simplest form, so I am building locally.

cd .\TestDotNetCoreWebApp\

dotnet publish -c Release -o out

Step 3 - Create the docker image

First we need a docker file. You can use an editor but I am just using powershell.

new-item -path "Dockerfile" -ItemType "file" `
-Value 'FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS runtime
WORKDIR /app 
COPY out ./ 
ENTRYPOINT ["dotnet", "TestDotNetCoreWebApp.dll"]' 

Build the image

docker build . -t test-dotnetcore-webapp-docker-image

Step 4

docker run -it --rm -p 8080:80 --name test-dotnetcore-webap-docker  test-dotnetcore-webapp-docker-image

the --rm option will tell docker engine to delete your docker as soon as it stopps.

Step 5

That's all. Your application is running on http://localhost:8080

if you run the docker ps in another terminal you would see your docker instance running but as soon as you stop it with Ctrl+C in the first terminal, the docker will be deleted.

Discussion (0)