user687554
user687554

Reputation: 11151

How to create & run .net Core Console App in docker

I have a .NET Core 2.1 console app. I want to run this console app in a Docker image. I'm new to Docker and just trying to figure it out.

At this time, I have a Dockerfile, which was inspired from Microsoft's Example. My file actually looks like this:

FROM microsoft/dotnet:2.1-runtime-nanoserver-1709 AS base
WORKDIR /app

FROM microsoft/dotnet:2.1-sdk-nanoserver-1709 AS build
WORKDIR /src
COPY MyConsoleApp/MyConsoleApp.csproj MyConsoleApp/
RUN dotnet restore MyConsoleApp/MyConsoleApp.csproj
COPY . .
WORKDIR /src/MyConsoleApp
RUN dotnet build MyConsoleApp.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish MyConsoleApp.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyConsoleApp.exe"]

My question is, what is the difference between: microsoft/dotnet:2.1-runtime-nanoserver-1709, microsoft/dotnet:2.1-sdk, and microsoft/dotnet:2.1-runtime? Which one should I use for my .NET 2.1 console app? I'm confused as to if I a) build my console app then deploy it to a docker image or b) build a docker image, get the code, and build the console app on the image itself. Any help is appreciated.

Upvotes: 19

Views: 28699

Answers (6)

Odilbek Utamuratov
Odilbek Utamuratov

Reputation: 341

For .NET 7 the following code works

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

COPY . .
RUN dotnet restore <your app name>.csproj --disable-parallel
RUN dotnet publish <your app name>.csproj -c Release -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build /app ./

ENTRYPOINT ["dotnet", "<your app name>.dll"]

Upvotes: 1

Michael Ceber
Michael Ceber

Reputation: 2452

I put an example on Github on how to do this for a .net core webapi application, so should be pretty much the same for a console application.

My docker file looks like:

https://github.com/MikeyFriedChicken/MikeyFriedChicken.DockerWebAPI.Example

This is doing pretty much the same as yours.

Just to clarify previous answers what your dockerfile is doing is creating 'temporary' images which have the ability to build your software. Once it is all built these images just get scrapped.

If you look at your last lines in your dockerfile you can see it copies the output from the build images to your final image which is based on 'microsoft/dotnet:2.1-runtime-nanoserver-1709' which has just enough libraries and dependencies for running your .net core application.

Upvotes: 0

Mugen
Mugen

Reputation: 9095

For anyone interested in actual Dockerfile reference:

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /app

COPY <your app>.csproj .
RUN dotnet restore <your app>.csproj

COPY . .
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime
WORKDIR /app
COPY --from=build /app/out ./

ENTRYPOINT ["dotnet", "<your app>.dll"]

Upvotes: 14

zhimin
zhimin

Reputation: 3050

My answer for the first question is:

microsoft/dotnet:2.1-runtime-nanoserver-1709 is a docker image you can run .net core apps,

And microsoft/dotnet:2.1-sdk-nanoserver-1709 is a docker image you build apps , you can also run app with it.

And my answer for the second question is:

If you just want to run app in docker, build your app with visual studio on you local machine (this will be the most easy way to build it), use the microsoft/dotnet:2.1-runtime-nanoserver-1709 , build a docker image.

Upvotes: 0

Sagar
Sagar

Reputation: 37

microsoft/dotnet:2.1-runtime-nanoserver-1709 - This image is used to setup .net runtime, under which any .net app runs.

microsoft/dotnet:2.1-sdk-nanoserver-1709 - This image is of SDK used to compile your .net core app

Refer this blog for how to create & run .net core console app in docker:

Upvotes: 0

bxN5
bxN5

Reputation: 1430

It's a question more not about docker itself, but .net docker images

You can go to the https://hub.docker.com/r/microsoft/dotnet/ click in particular image find Dockerfile and understand what exactly software builds in this image and find the difference between different Dockerfile's

Regarding second question better to "build a docker image, get the code, and build the console app on the image itself." So you build the image, expose port, mount your code as volume, and your code executes inside the container with help of software installed in the container

Upvotes: 2

Related Questions