19

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.

6 Answers 6

14

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"]
Sign up to request clarification or add additional context in comments.

4 Comments

note to use the correct .NET core version in the base images (2.1, 2.2 etc)
so does ENTRYPOINT .. "find" the static main method (inside Program.cs)..........inside "<your app>.dll" ? Is that the magic?
yes, same as when you just run the dll from debugger or command line
First, it did not work. I had just replaced /app with /source on the second line. Then it worked
2

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

Comments

1

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"]

Comments

0

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:

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.