Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Containerize .NET 7.0 Console AND ASP.NET application with Docker?

I’m a little stuck, I have a question here which is around missing /bin/sh in Docker containers. I’m looking for the correct docker image for .NET 7.0 on LINUX that can host a Console Application with an embedded ASP.NET OWIN web server, do I need to make a custom one here? None of them seem to have /bin/sh?

Error: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown

Here is my docker file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

FROM ubuntu/dotnet-aspnet:7.0-22.10_edge

COPY . .

RUN dotnet publish "./RaspCronJobServer/RaspCronJobServer.csproj" -o /application

WORKDIR /application

EXPOSE 5000

ENTRYPOINT [ "dotnet", "RaspCronJobServer.dll" ]

>Solution :

It seems the real question is how to use Chiseled Ubuntu container images. dotnet-aspnet:7.0-22.10_edge is the name of a Chiseled image, not a regular image.

As the linked blog post explains, these are essentially distroless images, stripped down to the very basics. There’s no shell or anything not directly needed by the application itself. Building the application still requires a regular SDK image but the final target will be a chiseled image.

From the Using Chiseled Container Images example, the Dockerfile should look like this:

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

# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore --use-current-runtime

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

# final stage/image
FROM mcr.microsoft.com/dotnet/nightly/runtime:7.0-jammy-chiseled
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "dotnetapp.dll"]

This builds the app normally but packages it in a Chiseled image instead of the normal runtime image.


Original Answer

Short Version: You can already publish an ASP.NET Core 7 web app directly to Docker :

dotnet publish --os linux --arch x64 -c Release -p:PublishProfile=DefaultContainer

ASP.NET Core doesn’t need OWIN, it uses its own middleware pipelines and provides everything that OWIN did. I’m not sure if it can use OWIN, or it will interfere with ASP.NET Core.

ASP.NET Core applications are console applications too, so the problem is solved out of the box. They can be packaged into Docker images easily. The ASP.NET Core documentation shows how to manually add a Dockerfile to build and package the web app but that can also be done using project template settings.

The Dockerfile builds the application using the SDK image and copies the output to a new image based on the Runtime image. This ensures the final image is as small as possible:

# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source

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

# copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /source/aspnetapp
RUN dotnet publish -c release -o /app --no-restore

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

The actual publishing command is dotnet publish -c release -o /app --no-restore. You could create a single file executable if you wanted, but since you use a container it’s fine to use dotnet aspnetapp.dll to run the web app.

Publish direct to Docker

.NET 7 can publish directly to Docker images, eliminating the Dockerfile. You can do what you want with a single profile switch.

From the blog example, you can create an MVC app with

# create a new project and move to its directory
dotnet new mvc -n my-awesome-container-app
cd my-awesome-container-app

# add a reference to a (temporary) package that creates the container
dotnet add package Microsoft.NET.Build.Containers

And publish it to Docker with

# publish your project for linux-x64
dotnet publish --os linux --arch x64 -c Release -p:PublishProfile=DefaultContainer
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading