Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,55 @@ public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this ID

return builder.AddResource(resource)
.WithGolangDefaults()
.WithArgs([.. allArgs]);
.WithArgs([.. allArgs])
.PublishAsGolangDockerfile(workingDirectory, executable, buildTags);
}

private static IResourceBuilder<GolangAppExecutableResource> WithGolangDefaults(
this IResourceBuilder<GolangAppExecutableResource> builder) =>
builder.WithOtlpExporter();
}

/// <summary>
/// Configures the Golang application to be published as a Dockerfile with automatic multi-stage build generation.
/// </summary>
/// <param name="builder">The resource builder.</param>
/// <param name="workingDirectory">The working directory containing the Golang application.</param>
/// <param name="executable">The path to the Golang package directory or source file to be executed.</param>
/// <param name="buildTags">The optional build tags to be used when building the Golang application.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
#pragma warning disable ASPIREDOCKERFILEBUILDER001
private static IResourceBuilder<GolangAppExecutableResource> PublishAsGolangDockerfile(
this IResourceBuilder<GolangAppExecutableResource> builder,
string workingDirectory,
string executable,
string[]? buildTags)
{
return builder.PublishAsDockerFile(publish =>
{
publish.WithDockerfileBuilder(workingDirectory, context =>
{
var buildArgs = new List<string> { "build", "-o", "/app/server" };

if (buildTags is { Length: > 0 })
{
buildArgs.Add("-tags");
buildArgs.Add(string.Join(",", buildTags));
}

buildArgs.Add(executable);

var buildStage = context.Builder
.From("golang:1.23", "builder")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot See the code here https://github.com/dotnet/aspire/blob/04bae8e2f0dfa71823b74dc3a7a48c630f441e99/src/Aspire.Hosting.JavaScript/JavaScriptHostingExtensions.cs#L316-L322. Use DockerfileBaseImageAnnotation to override the default base image. Also handle detecting the default go version based on the go installed and fallback to version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Added DockerfileBaseImageAnnotation support to customize both build and runtime images, and implemented automatic Go version detection from go.mod and installed toolchain with fallback to 1.23. The implementation follows the JavaScript integration pattern. (46c54ea)

.WorkDir("/build")
.Copy(".", "./")
.Run(string.Join(" ", ["go", .. buildArgs]));

context.Builder
.From("alpine:3.21")
.CopyFrom(buildStage.StageName!, "/app/server", "/app/server")
.Entrypoint(["/app/server"]);
});
});
}
#pragma warning restore ASPIREDOCKERFILEBUILDER001
}
12 changes: 12 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.Golang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ To have the Golang application listen on the correct port, you can use the follo
r.Run(":"+os.Getenv("PORT"))
```

## Publishing

When publishing your Aspire application, the Golang resource automatically generates a multi-stage Dockerfile for containerization. This means you don't need to manually create a Dockerfile for your Golang application.

The generated Dockerfile:
- Uses `golang:1.23` as the build stage to compile your application
- Uses `alpine:3.21` as the runtime stage for a smaller final image
- Respects your build tags if specified
- Builds the executable specified in your `AddGolangApp` call

This automatic Dockerfile generation happens when you publish your Aspire application and requires no additional configuration.

## Additional Information

https://learn.microsoft.com/dotnet/aspire/community-toolkit/hosting-golang
Expand Down
Loading