You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using Docker.DotNet;
using Docker.DotNet.Models;
using System.Text;
using ICSharpCode.SharpZipLib.Tar;
using var client = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine"))
.CreateClient();
await BuildDockerImage(client, ".");
static async Task BuildDockerImage(DockerClient client, string dockerFileDirectory)
{
// Create an ImageBuildParameters object
var buildParameters = new ImageBuildParameters
{
Dockerfile = "Dockerfile",
Remove = true, // Remove intermediate containers after a successful build
Tags = new[] { "yourimagename:tag" }, // Set your image name and tag
ForceRemove = true, // Always remove intermediate containers, even upon failure
NoCache = true // Do not use the cache when building the image,
};
// Read the dockerfile content
var dockerFileContent = File.ReadAllText(Path.Combine(dockerFileDirectory, buildParameters.Dockerfile));
// Create a tarball with the Dockerfile
var tarball = new FileStream("new.tar", FileMode.Create);
await using (var tarArchive = new TarOutputStream(tarball))
{
var tarEntry = TarEntry.CreateTarEntry("Dockerfile");
var fileBytes = Encoding.UTF8.GetBytes(dockerFileContent);
tarEntry.Size = fileBytes.Length;
tarArchive.PutNextEntry(tarEntry);
tarArchive.Write(fileBytes, 0, fileBytes.Length);
tarArchive.CloseEntry();
}
tarball = new FileStream("new.tar", FileMode.Open);
// Build the image
await using (var responseStream = await client.Images.BuildImageFromDockerfileAsync(tarball, buildParameters))
{
// Read the response from the Docker API
using var reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync();
Console.WriteLine(line);
}
}
}
What actually happened?:
{"errorDetail":{"message":"the --chmod option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with BuildKit enabled"},"error":"the --chmod option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with BuildKit enabled"}
What did you expect to happen?:
Image is created equivalent to doing docker build on the command line.
I am wondering if this is the same problem causing me difficulties related to multi-architecture builds. If I try to do this in my Dockerfile, I'd expect to get the $BUILDPLATFORM from docker, similar to how it appears via the docker CLI.
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build-env
Output of
dotnet --info
:What version of Docker.DotNet?:
Steps to reproduce the issue:
Dockerfile
Code (ConsoleApp calling BuildImageFromDockerfileAsync)
What actually happened?:
What did you expect to happen?:
Image is created equivalent to doing
docker build
on the command line.Additional information:
moby/moby#41711 (comment)
The text was updated successfully, but these errors were encountered: