-
Hey there. I have a NextJS website that requires access to a database during the build phase (static site generation). Is it possible to arrange such setup using Aspire? Basically I need to create a database and somehow pass it's connection string as a build secret to docker build. Seems like Any help appreciated. EDIT: I suppose as a workaround I can create username and password myself and use them to create the database resource and then construct the connection string manually, but still curious if it is possible to use opaque connection string generated by the database resource itself. EDIT 2: Ugh, figured out how to manually construct the connection string (using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Looking at the code, it should be possible if you use the lower-level annotation to make this work: public static IResourceBuilder<T> WithBuildSecret<T>(this IResourceBuilder<T> builder, string name, IResourceBuilder<IResourceWithConnectionString> value) where T : ContainerResource
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);
ArgumentNullException.ThrowIfNull(value);
var annotation = builder.Resource.Annotations.OfType<DockerfileBuildAnnotation>().SingleOrDefault();
if (annotation is null)
{
throw new InvalidOperationException("The resource does not have a Dockerfile build annotation. Call WithDockerfile before calling WithSecretBuildArg.");
}
annotation.BuildSecrets[name] = value.Resource;
return builder;
} PS: That said, I'm not sure if this is the "right" thing to do. |
Beta Was this translation helpful? Give feedback.
-
To avoid complicating the code decided to solve this with some config duplication and just provide username/password and connection strings as separate But now there's another issue: it seems that either network resources are inaccessible inside container build process or container build process doesn't respect DB healthchecks. E.g. in this example // Program.cs
var builder = DistributedApplication.CreateBuilder(args);
var db = builder.AddPostgres("db");
var pgTest = builder.AddDockerfile("pg-test", "./").WaitFor(db);
builder.Build().Run(); # Dockerfile
FROM httpd
RUN apt update
RUN apt install -y postgresql
RUN pg_isready -U postgres -h db
|
Beta Was this translation helpful? Give feedback.
Mkay, after some more investigation it turns out that my build-time connectivity issues likely stem from the way Docker networking works during build.
TLDR: you can't really access other resources during build, even when using docker-compose you need to resolve to some hacks involving the usage of host's network.