dotnet / sdk-container-builds

Libraries and build tooling to create container images from .NET projects using MSBuild
https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
MIT License
180 stars 34 forks source link

Add a target to generate the equivalent Dockerfile for a given project #146

Open baronfel opened 2 years ago

baronfel commented 2 years ago

As part of the 'grow up' story for the tool, it would be great if there was a way to scaffold out the Dockerfile equivalent of what we do in the Task. That would give folks an opinionated base to start any customizations from.

seesharprun commented 2 years ago

This is an amazing suggestion. There are some environments (including Cloud Shell) where you don't have an active Docker host. Being able to generate a Dockerfile enables us to build the container image using tools like Azure Container Registry and az acr build.

Today, the tmds/build-image project gets close, but it has some extra commands that require BuildKit.

baronfel commented 2 years ago

@tmds' project is awesome, by the way :) I'm very excited to see so much containerization energy in the .NET ecosystem right now!

Integration with other tools is a great additional use case for this issue. Docker Compose for example doesn't know anything about MSBuild and so cannot reasonably publish and use your container inside docker compose up. If we could emit a Dockerfile (even if it was in gitignore and never commited) that makes it easy to integrate.

tmds commented 2 years ago

Today, the tmds/build-image project gets close, but it has some extra commands that require BuildKit.

@seesharprun if you create a ticket at https://github.com/tmds/build-image/issues I'll look into it.

mehdihadeli commented 1 year ago

This a helpful thing, if we have an option for exporting docker file beside of publish a docker image

baronfel commented 1 year ago

@mehdihadeli are you interested in potentially contributing it? I'd be happy to walk you through the process.

tmds commented 1 year ago

tmds/build-image project gets close, but it has some extra commands that require BuildKit.

I've made some improvements to https://github.com/tmds/build-image. By specifying the --portable flag you can create a Dockerfile that doesn't depend on BuildKit. I've also aligned the property names with those used by sdk-container-builds.

dotnet tool install -g dotnet-build-image
dotnet new web -o web
cd web
dotnet build-image --portable --as-file Dockerfile
clarkezone commented 1 year ago

tmds/build-image project gets close, but it has some extra commands that require BuildKit.

I've made some improvements to https://github.com/tmds/build-image. By specifying the --portable flag you can create a Dockerfile that doesn't depend on BuildKit. I've also aligned the property names with those used by sdk-container-builds.


dotnet tool install -g dotnet-build-image

dotnet new web -o web

cd web

dotnet build-image --portable --as-file Dockerfile

This looks to be exactly what I'm after. Will give it a try

baterja commented 2 months ago

Hello there, I was looking for a possibility to integrate container creation using dotnet publish with docker compose. So as it seems impossible, we will have to stay with the manual Dockerfiles creation 😞 Honestly I'm surprised that emitting Dockerfile wasn't the first step before abstracting it away by hiding container creation details under the hood. Dependency on the docker engine is currently obvious anyway (what is a separate issue...).

baronfel commented 2 months ago

Emitting Dockerfiles is hard to do with 100% fidelity due to things like NuGet authentication. In many ways, directly creating the image tarballs and json was the easier thing to do! In addition, even if we did do Dockerfiles, you wouldn't get great docker-compose integration - the compose file tooling is not easily extensible (you have to write and distribute a buildx plugin, and that is a LARGE amount of work) and so you wouldn't be able to integrate the dotnet CLI nicely with the compose management process.

What we did aligns with how the JIB and ko communities for Java and go integrate with compose, so I'm pretty satisfied with that currently.

Having said that, we do want to do Dockerfile generation, but we don't have an internal customer really pushing for that development, and we've been focusing on workflows that support other team efforts like Aspire. The best way to get this functionality added would be to send us a PR - we love working with community members to make the SDK Container publishing better for everyone!

baterja commented 2 months ago

Thanks for your comment @baronfel. I'll take a look at Aspire. Maybe instead of trying to make docker compose work with dotnet publish going the other way and replacing compose with Aspire will be easier to achieve.